Java--------------------------学生管理系统

5 篇文章 0 订阅

 

 

 

 

 

 

 

 

 主方法

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class app {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到学生管理系统-------");
            System.out.println("请选择操作:1.登录  2.注册  3.忘记密码 4.退出系统");

            int choose = sc.nextInt();

            switch (choose) {
                case 1: {
                    login(list);
                    break;
                }
                case 2: {
                    register(list);
                    break;
                }
                case 3: {
                    foegetpassword(list);
                    break;
                }
                case 4: {
                    System.out.println("谢谢使用,再见");
                    System.exit(0);
                }
                default: {
                    System.out.println("输入错误");
                    break;
                }
            }
        }
    }


    //登录
    private static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
//三次登录后自动锁定
        for (int i = 0; i < 3; i++) {
            //用户输入
            System.out.println("请输入用户名");
            String username = sc.next();
            boolean flag = contain(list, username);
            if (!flag) {
                System.out.println("输入的用户名未注册,请先注册");
                return;
            }

            //密码输入
            System.out.println("请输入密码");
            String password = sc.next();

            //验证码输入
            while (true) {
                String rightcode = getcode();
                System.out.println("当前正确验证码为:" + rightcode);
                System.out.println("请输入验证码");
                String code = sc.next();
                if (rightcode.equalsIgnoreCase(code)) {
                    System.out.println("验证码输入正确");
                    break;
                } else {
                    System.out.println("验证码输入错误,请重新输入");
                    continue;
                }
            }

            //判断用户名和密码是否正确
//        封装思想把俩个需要用的条件放在对象里面
            User infor = new User(username, password, null, null);
            boolean result = checkuserinfor(list, infor);
            if (result) {
                System.out.println("用户名密码正确,成功进入学生管理系统");
//                创建对象,调用方法,启动学生管理系统、
                studentsystem ss = new studentsystem();
                ss.startstudentSystem();

                break;
            } else {
                System.out.println("用户名或密码错误");
                if (i == 2) {
                    System.out.println("用户名" + username + "已经被锁定请联系xx解锁,电话是:xxxxxxxx");
//                    锁定账户直接结束方法
                    return;
                } else {
                    System.out.println("还剩下" + (2 - i) + "次机会");
                }
            }
        }
    }

    //  检查对象里面的用户名和密码是否和注册集合里面的内容相同
    private static boolean checkuserinfor(ArrayList<User> list, User infor) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUsername().equals(infor.getUsername()) && user.getPassword().equals(infor.getPassword())) {
                return true;
            }
        }
        return false;

    }


    //注册
    private static void register(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        String usename;
        String password;
        String personID;
        String phonenumber;

//用户
        while (true) {
            System.out.println("请输入用户名");
            usename = sc.next();

//        长度在3-15之间
//        只能是数字字母的组合
//        但是不能全是数字
            boolean flag1 = checkusename(usename);
            if (!flag1) {
                System.out.println("用户名输入有误请重新输入");
                continue;
            }
            //        用户名唯一判断
            boolean flag2 = contain(list, usename);
            if (flag2) {
                System.out.println("当前用户名已存在请重新输入");
                continue;
            } else {
                System.out.println("当前" + usename + "可用");
                break;
            }
        }


//        密码

        while (true) {
            System.out.println("请输入密码");
            password = sc.next();
            System.out.println("请再次输入密码");
            String againpassword = sc.next();
            if (!password.equals(againpassword)) {
                System.out.println("俩次密码不一致,请重新输入");
                continue;
            } else {
                System.out.println("输入正确");
                break;
            }
        }

//        身份证

//        长度为18
//        不能以0开头
//        前17位位数字
//        最后一位可以为数字也可以为字母
        while (true) {
            System.out.println("请输入身份证");
            personID = sc.next();
            boolean flag = checkpersonID(personID);
            if (!flag) {
                System.out.println("身份证错误,请重新输入");
                continue;
            } else {
                System.out.println("身份证录入成功");
                break;
            }
        }


//        手机号码
//        长度11位
//        不能以0开头必须都是数字
        while (true) {
            System.out.println("请输入手机号码");
            phonenumber = sc.next();
            boolean flag = checkphonenumber(phonenumber);
            if (flag) {
                System.out.println("手机号码录入成功");
                break;
            } else {
                System.out.println("手机号码格式错误,请重新录入");
                continue;
            }
        }

//把用户名,密码,身份证号,手机号码放入对象
        User s = new User(usename, password, personID, phonenumber);
        list.add(s);
        System.out.println("注册成功");
        //遍历
        printlist(list);


    }

    //打印集合中内容
    private static void printlist(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            System.out.println(user.getUsername() + "," + user.getPassword() + "," + user.getPeisonID() + "," + user.getPhoneNumber());
        }
    }


    //    判断手机号码是否满足
    private static boolean checkphonenumber(String phonenumber) {
        //        长度11位

        if (phonenumber.length() != 11) {
            return false;
        }
//        不能以0开头
        if (phonenumber.startsWith("0")) {
            return false;
        }
//        必须都是数字
        for (int i = 0; i < phonenumber.length(); i++) {
            char c = phonenumber.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        return true;

    }


    //    判断身份证是否满足
    private static boolean checkpersonID(String personID) {
        //        长度为18
        if (personID.length() != 18) {
            return false;
        }

//        不能以0开头
        if (personID.startsWith("0")) {
            return false;
        }

//        前17位位数字
        for (int i = 0; i < personID.length() - 1; i++) {
            char c = personID.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
//        最后一位可以为数字也可以为字母
        int len = personID.length() - 1;
        char s = personID.charAt(len);
        if (!((s >= '0' && s <= '9') || (s == 'x') || (s == 'X'))) {
            return false;
        } else {
            return true;
        }


    }

    //    判断用户名条件
    private static boolean checkusename(String usename) {
        //        长度在3-15之间
        int len = usename.length();
        if (len < 3 || len > 15) {
            return false;
        }

//        只能是数字字母的组合
        for (int i = 0; i < usename.length(); i++) {
            char c = usename.charAt(i);
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                return false;
            }
        }

//        但是不能全是数字
        int count = 0;
        for (int i = 0; i < usename.length(); i++) {

            char c = usename.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                count++;
                break;
            }

        }
        return count > 0;
    }

    //忘记密码
    private static void foegetpassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
