先自己做了一遍,再看网课时发现有挺多能优化的思路
学生类
1 public class Student { 2 private String name; 3 private int id; 4 private String password; 5 6 public Student(){ 7 } 8 public Student(int id,String name){ 9 this.id = id; 10 this.name = name; 11 } 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public int getId() { 22 return id; 23 } 24 25 public void setId(int id) { 26 this.id = id; 27 } 28 29 public String getPassword() { 30 return password; 31 } 32 33 public void setPassword(String password) { 34 this.password = password; 35 } 36 }
用户类
1 public class User { 2 private String name; 3 private String password; 4 private String phoneNum; 5 private String personId; 6 7 public User() { 8 } 9 10 public User(String name, String password, String phoneNum, String personId) { 11 this.name = name; 12 this.password = password; 13 this.phoneNum = phoneNum; 14 this.personId = personId; 15 } 16 17 public User(String name, String password) { 18 this.name = name; 19 this.password = password; 20 } 21 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public String getPassword() { 31 return password; 32 } 33 34 public void setPassword(String password) { 35 this.password = password; 36 } 37 38 public String getPhoneNum() { 39 return phoneNum; 40 } 41 42 public void setPhoneNum(String phoneNum) { 43 this.phoneNum = phoneNum; 44 } 45 46 public String getPersonId() { 47 return personId; 48 } 49 50 public void setPersonId(String personId) { 51 this.personId = personId; 52 } 53 }
学生系统
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class StudentSystem { 5 public static void startStudentSystem() { 6 ArrayList<Student> list = new ArrayList<>(); 7 Student temp = new Student(1, "a"); 8 Student temp1 = new Student(2, "b"); 9 list.add(temp); 10 list.add(temp1); 11 System.out.println("--------学生管理系统测试--------"); 12 System.out.println("1.添加学生"); 13 System.out.println("2.删除学生"); 14 System.out.println("3.修改学生"); 15 System.out.println("4.查询学生"); 16 System.out.println("5.退出系统"); 17 loop: 18 while (true) { 19 System.out.println("输入选择:"); 20 Scanner sc = new Scanner(System.in); 21 String chose = sc.next(); 22 switch (chose) { 23 case "1" -> addStudent(list); 24 case "2" -> deleteStudent(list); 25 case "3" -> updateStudent(list); 26 case "4" -> queryStudent(list); 27 case "5" -> { 28 System.out.println("退出系统"); 29 break loop; 30 // System.exit(0); 31 } 32 default -> System.out.println("没有这个选项"); 33 } 34 } 35 36 37 } 38 39 public static void addStudent(ArrayList<Student> list) { 40 System.out.println("----------------"); 41 System.out.println("添加学生id"); 42 Scanner sc = new Scanner(System.in); 43 Student s = new Student(); 44 int id; 45 loop2: 46 while (true) { 47 id = sc.nextInt(); 48 for (int i = 0; i < list.size(); i++) { 49 if (list.get(i).getId() == id) { 50 System.out.println("----------------"); 51 System.out.println("已有该学号,重新输入!"); 52 continue loop2; 53 } 54 } 55 break; 56 } 57 s.setId(id); 58 System.out.println("添加成功"); 59 System.out.println("----------------"); 60 System.out.println("添加学生姓名"); 61 String name = sc.next(); 62 s.setName(name); 63 list.add(s); 64 System.out.println("添加成功"); 65 } 66 67 public static void deleteStudent(ArrayList<Student> list) { 68 if (list.size() == 1) { 69 System.out.println("只有一组数据,无法删除"); 70 return; 71 } 72 System.out.println("输入要删除学生的id"); 73 Scanner sc = new Scanner(System.in); 74 int id = sc.nextInt(); 75 int i = getIndex(list, id); 76 if (i >= 0) { 77 list.remove(i); 78 System.out.println("id为:" + id + "的学生删除成功,姓名为" + list.get(i).getName()); 79 } else 80 System.out.println("不存在该id,请确认后重新输入"); 81 } 82 83 public static void updateStudent(ArrayList<Student> list) { 84 System.out.println("输入修改学生的ID"); 85 Scanner sc = new Scanner(System.in); 86 int id = sc.nextInt(); 87 int newId; 88 int temp = getIndex(list, id); 89 //返回菜单 90 if (temp == -1) { 91 System.out.println("该id不存在,请查询后重新输入"); 92 return; 93 } 94 System.out.println("输入修改后的Id:"); 95 //返回修改id 96 while (true) { 97 newId = sc.nextInt(); 98 temp = getIndex(list, newId); 99 if (temp != -1) { 100 System.out.println("该id已存在,请重新输入"); 101 continue; 102 } 103 break; 104 } 105 list.get(id).setId(newId); 106 System.out.println(newId); 107 System.out.println("输入修改后的姓名:"); 108 String newName = sc.next(); 109 list.get(id).setName(newName); 110 System.out.println("修改成功"); 111 112 } 113 114 public static void queryStudent(ArrayList<Student> list) { 115 if (list.size() == 0) { 116 System.out.println("当前无学生信息,请添加后再查询"); 117 return; 118 } 119 System.out.println("id\t姓名\t"); 120 for (int i = 0; i < list.size(); i++) { 121 Student stu = list.get(i); 122 System.out.println(stu.getId() + "\t" + stu.getName()); 123 } 124 } 125 126 //在列表中查询有无id信息,有的话返回位置,无的话返回-1 127 public static int getIndex(ArrayList<Student> list, int id) { 128 for (int i = 0; i < list.size(); i++) { 129 Student stu = list.get(i); 130 int stuId = stu.getId(); 131 if (stuId == id) { 132 return i; 133 } 134 } 135 return -1; 136 } 137 }
用户系统
1 import java.util.ArrayList; 2 import java.util.Random; 3 import java.util.Scanner; 4 5 //先判断格式再判断唯一 6 // 7 public class UserSystem { 8 public static void main(String[] args) { 9 getCode(); 10 Scanner sc = new Scanner(System.in); 11 ArrayList<User> list = new ArrayList<>(); 12 User u1 = new User("ccmd3", "123a", "13805655843", "340123200011143352"); 13 list.add(u1); 14 System.out.println("----------用户系统---------"); 15 System.out.println("1.登录"); 16 System.out.println("2.注册"); 17 System.out.println("3.忘记密码"); 18 System.out.println("4.退出系统"); 19 while (true) { 20 System.out.println("输入选项"); 21 //测试用户 22 for (int i = 0; i < list.size(); i++) { 23 System.out.println(list.get(i).getName() + ", " + list.get(i).getPassword() + ", " + list.get(i).getPhoneNum() + ", " + list.get(i).getPersonId()); 24 } 25 String chose = sc.next(); 26 switch (chose) { 27 case "1" -> logIn(list); 28 case "2" -> creatUser(list); 29 case "3" -> findPassword(list); 30 case "4" -> { 31 System.out.println("再见"); 32 System.exit(0); 33 } 34 default -> System.out.println(); 35 36 } 37 } 38 } 39 40 //------------------------------------------------ 41 //登录系统 42 //------------------------------------------------ 43 public static void logIn(ArrayList<User> list) { 44 //判断输入的用户id是否在user列表里 45 while (true) { 46 System.out.println("输入用户名"); 47 Scanner sc = new Scanner(System.in); 48 String userId = sc.next(); 49 int index = getIndex(list, userId); 50 if (index == -1) { 51 System.out.println("所输用户未注册"); 52 continue; 53 } 54 //检测密码,三次机会 55 // while (count >= 0) { 56 // System.out.println("输入密码"); 57 // String checkCode = sc.next(); 58 // if (list.get(index).getPassword().equals(checkCode)) { 59 // System.out.println("密码正确"); 60 // } else { 61 // System.out.println("密码错误,还有" + count + "次机会"); 62 // count--; 63 // } 64 // } 65 System.out.println("输入密码"); 66 67 while (true) { 68 String password = sc.next(); 69 User userInfo = new User(userId, password); 70 boolean result = checkUserInfo(list, userInfo); 71 int count = 2; 72 //输入验证码,如果错误则重新输入,三次机会 73 while (count>=0) { 74 String code = getCode(); 75 System.out.println("输入验证码" + code); 76 String checkCode = sc.next(); 77 if (code.equalsIgnoreCase(checkCode)) { 78 System.out.println("验证码成功"); 79 break; 80 } else { 81 System.out.println("验证码错误,请重新输入,还有" + count + "次机会"); 82 count--; 83 } 84 } 85 if(count<0){ 86 System.out.println("次数用尽"); 87 return; 88 } 89 if(!result){ 90 System.out.println("密码错误,请重试"); 91 }else { 92 System.out.println("登录成功"); 93 StudentSystem ss = new StudentSystem(); 94 ss.startStudentSystem(); 95 break; 96 } 97 } 98 return; 99 } 100 } 101 102 103 //账号与密码校对系统 104 private static boolean checkUserInfo(ArrayList<User> list, User userInfo) { 105 for (int i = 0; i < list.size(); i++) { 106 User user = list.get(i); 107 if (user.getName().equals(userInfo.getName()) && user.getPassword().equals(userInfo.getPassword())) { 108 return true; 109 } 110 } 111 return false; 112 } 113 114 115 //------------------------------------------------ 116 //注册系统 117 //------------------------------------------------ 118 public static void creatUser(ArrayList<User> list) { 119 Scanner sc = new Scanner(System.in); 120 int countNum = 0, countChar = 0; 121 System.out.println("输入注册名"); 122 User newUser = new User(); 123 String newName = null; 124 while (true) { 125 newName = sc.next(); 126 if (newName.length() < 3 || newName.length() > 15) { 127 System.out.println("用户名长度不合法"); 128 return; 129 } 130 //检测字符的合法性 131 for (int i = 0; i < newName.length(); i++) { 132 char temp = newName.charAt(i); 133 //如果元素不在字母或者数字中 134 if ((temp > 'a' && temp < 'z') || temp > 'A' && temp < 'Z') { 135 countChar++; 136 } else if (temp > '0' && temp < '9') { 137 countNum++; 138 } else { 139 System.out.println("输入的字符不合法"); 140 return; 141 } 142 143 } 144 if (getIndex(list, newName) != -1) { 145 System.out.println("该用户名已被注册"); 146 return; 147 } 148 if (countNum == 0 || countChar == 0) { 149 System.out.println("不能为纯数字或字母"); 150 return; 151 } 152 newUser.setName(newName); 153 break; 154 } 155 156 //输入密码 157 while (true) { 158 System.out.println("请输入密码"); 159 String password = sc.next(); 160 System.out.println("再输入一次密码"); 161 String password2 = sc.next(); 162 if (password.equals(password2)) { 163 newUser.setPassword(password); 164 break; 165 } else 166 System.out.println("两次输入不一致,请重试"); 167 } 168 System.out.println("输入身份证号码"); 169 //输入身份证号码 170 String newId = sc.next(); 171 if (newId.length() != 18) { 172 System.out.println("身份证长度不合法"); 173 return; 174 } 175 if (newId.charAt(0) == '0') { 176 System.out.println("身份证首位不能为0"); 177 return; 178 } 179 //检测前17位合法性 180 for (int i = 0; i < newId.length() - 1; i++) { 181 if (!(newId.charAt(i) >= '0' && newId.charAt(i) <= '9')) { 182 System.out.println("身份证前17位必须为数字"); 183 return; 184 } 185 } 186 char temp = newId.charAt(newId.length() - 1); 187 if (!((temp == 'x') || (temp == 'X') || ((temp >= '0') && (temp <= '9')))) { 188 System.out.println("身份证尾号只能为数字或x"); 189 return; 190 } 191 newUser.setPersonId(newId); 192 //输入手机号 193 System.out.println("请输入手机号码"); 194 String newPhone = sc.next(); 195 if (newPhone.length() != 11) { 196 System.out.println("手机号长度不合法"); 197 return; 198 } 199 if (newPhone.charAt(0) != '1') { 200 System.out.println("手机号首位错误"); 201 return; 202 } 203 for (int i = 0; i < newPhone.length(); i++) { 204 if (!(newPhone.charAt(i) >= '0') && (newPhone.charAt(i) <= '9')) { 205 System.out.println("手机号输入格式不合法"); 206 return; 207 } 208 } 209 newUser.setPhoneNum(newPhone); 210 list.add(newUser); 211 System.out.println("创建成功"); 212 } 213 214 //------------------------------------------------ 215 //忘记密码系统 216 // ------------------------------------------------ 217 public static void findPassword(ArrayList<User> list) { 218 System.out.println("请输入用户名"); 219 Scanner sc = new Scanner(System.in); 220 String userName = sc.next(); 221 int temp = getIndex(list, userName); 222 if (temp == -1) { 223 System.out.println("该用户名未注册"); 224 return; 225 } 226 System.out.println("请输入身份证号码"); 227 String userId = sc.next(); 228 System.out.println("请输入手机号码"); 229 String userPhone = sc.next(); 230 if (!(userId.equals(list.get(temp).getPersonId()) && userPhone.equals(list.get(temp).getPhoneNum()))) { 231 System.out.println("账号信息不匹配,修改失败"); 232 return; 233 } 234 System.out.println("输入密码"); 235 String userPassword = sc.next(); 236 list.get(temp).setPassword(userPassword); 237 System.out.println("修改成功"); 238 } 239 240 //------------------------------------------------ 241 //返回id在user列表中的位置,若为空返回-1 242 //------------------------------------------------ 243 public static int getIndex(ArrayList<User> list, String id) { 244 for (int i = 0; i < list.size(); i++) { 245 User temp = list.get(i); 246 if (temp.getName().equals(id)) 247 return i; 248 } 249 return -1; 250 } 251 252 //------------------------------------------------ 253 //生成一个4字母1数字的随机验证码 254 // ------------------------------------------------ 255 public static String getCode1() { 256 Random r = new Random(); 257 String code = ""; 258 char c; 259 //生成1个随机数字 260 int num = r.nextInt(10); 261 code += num; 262 //生成4个随机字母 263 for (int i = 0; i < 4; i++) { 264 int tempChar = r.nextInt(52) + 1; 265 if (tempChar <= 26) { 266 c = (char) (tempChar + 96); 267 } else { 268 c = (char) (tempChar + 38); 269 } 270 code += c; 271 } 272 // 检测语句 273 // System.out.println("打乱前为" + code); 274 //将字符串转为字符 275 char[] tempCode = new char[5]; 276 for (int i = 0; i < code.length(); i++) { 277 tempCode[i] = code.charAt(i); 278 } 279 //打乱顺序 280 for (int i = 0; i < code.length(); i++) { 281 int newC = r.nextInt(5); 282 char temp = tempCode[i]; 283 tempCode[i] = tempCode[newC]; 284 tempCode[newC] = temp; 285 } 286 //将打乱后的数组赋值给新的字符串并输出 287 String newCode = ""; 288 for (int i = 0; i < code.length(); i++) { 289 newCode += tempCode[i]; 290 } 291 return newCode; 292 } 293 294 public static String getCode() { 295 ArrayList<Character> list = new ArrayList<>(); 296 //创建一个包含所有字符的集合 297 for (int i = 0; i < 26; i++) { 298 list.add((char) ('a' + i)); 299 list.add((char) ('A' + i)); 300 } 301 StringBuilder sb = new StringBuilder(); 302 Random r = new Random(); 303 for (int i = 0; i < 4; i++) { 304 int index = r.nextInt(list.size()); 305 char c = list.get(index); 306 sb.append(c); 307 } 308 int c = r.nextInt(10); 309 sb.append(c); 310 char[] arr = sb.toString().toCharArray(); 311 //将数字进行交换 312 int randomIndex = r.nextInt(arr.length); 313 char temp = arr[randomIndex]; 314 arr[randomIndex] = arr[arr.length - 1]; 315 arr[arr.length - 1] = temp; 316 return new String(arr); 317 } 318 319 320 }