package com.chn.comprehensive; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class ManagerStudent { public static void main(String[] args) { ArrayList<User> list = new ArrayList<>(); while (true) { System.out.println("欢迎来到学生管理系统"); System.out.println("请选择操作:1.登录2.注册3.忘记密码"); Scanner sc = new Scanner(System.in); String choose = sc.next(); switch (choose) { case "1": loginStudent(list); break; case "2": registerStudent(list); break; case "3": forgetStudent(list); break; case "4": { System.out.println("退出"); System.exit(0); break; } default: System.out.println("无此选项"); } } } //注册 private static void registerStudent(ArrayList<User> list) { Scanner sc = new Scanner(System.in); System.out.println("注册"); String username; while (true) { System.out.println("请输入用户名:"); username = sc.next(); boolean flag = checkUsername(username); if (flag) { System.out.println("格式满足条件,继续判断用户名是否唯一"); } else { System.out.println("格式不满足条件,请重新输入"); continue; } boolean contains = contains(list, username); if (!contains) { System.out.println("用户名" + username + "可用"); break; } else { System.out.println("此用户名已注册,请重新输入"); } } String Password; while (true) { System.out.println("请输入要注册的密码"); Password = sc.next(); System.out.println("请再次输入要注册的密码"); String AgainPassword = sc.next(); if (Password.equals(AgainPassword)) { System.out.println("两次密码录入一致,请继续录入其他信息"); break; } else { System.out.println("两次密码录入不一致,请重新录入"); } } String personID; while (true) { System.out.println("请输入身份证号码"); personID = sc.next(); boolean flag = checkPersonID(personID); if (flag) { System.out.println("格式满足条件,请继续录入数据"); break; } else { System.out.println("格式不满足条件,请重新录入"); } } String phoneNumber; while (true) { System.out.println("请输入手机号"); phoneNumber = sc.next(); boolean flag = checkphoneNumber(phoneNumber); if (flag) { System.out.println("格式满足条件"); break; } else { System.out.println("格式不满足条件,请重新输入"); } } User user = new User(username, Password, personID, phoneNumber); list.add(user); System.out.println("注册成功"); System.out.println(user.getUsername() + "," + user.getPassword() + "," + user.getPersonID() + "," + user.getPhoneNumber()); } //登录 private static void loginStudent(ArrayList<User> list) { System.out.println("登录"); Scanner sc = new Scanner(System.in); for (int i = 0; i < 3; i++) { System.out.println("请输入用户名"); String username = sc.next(); boolean flag = contains(list, username); if (!flag) { System.out.println("用户名" + username + "未注册,请先注册再登录"); 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 (code.equalsIgnoreCase(rightcode)) { System.out.println("验证码正确"); break; } else { System.out.println("验证码不正确,请重新输入"); } } User useInfo = new User(username, password, null, null); boolean result = checkUseInfo(list, useInfo); if (result) { System.out.println("登录成功"); StudentSystem sy=new StudentSystem(); //创建对象调用方法,使用学生管理系统 sy.system(); break; } else { System.out.println("登录失败,用户名或密码错误,还有" + (2 - i) + "次机会"); if (i == 2) { System.out.println("账户已被冻结,请联系客服"); } } } } private static void forgetStudent(ArrayList<User> list) { System.out.println("忘记密码"); Scanner sc = new Scanner(System.in); System.out.println("请输入用户名"); String username = sc.next(); boolean flag = contains(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.getPersonID().equals(personID) && user.getPhoneNumber().equals(phoneNumber))) { System.out.println("身份证或手机号错误,不能修改密码"); return; }//这里之所以取反是为了提高代码的可读性,如果按照if(true)的写法就需要将下面的while循环写入到if循环中,会显得代码可读性差 String password;//定义全局变量 while (true) { System.out.println("请输入要修改的密码"); password = sc.next(); System.out.println("再次输入要修改的密码"); String Againpassword = sc.next(); if (password.equals(Againpassword)) { System.out.println("两次密码输入一致"); break; }else{ System.out.println("两次密码输入不一致"); continue; } } //这里user对象就是上面通过用户名取出的对象 user.setPassword(password); System.out.println("密码修改成功" ); } private static boolean checkphoneNumber(String phoneNumber) { if (phoneNumber.length() != 11) { return false; } if (phoneNumber.charAt(0) == '0') { return false; } return true; } private static boolean checkPersonID(String personID) { if (personID.length() != 18) { return false; } if (personID.charAt(0) == 0) { return false; } for (int i = 0; i < personID.length() - 2; i++) { char c = personID.charAt(i); if (c < '0' || c > '9') { return false; } } char c = personID.charAt(personID.length() - 1); if (!((c >= '0' && c <= '9') || c == 'x' || c == 'X')) { return false; } return true; } public static boolean checkUsername(String username) { //用户名长度在3-15之间 int len = username.length(); if (len > 3 && len < 15) { return true; } //用户名由数字和字母组成 for (int i = 0; i < username.length(); i++) { char c = username.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 < username.length(); i++) { char c = username.charAt(i); if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { count++; break; } } return count > 0; } public static boolean contains(ArrayList<User> list, String username) { for (int i = 0; i < list.size(); i++) { User user = list.get(i); String name = user.getUsername(); if (name.equals(username)) { return true; } } return false; } private static boolean checkUseInfo(ArrayList<User> list, User useInfo) { User user = new User(); for (int i = 0; i < list.size(); i++) { String username = list.get(i).getUsername(); String password = list.get(i).getPassword(); if (useInfo.getUsername().equals(username) && useInfo.getPassword().equals(password)) { return true; } } return false; } //随机生成验证码 public static String getcode() { ArrayList<Character> list = new ArrayList<>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 26; i++) { list.add((char) ('a' + i)); list.add((char) ('A' + i)); } //随机抽取四个字符 Random rd = new Random(); for (int i = 0; i < 4; i++) { int index = rd.nextInt(list.size()); Character c = list.get(index); sb.append(c); } int number = rd.nextInt(10); sb.append(number); //将sb转化成字符数组 //将最后一个字符随机放置在任意位置,然后将最有一个字符的位置换成随机索引对应的字符(用temp来保存这个字符) //因为这个位置的数据会被(放置在最后一位的数字)修改 char[] arr = sb.toString().toCharArray(); int RandomIndex = rd.nextInt(arr.length); char temp = arr[RandomIndex]; arr[RandomIndex] = arr[arr.length - 1]; arr[arr.length - 1] = temp; return new String(arr); } private static int findIndex(ArrayList<User> list, String username) { for (int i = 0; i < list.size(); i++) { User user = list.get(i); String name = user.getUsername(); if (name.equals(username)) { return i; } } return -1; } }
//增删改查操作界面 package com.chn.comprehensive; import com.chn.upgrade.Student; import java.util.ArrayList; import java.util.Scanner; public class StudentSystem { public static void system() { 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.退出"); Scanner sc=new Scanner(System.in); System.out.println("请选择:"); String choose = sc.next(); switch(choose){ case "1": addStudent(list);break; case "2": deleteStudent(list);break; case "3": modifyStudent(list);break; case "4": queryStudent(list);break; case "5": { System.out.println("退出"); System.exit(0); } default: System.out.println("没有此选项"); } } } public static void addStudent(ArrayList<Student> list){ Student student=new Student(); Scanner sc=new Scanner(System.in); while (true) { System.out.println("请输入学生id"); String sid = sc.next(); if(contains(list,sid)){ System.out.println("请重新录入"); }else{ student.setId(sid); break; } } System.out.println("请输入学生姓名"); String name = sc.next(); student.setName(name); System.out.println("请输入学生年龄"); int age = sc.nextInt(); student.setAge(age); System.out.println("请输入学生家庭住址"); String address = sc.next(); student.setAddress(address); list.add(student); } public static boolean contains(ArrayList<Student> list,String id){ // for (int i = 0; i < list.size(); i++) { // Student student = list.get(i); // String sid = student.getId(); // if(sid.equals(id)){ // return true; // } // } // return false; return getIndex(list,id)>=0; } public static int getIndex(ArrayList<Student> list,String id){ for (int i = 0; i < list.size(); i++) { Student student = list.get(i); String sid = student.getId(); if(sid.equals(id)){ return i; } } return -1; } public static void queryStudent(ArrayList<Student> list){ if(list.size()==0){ System.out.println("无信息"); return; } System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址\t"); for (int i = 0; i < list.size(); i++) { Student student = list.get(i); System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t\t"+student.getAddress()); } } public static void deleteStudent(ArrayList<Student> list){ Scanner sc=new Scanner(System.in); System.out.println("请输入要删除的学生id"); String id = sc.next(); int index = getIndex(list, id); if(index>=0){ list.remove(index); System.out.println("删除成功"); }else { System.out.println("删除失败"); } } public static void modifyStudent(ArrayList<Student> list){ Scanner sc=new Scanner(System.in); System.out.println("请输入要修改的学生id"); String newId = sc.next(); int index = getIndex(list, newId); if(index==-1){ System.out.println("学生不存在,请重新输入"); return; } Student student = list.get(index); student.setId(newId); System.out.println("请输入要修改学生的姓名"); String newName = sc.next(); student.setName(newName); System.out.println("请输入要修改学生的年龄"); int newAge = sc.nextInt(); student.setAge(newAge); System.out.println("请输入要修改学生的家庭地址"); String newAddress = sc.next(); student.setAddress(newAddress); System.out.println("学生信息修改成功"); } }