//        判断用户名是否存在
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean flag = contain(list, username);
        if (!flag) {
            System.out.println("用户名未注册");
            return;
        }

        System.out.println("请输入身份证号");
        String personID = sc.next();

        System.out.println("请输入手机号码");
        String phonenumber = sc.next();

        int index = findindex(list, username);
//        需要修改的集合的索引拿出来,进行修改
        User user = list.get(index);
//        判断输入身份证和手机号码是否和集合中原本储存的内容一致
        if (!(user.getPeisonID().equalsIgnoreCase(personID) && user.getPhoneNumber().equals(phonenumber))) {
            System.out.println("身份证或者电话号码输入有误不能修改密码");
            return;
        }

//        说明以上身份证和电话号码都正确
        String newpassword;
//        循环以防输入密码不一致
        while (true) {
//            输入密码俩次
            System.out.println("请输入新的密码");
            newpassword = sc.next();
            System.out.println("请再次输入密码");
            String agnewpassword = sc.next();

            if (newpassword.equals(agnewpassword)) {
                System.out.println("密码俩次输入一致,");
                break;
            } else {
                System.out.println("俩次密码不一致请重新输入");
                continue;
            }
        }
//放入集合
        user.setPassword(newpassword);
        System.out.println("密码修改成功");


    }

    //找到集合中和输入的用户名一样的索引的值
    private static int findindex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }

    //遍历与集合中用户名是否一致
    private static boolean contain(ArrayList<User> list, String usename) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            String rightusername = user.getUsername();
            if (rightusername.equals(usename)) {
                return true;
            }
        }
        return false;

    }

    //获取验证码
    public static String getcode() {
//        创建集合存下元素
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }
//随机调用四次
        Random r = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }


        int number = r.nextInt(10);
        sb.append(number);

