【Java基础】---简易学习系统Plus版

发布时间:2023年12月23日

题目概述:

需求:
为学生管理系统书写一个登陆、注册、忘记密码的功能只有用户登录成功之后,才能进入到学生管理系统中进行增删改查操作.

分析:

登录界面:
system.out.printIn("欢迎来到学生管理系统”) ;

System.out.println("请选择操作1登录 2注册 3忘记密码");


用户类:
属性:用户名、密码、身份证号码、手机号码注册功能:
1,用户名需要满足以下要求:验证要求:
用户名唯一
用户名长度必须在3~15位之间
只能是字母加数字的组合,但是不能是纯数字

2,密码键盘输入两次,两次一致才可以进行注册
3,身份证号码需要验证。
验证要求:
长度为18位
不能以0为开头
前17位,必须都是数字

最为一位可以是数字,也可以是大写X或小写x
4,手机号验证。
验证要求
长度为11位
不能以0为开头
必须都是数字

登录功能:
1,键盘录入用户名
2,键盘录入密码
3,键盘录入验证码
验证要求:
用户名如果未注册,直接结束方法,并提示:用户名未注册,请先注册判断验证码是否正确,如不正确,重新输入再判断用户名和密码是否正确,有3次机会

忘记密码:
键盘录入用户名,判断当前用户名是否存在,如不存在,直接结束方法,并提示:未注册1.
键盘录入身份证号码和手机号码2
3,判断当前用户的身份证号码和手机号码是否一致
如果一致,则提示输入密码,进行修改
如果不一致,则提示: 账号信息不匹配,修改失败

验证码规则:
长度为5
由4位大写或者小写字母和1位数字组成,同一个字母可重复
数字可以出现在任意位置
比如:
aQa1K

代码实现:

public class 学生管理系统_02 {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到Boss学生管理系统--------");
            System.out.println("请选择操作:1.登录 2.注册 3.忘记密码");
            String choose = scanner.next();
            switch (choose) {
                case "1":
                    System.out.println("请输入你的用户名:");
                    String uName = scanner.next();
                    User user1 = login(users, uName);
                    System.out.println(user1);
                    if (user1 != null) {
                        for (int i = 0; i < 3; i++) {
                            System.out.println("请输入你的密码:");
                            String uPassword = scanner.next();
                            String s = YanZheng();
                            System.out.println("请输入你的验证码:" + s);
                            String uYanZheng = scanner.next();
                            String password = user1.getPassword();
                            if (uYanZheng.equals(s) && password.equals(uPassword)) {
                                System.out.println("登录成功!");
                                indexMenu();
                                break;
                            } else {
                                System.out.println("密码或验证码错误!请重新输入!你还有" + (2 - i));
                            }
                        }
                    }
                    break;
                case "2":
                    String username = null;
                    String password = null;
                    String idCard = null;
                    String pNumber = null;
                    //用户名
                    while (true) {
                        System.out.println("请输入你的用户名:");
                        username = scanner.next();
                        boolean flag = contains(users, username);
                        if (flag) {
                            if (username.length() >= 3 && username.length() <= 15) {
                                int count = 0;
                                for (int i = 0; i < username.length(); i++) {
                                    //判断username中数字的个数
                                    char c = username.charAt(i);
                                    if (c >= '0' && c <= '9') {
                                        count++;
                                    }
                                }
                                if (count == 0 || count == username.length()) {
                                    System.out.println("密码只能是字母加数字的组合!");
                                } else {
                                    break;
                                }
                            } else {
                                System.out.println("密码长度要在3-15!");
                            }

                        } else {
                            System.out.println("用户名重复,请重新输入!!");
                        }
                    }
                    //密码
                    while (true) {
                        System.out.println("请输入密码:");
                        password = scanner.next();
                        System.out.println("请确认密码:");
                        String password1 = scanner.next();
                        if (!(password1.equals(password))) {
                            System.out.println("两次密码不一致,请重新输入");
                        } else {
                            break;
                        }

                    }
                    //身份证验证
                    while (true) {
                        System.out.println("请输入你的身份证:");
                        idCard = scanner.next();
                        char[] chars = idCard.toCharArray();
                        int length = chars.length;
                        if (length == 18 && chars[0] != '0' && (chars[length - 1] == 'X' || chars[length - 1] == 'x' || (chars[length - 1] >= '0' && chars[length - 1] <= '9'))) {
                            boolean mark = true;
                            for (int i = 0; i < chars.length - 1; i++) {
                                if (chars[i] >= '0' && chars[i] <= '9') {
                                } else {
                                    System.out.println("身份证输入错误!");
                                    mark = false;
                                    break;
                                }
                            }
                            if (mark) {
                                break;
                            }
                        } else {
                            System.out.println("身份证输入有误,请重新输入!!!");
                        }
                    }
                    //手机号验证
                    while (true) {
                        System.out.println("请输入你的手机号码:");
                        pNumber = scanner.next();
                        if (pNumber.charAt(0) != '0' && pNumber.length() == 11) {
                            boolean mark = true;
                            for (int i = 0; i < pNumber.length(); i++) {
                                char c = pNumber.charAt(i);
                                if (c >= '0' && c <= '9') {
                                } else {
                                    System.out.println("手机号码输入错误!");
                                    mark = false;
                                    break;
                                }
                            }
                            if (mark) {
                                break;
                            }
                        } else {
                            System.out.println("手机号码输入错误!");
                        }
                    }
                    User user = new User(username, password, idCard, pNumber);
                    users.add(user);
                    System.out.println("注册成功!");
                    break;
                case "3":
                    System.out.println("请输入你要修改的用户名:");
                    String uname = scanner.next();
                    User user2 = login(users, uname);
                    if (user2 != null) {
                        System.out.println("请输入你的身份证号:");
                        String uId = scanner.next();
                        System.out.println("请输入你的手机号:");
                        String phoneNumber = scanner.next();
                        if (user2.getIdCard().equals(uId) && user2.getPNumber().equals(phoneNumber)) {
                            System.out.println("新密码:");
                            String newPassword = scanner.next();
                            user2.setPassword(newPassword);
                            System.out.println("修改成功!");
                        } else {
                            System.out.println("账号信息不匹配,修改失败!");
                        }
                    }
                    break;
                default:
                    System.out.println("请输入正确的选项!!!!!!");

            }
        }
    }


    //校验用户名唯一
    public static boolean contains(ArrayList<User> users, String username) {
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            if (user.getUsername().equals(username)) {
                return false;
            }
        }
        return true;
    }

    //随机产生一个验证码
    public static String YanZheng() {
        //将52个大写和小写字母添加到char数组中
        char[] arr = new char[52];
        for (int i = 0; i < arr.length; i++) {
            if (i < 26) {
                arr[i] = (char) (i + 65);
            } else {
                arr[i] = (char) (i + 97 - 26);
            }
        }
        //随机取出数组中的4个数作为验证码的前4位
        Random random = new Random();
        char[] newArr = new char[5];
        for (int i = 0; i < 4; i++) {
            int radomIndex = random.nextInt(52);
            newArr[i] = arr[radomIndex];
        }
        //产生随机的0-9的数字
        int radomNumber = random.nextInt(10);
        newArr[4] = (char) (radomNumber + 48);
        //产生随机的索引  1-4
        int radomIndex = random.nextInt(4);
        char temp = newArr[radomIndex];
        newArr[radomIndex] = newArr[4];
        newArr[4] = temp;
        return new String(newArr);
    }

    //验证登录
    public static User login(ArrayList<User> users, String uName) {
        boolean flag = contains(users, uName);
        User user = null;
        if (flag) {
            System.out.println("用户未注册,请先注册!");
        } else {
            for (int i = 0; i < users.size(); i++) {
                User user1 = users.get(i);
                if (user1.getUsername().equals(uName)) {
                    user = user1;
                }
            }
        }
        return user;
    }

    public static void indexMenu() {
        ArrayList<Student> students = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("--------欢迎来到Boss学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.查询所有学生");
            System.out.println("6.退出");
            System.out.println("请输入你的选择:");
            String s = scanner.next();
            switch (s) {
                case "1":
                    System.out.println("请输入学生的id:");
                    Integer id = scanner.nextInt();
                    for (int i = 0; i < students.size(); i++) {
                        Student student = students.get(i);
                        if (student.getId().equals(id)) {
                            System.out.println("id重复,请重新输入!");
                            break loop;
                        }
                    }
                    System.out.println("请输入学生的name:");
                    String name = scanner.next();
                    System.out.println("请输入学生的age:");
                    int age = scanner.nextInt();
                    System.out.println("请输入学生的address:");
                    String address = scanner.next();
                    Student stu = new Student(id, name, age, address);
                    students.add(stu);
                    System.out.println("添加学生成功!");
                    break;
                case "2":
                    System.out.println("请输入你要删除学生的id:");
                    Integer sid = scanner.nextInt();
                    boolean flag = delectStu(students, sid);
                    if (!flag) {
                        System.out.println("id不存在,请重新输入!");
                    }
                    break;
                case "3":
                    System.out.println("请输入你要修改学生的id:");
                    Integer sid1 = scanner.nextInt();
                    boolean flag1 = updateStu(students, sid1, scanner);
                    if (!flag1) {
                        System.out.println("id不存在,请重新输入!");
                    }
                    break;
                case "4":
                    System.out.println("请输入你要查询学生的id:");
                    Integer sid2 = scanner.nextInt();
                    boolean flag2 = selectStu(students, sid2);
                    if (!flag2) {
                        System.out.println("当前无学生信息,请添加后再次查询!");
                    }
                    break;
                case "5":
                    if (students.size() != 0) {
                        selectStuAll(students);
                    } else {
                        System.out.println("当前学生信息为空,请进行添加!");
                    }
                    break;
                case "6":
//                    System.exit(0);
                    break loop;
            }
        }

    }

    public static boolean delectStu(ArrayList<Student> students, Integer sid) {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(sid)) {
                students.remove(i);
                System.out.println("删除成功!");
                return true;
            }
        }
        return false;
    }

    public static boolean updateStu(ArrayList<Student> students, Integer sid, Scanner scanner) {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(sid)) {
                System.out.println("请输入你要修改学生的name:");
                String name1 = scanner.next();
                System.out.println("请输入你要修改学生的age:");
                int age1 = scanner.nextInt();
                System.out.println("请输入你要修改学生的address:");
                String address1 = scanner.next();
                student.setName(name1);
                student.setAge(age1);
                student.setAddress(address1);
                students.set(i, student);
                System.out.println("修改成功!");
                return true;
            }
        }
        return false;
    }


    public static boolean selectStu(ArrayList<Student> students, Integer sid) {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(sid)) {
                System.out.println("id=" + student.getId() + " name=" + student.getName() + " age=" + student.getAge() + " address=" + student.getAddress());
                return true;
            }
        }
        return false;
    }

    public static void selectStuAll(ArrayList<Student> students) {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println("id=" + student.getId() + " name=" + student.getName() + " age=" + student.getAge() + " address=" + student.getAddress());
        }
    }
    
}

文章来源:https://blog.csdn.net/m0_65368672/article/details/135174556
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。