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
| public 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("用户名不存在,请输入正确用户名"); return; } System.out.println("请输入密码:"); String password = sc.next(); while (true) { String code = getcode(); System.out.println("验证码为:" + code); System.out.println("请输入验证码:"); String code1 = sc.next(); if (!code.equalsIgnoreCase(code1)) { System.out.println("验证码错误,请重新输入"); continue; } else { break; } } User u = new User(username, password, null, null); boolean flag1 = checkcode(list, u); if (!flag1) { if (i == 2) { System.out.println("三次密码错误,账号被锁定"); return; } else { System.out.println("密码错误,请重新输入你还有" + (2 - i) + "次机会"); } } else { System.out.println("登录成功"); StudentTest_important ss=new StudentTest_important(); ss.starttest(); break; } }
}
public static void register(ArrayList<User> list) { Scanner sc = new Scanner(System.in); String username; while (true) { System.out.println("请输入用户名:"); username = sc.next(); boolean flag = contains(list, username); if (flag) { System.out.println("用户名已存在,请重新输入"); continue; } else { boolean flag1 = checkname(username); if (flag1) { break; } else { System.out.println("用户名不符合要求,请重新输入"); continue; } } } String password; while (true) { System.out.println("请输入密码:"); password = sc.next(); System.out.println("请输入确认密码:"); String password1 = sc.next(); if (password.equals(password1)) { break; } else { System.out.println("两次密码不一致,请重新输入"); continue; } } String id; while (true) { System.out.println("请输入身份证号:"); id = sc.next(); boolean flag2 = checkid(id); if (flag2) { break; } else { System.out.println("身份证号不符合要求,请重新输入"); continue; } } String phoneNumber; while (true) { System.out.println("请输入手机号:"); phoneNumber = sc.next(); boolean flag3 = checksecnumber(phoneNumber); if (flag3) { break; } else { System.out.println("手机号不符合要求,请重新输入"); continue; } } User u = new User(username, password, id, phoneNumber); list.add(u); System.out.println("注册成功"); }
public static void forgetPassword(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("用户名不存在,请输入正确用户名"); return; } System.out.println("请输入身份证号:"); String id = sc.next(); System.out.println("请输入手机号:"); String phoneNumber = sc.next(); int index = getIndex(list, username); User u1 = list.get(index); User u = new User(username, null, id, phoneNumber); String password; while (true) { if (u1.getId().equals(u.getId()) && u1.getPhoneNumber().equals(u.getPhoneNumber())) { System.out.println("请输入新密码:"); password = sc.next(); System.out.println("请输入确认密码:"); String password1 = sc.next(); if (password.equals(password1)) { System.out.println("密码修改成功"); break; } else { System.out.println("两次密码不一致,请重新输入"); continue; } } else { System.out.println("身份证号或手机号错误,请重新输入"); return; } }u1.setPassword(password);
}
public static int getIndex(ArrayList<User> list, String username) { for (int i = 0; i < list.size(); i++) { User u1 = list.get(i); if (u1.getUsername().equals(username)) { return i; } } return -1; }
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)); } for (int i = 0; i < 10; i++) { list.add((char) ('0' + i)); } StringBuilder sb = new StringBuilder(); Random random = new Random(); for (int i = 0; i < 5; i++) { int index; if (i == 4) { index = random.nextInt(10) + 52; sb.append(list.get(index)); } else { index = random.nextInt(52); sb.append(list.get(index)); } } for (int i = 0; i < sb.length(); i++) { int index = random.nextInt(5); char temp = sb.charAt(i); sb.setCharAt(i, sb.charAt(index)); sb.setCharAt(index, temp); } return sb.toString(); }
public static boolean checkcode(ArrayList<User> list, User u) { for (int i = 0; i < list.size(); i++) { User u1 = list.get(i); if (u1.getUsername().equals(u.getUsername()) && u1.getPassword().equals(u.getPassword())) { return true; } } return false; }
public static boolean contains(ArrayList<User> list, String username) { String username1; for (int i = 0; i < list.size(); i++) { username1 = list.get(i).getUsername(); if (username1.equals(username)) { return true; } } return false; }
public static boolean checkname(String username) { if (username.length() < 3 || username.length() > 13) { 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; } } for (int i = 0; i < username.length(); i++) { char c = username.charAt(i); if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { return true; } } return false; }
public static boolean checkid(String id) { for (int i = 0; i < id.length() - 1; i++) { char c = id.charAt(i); if (!(c >= '0' && c <= '9')) { return false; } } char c = id.charAt(id.length() - 1); if ((c >= '0' && c <= '9') || (c == 'x' || c == 'X')) { return true; } return false; }
public static boolean checksecnumber(String phoneNumber) { if (phoneNumber.length() != 11) { return false; } 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; } }
|