1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
| 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<>(); loop: while (true) { System.out.println("—————————————————欢迎登使用管理系统————————————————"); System.out.print(" 1.登录 "); System.out.print(" 2.注册 "); System.out.print(" 3.忘记密码 "); System.out.println(" 4.退出"); System.out.print("请输入你的选择:"); 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("退出系统"); break loop; } default: System.out.println("输入无效,请重新输入"); } } }
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 + "未注册,请先注册!"); return; } System.out.println("请输入身份证号"); String personID = sc.next(); System.out.println("请输入手机号"); String phoneNumber = sc.next(); int index = findUserInfo(list, username); User user = list.get(index); if (!(personID.equalsIgnoreCase(user.getPersonId()) && phoneNumber.equals(user.getPhoneNumber()))) { 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("两次密码不一致,请重新输入"); } else { System.out.println("两次密码一致,请继续输入其他信息"); break; } } user.setPassword(password); System.out.println("密码修改成功"); }
private static int findUserInfo(ArrayList<User> list, String username) { for (int i = 0; i < list.size(); i++) { User u = list.get(i); if (username.equals(u.getUserName())) { return i; } } return -1; }
private static void register(ArrayList<User> list) { System.out.println("注册"); Scanner sc = new Scanner(System.in); String userName; String password; String personID; String phoneNumber; while (true) { System.out.println("请输入用户名:"); userName = sc.next(); boolean flag = checkName(userName); if (!flag) { System.out.println("用户名" + userName + "格式不正确,请重新输入"); continue; } boolean b = contains(list, userName); if (b) { 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("两次密码不一致,请重新输入"); } else { System.out.println("两次密码一致,请继续输入其他信息"); break; } }
while (true) { System.out.println("请输入身份证信息"); personID = sc.next(); boolean pID = checkPersonId(personID); if (pID) { System.out.println("身份证号码满足要求,请继续输入"); break;
} else { System.out.println("身份证信息不满足要求,请重新输入"); } } while (true) { System.out.println("请输入手机号码"); phoneNumber = sc.next(); boolean phone = checkPhoneNumber(phoneNumber); if (phone) { System.out.println("手机号正确,请继续输入"); break; } else { System.out.println("手机号格式不正确,请重新输入"); } } User user = new User(userName, password, personID, phoneNumber); list.add(user); System.out.println("注册成功"); printList(list); }
private static void printList(ArrayList<User> list) { for (int i = 0; i < list.size(); i++) { User u = list.get(i); System.out.println("用户名:" + u.getUserName() + " 密码:" + u.getPassword() + " 身份证:" + u.getPersonId() + " 手机号:" + u.getPhoneNumber()); } }
private static boolean checkPhoneNumber(String phoneNumber) { if (phoneNumber.length() != 11) { return false; } if (phoneNumber.startsWith("0")) { return false; } for (int i = 0; i < phoneNumber.length(); i++) { char ph = phoneNumber.charAt(i); if (!(ph >= '0' && ph <= '9')) { return false; } } return true; }
private static boolean checkPersonId(String personID) { if (personID.length() != 18) { return false; } if (personID.startsWith("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); if (user.getUserName().equals(userName)) { return true; } } return false; }
private static boolean checkName(String username) { int length = username.length(); if (length < 3 || length > 15) { return false; } for (int i = 0; i < username.length(); i++) { char ch = username.charAt(i); if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) { 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 login(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) { System.out.println("请输入验证码"); String code = getCode(); System.out.println("验证码为: " + code); String usercode = sc.next(); if (code.equalsIgnoreCase(usercode)) { System.out.println("验证码正确"); break; } else { System.out.println("验证码错误,请重新输入"); } } User userInfo = new User(username, password, null, null); boolean info = checkUserInfo(list, userInfo); if (info) { System.out.println("登录成功,开始使用"); StudentManagement.StartMenu(); break; } else {
if (i == 2) { System.out.println("账户为:" + username + "已被锁定,请联系管理员π!"); } else { System.out.println("还剩" + (2 - i) + "次机会"); } } } }
private static boolean checkUserInfo(ArrayList<User> list, User userInfo) { 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 String getCode() { ArrayList<Character> list = new ArrayList<>(); for (int i = 0; i < 26; i++) { list.add((char) (i + 'a')); list.add((char) (i + 'A')); } Random r = new Random(); StringBuilder sb = new StringBuilder(); 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(); for (int i = 0; i < arr.length; i++) { int index = r.nextInt(arr.length); char temp = arr[index]; arr[index] = arr[arr.length - 1]; arr[arr.length - 1] = temp; } return new String(arr); } }
|