import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;public class Demo_学生管理系统 {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.忘记密码");String choose = sc.next();switch (choose) {case "1":login(list);break;case "2":register(list);break;case "3":gorgetPassword(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 = 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("验证码错误");continue;}}// 验证用户名和密码是否正确// 集合中是否包含用户名和密码// 封装思想应用// 可以把一些零散的数据,封装成一个对象// 传递参数就可以穿一个整体User userInfo = new User(username, password, null, null);boolean result = checkUsername(list, userInfo);if (result) {System.out.println("登录成功");// 创建对象,调用方法,启动学生管理系统StudentSystem s = new StudentSystem();s.startStudent();break;} else {System.out.println("登录失败!");System.out.println("用户名账户或密码错误");if (i == 2) {System.out.println("当前账号" + username + "被锁定,请联系客服!");return;} else {System.out.println("用户名或密码错误,还剩" + (2 - i) + "次机会!");}}}}// 验证用户名和密码是否正确private static boolean checkUsername(ArrayList<User> list,User userInfo) {// TODO Auto-generated method stub//遍历集合,判断用户是否存在for (int i = 0; i < list.size(); i++) {User user = list.get(i);if(user.getUsername().equals(userInfo.getUsername())&&user.getPassword().equals(userInfo.getPassword())){return true;}}return false;}// 注册private static void register(ArrayList<User> list) {// 把用户名,密码,身份证号码,手机号放到用户对象中// 把用户信息添加到集合中Scanner sc = new Scanner(System.in);String username;String password;String personID;String PhoneNumber;while (true) {System.out.println("请输入用户名");username = sc.next();// 用户名唯一// 先验证格式是否正确,在验证是否唯一// 用户名长度在3-15之间// 只能是字母加数字组合,不能是纯数字boolean flag1 = checkUsername(username);if (!flag1) {System.out.println("用户名不满足格式,需要重新录入");continue;}// 验证是否唯一// username集合中判断是否存在boolean flag2 = contains(list, username);if (flag2) {// 用户名存在System.out.println("用户名" + username + "已存在,请重新输出");} else {// 不存在System.out.println("用户名" + username + "可用");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位必须是数字// 最后以为可以是Xwhile (true) {System.out.println("请输入身份证号码");personID = sc.next();boolean flag = checkPersonID(personID);if (flag) {System.out.println("身份证号码满足要求!");break;} else {System.out.println("身份证号码格式有误!请重新输入");continue;}}// 录入手机号码// 长度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 u = new User(username, password, personID, PhoneNumber);list.add(u);System.out.println("注册成功!");// 遍历集合,打印信息printList(list);// 把用户信息添加到集合中}private static void printList(ArrayList<User> list) {// TODO Auto-generated method stubfor (int i = 0; i < list.size(); i++) {User user = list.get(i);System.out.println(user.getUsername() + "," + user.getPassword() + "," + user.getPersonID() + ","+ user.getPhoneNumbwe());}}private static boolean checkPhoneNumber(String phoneNumber) {// TODO Auto-generated method stub// 长度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) {// TODO Auto-generated method stub// 长度18位if (personID.length() != 18) {return false;}// 不能以0开头// 判断是否以0开头,是返回true 不是返回falseboolean flag = personID.startsWith("0");if (flag) {return false;}// 前17位必须是数字for (int i = 0; i < personID.length() - 1; i++) {char c = personID.charAt(i);// 如果有一个字符不在0-9,直接返回falseif (!(c >= '0' && c <= '9')) {return false;}}char endChar = personID.charAt(personID.length() - 1);if (endChar >= '0' && endChar <= '9' || endChar == 'X' || endChar == 'x') {return true;} else {return false;}}private static boolean contains(ArrayList<User> list, String username) {// TODO Auto-generated method stub// 循环遍历集合,进行比较for (int i = 0; i < list.size(); i++) {User user = list.get(i);String rightUsername = user.getUsername();if (rightUsername.equals(username)) {return true;}}// 循环结束,满足return false;}private static boolean checkUsername(String username) {// TODO Auto-generated method stub// 用户名长度在3-15之间int ten = username.length();if (ten < 3 || ten > 15) {return false;}// 只能是字母加数字组合// 循环username里面每一个字符,如果有一个字符或数字,返回falsefor (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;}// 忘记密码private static void gorgetPassword(ArrayList<User> list) {Scanner sc = new Scanner(System.in);System.out.println("请输入用户名");String username = sc.next();boolean flag = contains(list, username);if (!flag) {System.out.println("当前用户" + username + "未注册,请先注册");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().equalsIgnoreCase(personID) && user.getPhoneNumbwe().equals(phoneNumber))) {System.out.println("身份证号码或手机号码有误,不能修改密码");return;}// 执行到这里表示所有验证都通过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.setPassword(password);System.out.println("密码修改成功!");}// 获取对象private static int findIndex(ArrayList<User> list, String username) {// TODO Auto-generated method stubfor (int i = 0; i < list.size(); i++) {User user = list.get(i);if (user.getUsername().equals(username)) {return i;}}return 0;}// 验证码// 长度为5// 由4位大写或者小写字母和1位数字组成,同一个字母可以重复// 数字可以出现在任何位置private static String getCode() {// 1.创建集合添加所以的大写和小写字母ArrayList<Character> list = new ArrayList<>();for (int i = 0; i < 26; i++) {list.add((char) ('a' + i));list.add((char) ('A' + i));}StringBuilder sb = new StringBuilder();// 2.随机抽取4个字符Random r = new Random();for (int i = 0; i < 4; i++) {// 获取随机索引int index = r.nextInt(list.size());// 利用随机索引获取随机字符char c = list.get(index);// 把随机字符添加到sb中sb.append(c);}// 3.把随机数字添加到末尾int number = r.nextInt(10);sb.append(number);// 4.修改字符串中的内容// 先把字符串变成字符数组,在数组中修改,然后再创建一个新的字符串char[] arr = sb.toString().toCharArray();// 用最后一个索引,跟随机索引进行交换int randomIndex = r.nextInt(arr.length);// 最大索引指向的元素,跟随机索引指向的元素交换char temp = arr[randomIndex];arr[randomIndex] = arr[arr.length - 1];arr[arr.length - 1] = temp;return new String(arr);}
}class User {private String username;private String password;private String personID;private String phoneNumbwe;public User(String username, String password, String personID, String phoneNumbwe) {super();this.username = username;this.password = password;this.personID = personID;this.phoneNumbwe = phoneNumbwe;}public User() {super();}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getPersonID() {return personID;}public void setPersonID(String personID) {this.personID = personID;}public String getPhoneNumbwe() {return phoneNumbwe;}public void setPhoneNumbwe(String phoneNumbwe) {this.phoneNumbwe = phoneNumbwe;}
}class StudentSystem {public void startStudent() {ArrayList<Student> list = new ArrayList<>();loop: 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();// if (choose.equals("1")) {//// } else if (choose.equals("2")) {//// } else if (choose.equals("3")) {//// } else if (choose.equals("4")) {//// } else if (choose.equals("5")) {// System.out.println("退出成功,欢迎下次再来");// break;// } else {// System.out.println("没有这个选项");// }switch (choose) {case "1":addStudent(list);break;case "2":deletStudent(list);break;case "3":updateStudent(list);break;case "4":queryStudent(list);break;case "5": {System.out.println("退出成功,欢迎下次再来");// break loop;System.exit(0);// 停止虚拟机运行}}}}// 添加学生public static void addStudent(ArrayList<Student> list) {// 利用空参构造先创建学生对象Student s = new Student();Scanner sc = new Scanner(System.in);String id = null;while (true) {System.out.println("请输入学生id");id = sc.next();boolean flag = contains(list, id);if (flag) {System.out.println("id已经存在,请重新录入");} else {s.setId(id);break;}}System.out.println("请输入学生姓名");String name = sc.next();s.setName(name);System.out.println("请输入学生年龄");int age = sc.nextInt();s.setAge(age);System.out.println("请输入学生家庭住址");String address = sc.next();s.setAddress(address);// 把学生对象添加到集合中list.add(s);// 提示用户System.out.println("学生信息添加成功");}// 删除学生public static void deletStudent(ArrayList<Student> list) {Scanner sc = new Scanner(System.in);System.out.println("请输入要删除的学生id");String id = sc.next();int index = getId(list, id);// 对index判断// >=0,存在,直接删除if (index >= 0) {list.remove(index);System.out.println("id为" + id + "的学生删除成功");} else {// 如果-1,表示不存在,返回出事菜单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 index = getId(list, id);if (index == -1) {System.out.println("要修改的id" + id + "不存在,请重新输入");return;}// id存在// 获取要修改的学生对象Student stu = list.get(index);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("当前无学生信息,请添加后在查询!");return;}// 打印表头信息System.out.println("id\t姓名\t年龄\t家庭住址");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());}}// 判断id是否已经存在public static boolean contains(ArrayList<Student> list, String id) {// 循环遍历// for (int i = 0; i < list.size(); i++) {// // 拿到学生对象后,获取id并进行判断// Student stu = list.get(i);// String sid = stu.getId();// if (sid.equals(id)) {// return true;// }// }// return false;return getId(list, id) >= 0;}// 通过id获取索引public static int getId(ArrayList<Student> list, String id) {// 遍历集合for (int i = 0; i < list.size(); i++) {// 得到每一个学生对象Student stu = list.get(i);// 得到每一个学生对象的idString sid = stu.getId();// 集合中的学生id和要删除的学生id进行比较if (sid.equals(id)) {// 如果一样就返回索引return i;}}// 不存在,返回不存在的索引return -1;}
}class Student {private String id;private String name;private int age;private String address;public Student(String id, String name, int age, String address) {super();this.id = id;this.name = name;this.age = age;this.address = address;}public Student() {super();}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;}
}
结果: