package StudentSys; import javax.jws.soap.SOAPBinding; 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<>(); 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": { login(list); break; } case "2": { register(list); break; } case "3": { forgetPassWord(list); break; } case "4": { System.out.println("谢谢使用"); System.exit(0); } default: System.out.println("没有这个选项"); } } } private static void register(ArrayList<User> list) { System.out.println("注册"); Scanner sc = new Scanner(System.in); String username; while (true) { System.out.println("请输入用户名"); username = sc.next(); //用户名必须是3-15位之间 boolean flag1 = checkUsername(username); if (!flag1) { System.out.println("当前格式不满足要求,请重新输入"); continue; } boolean flag2 = contains(list, username); if (flag2) { System.out.println("用户名" + username + "已存在"); continue; } else { System.out.println("用户名" + username + "可用"); break; } } String password; 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; } } //键盘输入身份证号码 String personId; while (true) { System.out.println("请输入身份证号码"); personId = sc.next(); boolean flag = checkPersonId(personId); if (flag) { System.out.println("身份证信息满足条件"); break; } else { System.out.println("身份证信息不满足条件"); continue; } } String phoneNumber; while (true) { System.out.println("请输入手机号码"); phoneNumber = sc.next(); boolean flag=checkNumber(phoneNumber); if (flag) { System.out.println("手机信息满足条件"); break; } else { System.out.println("手机信息不满足条件"); continue; } } User u=new User(username,password,personId,phoneNumber); list.add(u); printList(list); System.out.println("注册成功"); } 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.getPersonId()+","+user.getPhoneNumber()); } } private static boolean checkNumber(String phoneNumber) { if(phoneNumber.length()!=11){ System.out.println("手机号必须是18位数字"); return false; } if(phoneNumber.startsWith("0")){ System.out.println("手机号不能是0开头"); return false; } for (int i = 0; i < phoneNumber.length()-1; i++) { char c=phoneNumber.charAt(i); if(!(c>='0'&&c<='9')){ return false; } } return true; } private static void login(ArrayList<User> list){ Scanner sc=new Scanner(System.in); User useInfo; 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; } String rightCode=getCode(); System.out.println("请输入密码"); String password=sc.next(); useInfo=new User(username,password,null,null); //验证用户名和密码是否正确 boolean result=checkUserInfo(list,useInfo); if(result){ System.out.println("登录成功"); }else{ System.out.println("登录错误"); if(i==2){ System.out.println("多次登录失败 请联系管理员"); } } while (true){ System.out.println("请输入验证码"); System.out.println(rightCode); String code=sc.next(); if(code.equalsIgnoreCase(rightCode)){ System.out.println("验证码正确"); break; }else{ System.out.println("验证码错误"); continue; } } } } private static boolean checkUserInfo(ArrayList<User> list,User useInfo) { for (int i = 0; i < list.size(); i++) { User user=list.get(i); if(user.getUsername().equals(useInfo.getUsername())&&user.getPassword().equals(useInfo.getPassword())){ return true; } } return false; } private static void forgetPassWord(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("当前"+username+"不存在请重新注册"); } 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.getPhoneNumber().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("两次密码不一致,请重新输入"); continue; } else { System.out.println("两次密码一致,请继续输入"); break; } } user.setPassword(password); 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 checkUsername(String username){ 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; } private static boolean checkPersonId(String personId){ if(personId.length()!=18){ System.out.println("身份证号必须是18位数字"); return false; } if(personId.startsWith("0")){ System.out.println("身份证号不能是0开头"); return false; } for (int i = 0; i < personId.length()-1; i++) { char c=personId.charAt(i); if(!(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){ for (int i = 0; i <list.size() ; i++) { User user=list.get(i); String rightUsername=user.getUsername(); if(rightUsername.equals(username)){ return true; }else{ return false; } } 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)); } 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.append(c); } //随机数字添加到某位 int number=r.nextInt(10); sb.append(number); //数字可以再任意位置 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); } }