//        交换
//           字符串没有办法改变,所以要变成数组进行交换,然后再创建一个新的字符串
        char[] arr = sb.toString().toCharArray();
//        交换
        int index = r.nextInt(arr.length);
        char temp = arr[index];
        arr[index] = arr[arr.length - 1];
        arr[arr.length - 1] = temp;


        return new String(arr);

    }

}

student类

public class Student {
    private String id;
    private String name;
    private int age;
    private String address;

    public Student() {
    }

    public Student(String id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

学生管理系统类

import java.util.ArrayList;
import java.util.Scanner;

public class studentsystem {

    public static void startstudentSystem() {
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("--------------------欢迎来到郭佳乐管理系统--------------------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.println("请输入你的选择");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1": {
                    addStudent(list);
                    break;
                }
                case "2": {
                    deleteStudent(list);
                    break;
                }
                case "3": {
                    updateStudent(list);
                    break;
                }
                case "4": {
                    queryStudent(list);
                    break;
                }
                case "5": {
                    System.out.println("退出系统");
                    //  break loop;
                    System.exit(0);
                }
                default:
                    System.out.println("输入数据错误");

            }
        }


    }


    public static void addStudent(ArrayList<Student> list) {
        Student a = new Student();

        Scanner sc = new Scanner(System.in);


        while (true) {
            System.out.println("请输入id");
            String id = sc.next();
            boolean flag = contain(list, id);
            if (flag) {
                System.out.println("id已经存在,请重新录入");
            } else {
                a.setId(id);
                break;
            }
        }


        System.out.println("请输入姓名");
        String name = sc.next();
        a.setName(name);

        System.out.println("请输入年龄");
        int age = sc.nextInt();
        a.setAge(age);

        System.out.println("请输入地址");
        String address = sc.next();
        a.setAddress(address);

        list.add(a);


        System.out.println("添加成功");
    }


    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = sc.next();
//        获取到要删除的id
        int gid = getidex(list, id);
//        gid来判断是否存在这个id
        if (gid >= 0) {
            list.remove(gid);
            System.out.println("删除成功");
        } else {
            System.out.println("没有这个id,删除失败");
        }
    }

    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的id");
        String id = sc.next();

        int gidex = getidex(list, id);
        if (gidex < 0) {
            System.out.println("输入的" + id + "不存在,请重新输入");
            return;
        }
//获取学生对象
        Student stu = list.get(gidex);
//        对象重新赋值
        System.out.println("请重新输入学生姓名");
        String newName = sc.next();
        stu.setName(newName);

        System.out.println("请重新输入年龄");
        int newAge = sc.nextInt();
        stu.setAge(newAge);

        System.out.println("请重新输入地址");
        String newAddress = sc.next();
        stu.setAddress(newAddress);

        System.out.println("更新成功");


    }


    public static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无当前学生信息,请添加后查询");
        }

        System.out.println("id\t\tname\tage\taddress");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
        }
        System.out.println("查询学生");
    }


//    查看id是否存在的判断

    public static boolean contain(ArrayList<Student> list, String id) {
//        for (int i = 0; i < list.size(); i++) {
//            Student stu = list.get(i);
//            if (stu.getId().equals(id)){
//                System.out.println("存在这个id");
//                return true;
//            }
//        }
//        return false;
        return getidex(list, id) >= 0;
    }


    public static int getidex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

}

用于登录,注册,修改密码的user类

public class User {
    private String username;
    private String password;
    private String peisonID;
    private String phoneNumber;

    public User() {
    }

    public User(String username, String password, String peisonID, String phoneNumber) {
        this.username = username;
        this.password = password;
        this.peisonID = peisonID;
        this.phoneNumber = phoneNumber;
    }

    /**
     * 获取
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * 设置
     * @param username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 获取
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 获取
     * @return peisonID
     */
    public String getPeisonID() {
        return peisonID;
    }

    /**
     * 设置
     * @param peisonID
     */
    public void setPeisonID(String peisonID) {
        this.peisonID = peisonID;
    }

    /**
     * 获取
     * @return phoneNumber
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * 设置
     * @param phoneNumber
     */
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值