首先得先写个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;}/*** 获取* @return id*/public String getId() {return id;}/*** 设置* @param id*/public void setId(String id) {this.id = id;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}/*** 获取* @return address*/public String getAddress() {return address;}/*** 设置* @param address*/public void setAddress(String address) {this.address = address;}public String toString() {return "Student{id = " + id + ", name = " + name + ", age = " + age + ", address = " + address + "}";}
}
再写个User类,用来存放用户名,密码,身份证号,手机号
public class User {private String username;private String password;private String idOnly;private String phone;public User() {}public User(String username, String password, String idOnly, String phone) {this.username = username;this.password = password;this.idOnly = idOnly;this.phone = phone;}/*** 获取* @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 idOnly*/public String getIdOnly() {return idOnly;}/*** 设置* @param idOnly*/public void setIdOnly(String idOnly) {this.idOnly = idOnly;}/*** 获取* @return phone*/public String getPhone() {return phone;}/*** 设置* @param phone*/public void setPhone(String phone) {this.phone = phone;}public String toString() {return "User{username = " + username + ", password = " + password + ", idOnly = " + idOnly + ", phone = " + phone + "}";}
}
接下来就可以开始写正式的代码了
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;public class StudentSystem {public static void main(String[] args) {ArrayList<User>userList=new ArrayList<>();int count=0;while (true) {System.out.println("欢迎来到学生管理系统");System.out.println("请选择操作1登录 2注册 3忘记密码");Scanner sd=new Scanner(System.in);int s = sd.nextInt();switch (s){case 1: {boolean login = login(userList);if(count==3){System.out.println("您已输入三次错误");exit();}if (login){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);int number = sc.nextInt();switch (number) {case 1:addStudent(list);break;case 2:deleteStudent(list);break;case 3:reviseStudent(list);break;case 4:selectStudent(list);break;case 5:exit();System.exit(0);//停止虚拟机运行default:System.out.println("没有这个选项");}}}else {System.out.println("用户名密码错误,请重新输入");count++;break;}}case 2:zhuCe(userList);break;case 3:forget(userList);break;default:System.out.println("没有这个选项");}}}//添加学生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 = exist(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 deleteStudent(ArrayList<Student>list){System.out.println("您要删除的学生id");Scanner sc=new Scanner(System.in);String id=sc.next();int i = index(list, id);if(i>=0){list.remove(i);System.out.println("id为"+id+"的学生删除成功");}else{System.out.println("id不存在,删除失败");}}//修改学生public static void reviseStudent(ArrayList<Student>list){Scanner sc=new Scanner(System.in);System.out.println("请输入要修改的学生id");String stuId = sc.next();int i = index(list, stuId);if(i>=0){Student stu=list.get(i);System.out.println("请输入学生姓名");String name=sc.next();stu.setName(name);System.out.println("请输入学生年龄");int age=sc.nextInt();stu.setAge(age);System.out.println("请输入学生住址");String address=sc.next();stu.setAddress(address);System.out.println("学生信息修改成功");}else{System.out.println("要修改的id"+stuId+"不存在,请重新输入");return;}}//查询学生public static void selectStudent(ArrayList<Student>list){if(list.size()==0){System.out.println("当前无学生信息,请添加后再查询");return;}System.out.println("id\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"+student.getAddress());}}//退出public static void exit(){System.out.println("退出");}//是否存在idpublic static boolean exist(ArrayList<Student>list,String id){for (int i = 0; i < list.size(); i++) {Student stu = list.get(i);String stuId = stu.getId();if(stuId.equals(id)){return true;}}return false;}//根据id获取索引public static int index(ArrayList<Student>list,String id){for (int i = 0; i < list.size(); i++) {Student stu = list.get(i);String stuId = stu.getId();if(stuId.equals(id)){return i;}}return -1;}//登录public static boolean login(ArrayList<User>userList){Scanner sc=new Scanner(System.in);System.out.println("请输入用户名");String name = sc.next();System.out.println("请输入密码");String pwd=sc.next();String yesCode=getCode();System.out.println("请输入验证码");String code=sc.next();for (int i = 0; i < userList.size(); i++) {User user = userList.get(i);String username = user.getUsername();String password = user.getPassword();if(username.equals(name)&&password.equals(pwd)&&(code.equalsIgnoreCase(yesCode))){return true;}}boolean exist = existUser(userList, name);if(exist==false){System.out.println("用户未注册,请先注册");}return false;}//注册public static void zhuCe(ArrayList<User>userList){User user=new User();Scanner sc=new Scanner(System.in);while (true) {System.out.println("请输入用户名");String username = sc.next();boolean flag1 = checkUsername(username);if (!flag1) {System.out.println("用户格式不满足,需要重新输入");continue;}boolean flag2 = existUser(userList, username);if(flag2){System.out.println("用户名"+username+"已存在,请重新输入");}else{System.out.println("用户名"+username+"可用");user.setUsername(username);break;}}while (true) {System.out.println("请输入密码");String password = sc.next();System.out.println("请再次输入密码");String againPassword=sc.next();if(!againPassword.equals(password)){System.out.println("两次输入的密码不一致,请重新输入");}else{System.out.println("两次输入的密码一致");user.setPassword(password);break;}}while (true) {System.out.println("请输入身份证号,长度18,不能以0开头,前17为必须是数字,最后一位可以是数字,也可以是大写X或小写x");String idOnly = sc.next();boolean flag3 = checkIdOnly(idOnly);if(flag3){System.out.println("身份证"+idOnly+"可用");user.setIdOnly(idOnly);break;}else{System.out.println("身份证"+idOnly+"没有按要求填写");}}while (true) {System.out.println("请输入手机号码,长度为11,不能以0开头,必须都是数字");String phone = sc.next();boolean flag4 = checkPhone(phone);if(flag4){System.out.println("手机号"+phone+"可用");user.setPhone(phone);break;}else{System.out.println("手机号"+phone+"没有按要求填写");}}userList.add(user);}//生成验证码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));}StringBuilder sb=new StringBuilder();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);}int number=r.nextInt(10);sb.append(number);System.out.print("正确的验证码为:");System.out.println(sb);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);}//校验手机号private static boolean checkPhone(String phone) {//长度为11if(phone.length()!=11){return false;}//不能以0开头boolean flag = phone.startsWith("0");//如果判断正确返回trueif(flag){return false;}//必须都是数字for (int i = 0; i < phone.length()-1; i++) {char c = phone.charAt(i);if(!(c>='0'&&c<='9')){return false;}}return true;}//校验身份证private static boolean checkIdOnly(String idOnly) {if(idOnly.length()!=18){return false;}/*开头不能为0*/boolean flag = idOnly.startsWith("0");//如果判断正确返回trueif(flag){return false;}//前17为必须是数字for (int i = 0; i < idOnly.length()-1; i++) {char c = idOnly.charAt(i);if(!(c>='0'&&c<='9')){return false;}}//最后一位char endChar = idOnly.charAt(idOnly.length() - 1);if(!((endChar>='0'&&endChar<='9')||(endChar=='X')||(endChar=='x'))){return false;}return true;}//忘记密码public static void forget(ArrayList<User>userList){User user=new User();System.out.println("请输入用户名");Scanner sc=new Scanner(System.in);String username = sc.next();boolean flag = existUser(userList, username);if(flag){System.out.println("请输入身份证号");String idOnly=sc.next();boolean result1 = existIdOnly(userList, idOnly);System.out.println("请输入手机号");String phone=sc.next();boolean result2 = existPhone(userList, phone);if(result1&&result2){System.out.println("请输入密码");String password=sc.next();user.setPassword(password);}else{System.out.println("账号信息不匹配,修改失败");}}else{System.out.println("未注册");}}//校验用户名public static boolean checkUsername(String username){//用户名长度在3~15之间int len=username.length();if(len<3||len>15){return false;}//只能是字母+数字,但不能是纯数字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 existUser(ArrayList<User>userList,String username){for (int i = 0; i < userList.size(); i++) {User user = userList.get(i);String stuId = user.getUsername();if(stuId.equals(username)){return true;}}return false;}//是否存在身份证号public static boolean existIdOnly(ArrayList<User>userList,String idOnly){for (int i = 0; i < userList.size(); i++) {User user = userList.get(i);String stuIdOnly = user.getIdOnly();if(stuIdOnly.equals(idOnly)){return true;}}return false;}//是否存在手机号public static boolean existPhone(ArrayList<User>userList,String phone){for (int i = 0; i < userList.size(); i++) {User user = userList.get(i);String stuPhone = user.getPhone();if(stuPhone.equals(phone)){return true;}}return false;}
}
运行结果