|--需求说明
|--实现思路
1、使用User对象数组创建信息,使用map储存user.name和对应的user
|--代码内容
1 package cn.work.test; 2 3 import java.io.Serializable; 4 import java.security.PrivateKey; 5 import java.util.PrimitiveIterator; 6 7 /** 8 * @auther::9527 9 * @Description: 用户类 10 * @program: shi_yong 11 * @create: 2019-08-01 16:58 12 */ 13 public class User implements Serializable { 14 private String name; //会员名字 15 private String phoneNum; //电话号码 16 private String cardId; //卡号 17 private int score; //积分 18 private String encrypted; //密保 19 private String registerNum; //开卡日期 20 private String passWord; //密码 21 22 public User() { 23 } 24 25 //带参构造方法 26 27 28 public User(String name, String phoneNum, String cardId, int score, String encrypted, String registerNum, String passWord) { 29 this.name = name; 30 this.phoneNum = phoneNum; 31 this.cardId = cardId; 32 this.score = score; 33 this.encrypted = encrypted; 34 this.registerNum = registerNum; 35 this.passWord = passWord; 36 } 37 38 public String getName() { 39 return name; 40 } 41 42 public void setName(String name) { 43 this.name = name; 44 } 45 46 public String getPhoneNum() { 47 return phoneNum; 48 } 49 50 public void setPhoneNum(String phoneNum) { 51 this.phoneNum = phoneNum; 52 } 53 54 public String getCardId() { 55 return cardId; 56 } 57 58 public void setCardId(String cardId) { 59 this.cardId = cardId; 60 } 61 62 public int getScore() { 63 return score; 64 } 65 66 public void setScore(int score) { 67 this.score = score; 68 } 69 70 public String getEncrypted() { 71 return encrypted; 72 } 73 74 public void setEncrypted(String encrypted) { 75 this.encrypted = encrypted; 76 } 77 78 public String getRegisterNum() { 79 return registerNum; 80 } 81 82 public void setRegisterNum(String registerNum) { 83 this.registerNum = registerNum; 84 } 85 86 public String getPassWord() { 87 return passWord; 88 } 89 90 public void setPassWord(String passWord) { 91 this.passWord = passWord; 92 } 93 94 @Override 95 public String toString() { 96 return "User{" + 97 "\t姓名\t" + name + 98 "\t电话号码\t" + phoneNum + 99 "\t会员卡号\t" + cardId + 100 "\t积分\t" + score + 101 "\t密保信息\t" + encrypted + 102 "\t开卡日期\t" + registerNum + 103 "\t密码\t" + passWord + 104 '}'; 105 } 106 }
1 package cn.work.test; 2 3 4 import java.text.SimpleDateFormat; 5 import java.util.*; 6 import java.util.regex.*; 7 8 /** 9 * @auther::9527 10 * @Description: 方法类 11 * @program: shi_yong 12 * @create: 2019-08-01 16:57 13 */ 14 public class Meth { 15 Scanner scanner = new Scanner(System.in); 16 HashMap<String, User> map = new HashMap(); 17 User us1 = new User("a9527", "15986341689", "88888888", 100, "I`m ok", "2018-08-09", "a9527"); 18 User user = null; 19 20 //弄一个集合,用于将数据整理到一起 21 public HashMap<String, User> getMap(User user) { 22 map.put(user.getName(), user); 23 return map; 24 } 25 26 public HashMap<String, User> getMap() { 27 map.put(us1.getName(), us1); 28 return map; 29 } 30 31 32 //用于判断是否包含特殊符号的规则 33 private static final String REGEX1 = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t"; 34 //用于判断是否数字的规则 35 private static final String REGEX2 = "^-?\\d+(\\.\\d+)?$"; 36 37 38 //一级用户菜单 39 public static void ui_first() { 40 System.out.println("-------------------欢迎进入超市会员管理系统---------------------"); 41 System.out.println("1.登录 2.开卡 3.退出"); 42 System.out.println("---------------------------------------------------------------"); 43 System.out.print("请选择:"); 44 } 45 46 //二级用户菜单 47 public static void ui_second() { 48 System.out.println("********************超市会员管理系统二级目录********************"); 49 System.out.println("1.积分累计 2.积分兑换 3.积分查询 4.修改密码 5.返回一级菜单"); 50 System.out.println("***************************************************************"); 51 System.out.print("请选择:"); 52 } 53 54 //开卡功能 55 public void openCard() { 56 //用户名循环跳转条件 57 boolean isName = true; 58 String name = ""; 59 do { 60 //用户名不能以数字开头,不能有特殊符号,用户名长度必须在3到6位之间 61 System.out.println("请输入你的姓名,用户名不能以数字开头,\n不能有特殊符号,用户名长度必须在3到6位之间"); 62 63 name = scanner.next(); 64 if (name.length() < 3 || name.length() > 6) { 65 System.out.println("用户名的长度必须在3到6位之间(含)\n"); 66 } else if (isFirstNum(name)) { 67 System.out.println("用户名不能以数字开头\n"); 68 } else if (hasSpecificSymbol(name)) { 69 System.out.println("用户名不能包含特殊符号\n"); 70 } else { 71 isName = false; 72 } 73 } while (isName); 74 User user = new User(); 75 user.setName(name); 76 77 System.out.println("请输入电话号码"); 78 user.setPhoneNum(scanner.next()); 79 boolean isPassWord = true; 80 String passWord = ""; 81 do { 82 System.out.println("请输入密码"); 83 passWord = scanner.next(); 84 if (passWord.length() < 6 || passWord.length() > 10) { 85 System.out.println("密码长度必须在6-10位,请重新设置\n"); 86 } 87 System.out.println("请再次输入密码"); 88 String passWord2 = scanner.next(); 89 if (!passWord.equals(passWord2)) { 90 System.out.println("两次输入的密码不一致,请重新输入"); 91 } else { 92 isPassWord = false; 93 } 94 System.out.println("密码强度分析: " + strongPW(passWord)); 95 } while (isPassWord); 96 user.setPassWord(passWord); 97 System.out.println("密码设置成功"); 98 //开卡的验证码 99 yanZhen(); 100 // 设置密保, 101 System.out.println("请随意输入一组字符,作为密保"); 102 String encrypted = scanner.next(); 103 user.setEncrypted(encrypted); 104 //设置初始积分 105 user.setScore(100); 106 //设置卡号 107 user.setCardId(cardId()); 108 //设置注册日期 109 user.setRegisterNum(registerNum()); 110 System.out.println("开卡成功,会员卡的初始积分为100分"); 111 System.out.println("你的卡号是: " + cardId()); 112 System.out.println("你的开卡日期是:" + registerNum()); 113 // System.out.println(user.toString()); //测试会员信息是否完整,已注销 114 115 getMap(user); 116 } 117 118 //一个判断字符第一位是否是数字的方法 119 public static boolean isFirstNum(String string) { 120 Pattern pattern = Pattern.compile(REGEX2); 121 if (pattern.matcher(String.valueOf(string.charAt(0))).matches()) { 122 return true; 123 } else { 124 return false; 125 } 126 } 127 128 //使用正则表达式,判断字符串是否包含特殊符号 129 public static boolean hasSpecificSymbol(String string) { 130 Pattern k = Pattern.compile(REGEX1); 131 Matcher m = k.matcher(string); 132 if (m.find()) { 133 return true; 134 } else { 135 return false; 136 } 137 } 138 139 //密码强度量化 140 public String strongPW(String string) { 141 //将密码拆分成char数组,遍历这个数组,如果有数字则+1分,有英文小写字母+1分,有英文大写字母+1分,有特殊符号+1分 142 int total = 0; //给密码强度量化打分 143 int hasNum = 0; //有数字 144 int hasUpLetter = 0; //有大写英文字母 145 int hasLowLetter = 0; //有小写英文字母 146 int hasSpecial = 0; //有特殊符号 147 //设置密码强度的变量名 148 String strong = ""; 149 //将传入的密码拆分成char数组 150 char[] c = string.toCharArray(); 151 //遍历这个数组 152 for (int i = 0; i < c.length; i++) { 153 //数字0-9 154 if ((int) (c[i]) >= 48 && (int) (c[i]) <= 57) { 155 hasNum = 1; //有数字则+1 156 //小写英文字母 157 } else if ((int) (c[i]) >= 97 && (int) (c[i]) <= 122) { 158 hasLowLetter = 1; //有英文小写字母则+1 159 //大写英文字母 160 } else if ((int) (c[i]) >= 65 && (int) (c[i]) <= 90) { 161 hasUpLetter = 1; //有英文大写字母则+1 162 //键盘里面的所有特殊符号 163 } else { 164 hasSpecial = 1; 165 } 166 } 167 total = hasLowLetter + hasNum + hasSpecial + hasUpLetter; 168 switch (total) { 169 case 1: 170 strong = "密码强度最弱"; 171 break; 172 case 2: 173 strong = "密码强度较弱"; 174 break; 175 case 3: 176 strong = "密码强度一般"; 177 break; 178 default: 179 strong = "密码强度很强"; 180 break; 181 } 182 return strong; 183 } 184 185 //随机一个卡号的方法 186 public String cardId() { 187 Random random = new Random(); 188 String cardId = String.valueOf(random.nextInt(99999999)); 189 return cardId; 190 } 191 192 //生成开卡日期的方法 String registerNum 193 public String registerNum() { 194 Date date = new Date(); 195 SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd"); 196 String registerNum = sim.format(date); 197 return registerNum; 198 } 199 200 //验证码生成器 201 public String verificationCode() { 202 //生成6位验证码 203 String code = ""; 204 for (int i = 0; i < 6; i++) { 205 //随机出一个布尔值 206 boolean boo = ((int) (Math.random() * 2)) == 0 ? true : false; 207 if (boo) { 208 //如果布尔值为真,就随机出一个数字 209 code += (int) (Math.random() * 10); 210 } else { 211 //如果布尔值为假,就再随机一个char表的起点 212 int temp = ((int) (Math.random() * 2)) == 0 ? 65 : 97; 213 //如果随机出来的起点是65就是大写字母A,如果随机出来的起点是97就是小写字母的起点 214 //26是英文字母的总数 215 char ch = (char) ((int) (Math.random() * 26) + temp); 216 code += String.valueOf(ch); 217 } 218 } 219 return code; 220 } 221 222 223 //登录 224 public User login(String name) { 225 226 boolean islogin = true; 227 exit: 228 do { 229 230 //如果账户存在,但是密码输错5次,就会自动注销账户 231 if (getMap().containsKey(name)) { 232 int i = 0; 233 for (; i < 5; i++) { 234 String pwd = ""; 235 System.out.println("若忘记密码,可以使用密保可以按“1”找回密码,按其他任意键进入密码输入界面"); 236 String input = scanner.next(); 237 if ("1".equals(input)) { 238 System.out.println("请输入密保"); 239 String answer = scanner.next(); 240 if (answer.equals(map.get(name).getEncrypted())) { 241 find(name, map.get(name).getEncrypted()); 242 } else { 243 System.out.println("密保错误"); 244 } 245 } else { 246 System.out.println("你选择了不使用密保"); 247 } 248 System.out.println("请输入帐号密码"); 249 pwd = scanner.next(); 250 251 252 if (pwd.equals(getMap().get(name).getPassWord())) { 253 user = getMap().get(name); 254 islogin = false; 255 i = 6; 256 } else { 257 System.out.println("密码输入错误"); 258 } 259 } 260 if (i == 5) { 261 System.out.println("密码输错5次,冻结当前账户1分钟,冻结期间,帐号处于注销状态"); 262 User us = map.get(name); 263 map.remove(name); 264 if (((int) (System.currentTimeMillis() / 1000)) > 60) { 265 map.put(name, us); 266 } 267 } 268 269 } else { 270 System.out.println("帐号不存在,请重新输入"); 271 break exit; 272 } 273 274 } while (islogin); 275 return user; 276 } 277 278 //登录之后的积分累计 279 public void addScore(String string) { 280 System.out.println("请输入你消费的金额,消费一元累计1分,不足一元不做累计"); 281 int pay = 0; 282 while (!scanner.hasNextInt()) { 283 System.out.println("你输入的不是一个正整数,请重新输入"); 284 String strin = scanner.next(); 285 } 286 pay = scanner.nextInt(); 287 int score = map.get(string).getScore(); 288 map.get(string).setScore(score + pay); 289 System.out.println("积分累计成功,现在的积分是:" + map.get(string).getScore()); 290 } 291 292 //1.积分累计 2.积分兑换 3.积分查询 4.修改密码 5.退出登录 293 294 //登录之后的积分兑换 295 public void exchange(String name) { 296 System.out.println("1积分兑换0.05元,请问你要兑换多少积分"); 297 int change = 0; 298 while (!scanner.hasNextInt()) { 299 System.out.println("你输入的不是一个正整数,请重新输入"); 300 String string = scanner.next(); 301 } 302 change = scanner.nextInt(); 303 int momeny = (int) (change * 0.05); 304 int score = map.get(name).getScore(); 305 map.get(name).setScore(score - momeny); 306 System.out.println("积分兑换成功,你一共兑换" + momeny + "元"); 307 System.out.println("现有积分为:" + map.get(name).getScore()); 308 } 309 310 //积分查询 311 public void inquire(String name) { 312 System.out.println("现有积分为:" + map.get(name).getScore()); 313 } 314 315 //修改密码 316 public void amend(String name) { 317 boolean isAmend = true; 318 do { 319 System.out.println("请输入旧密码"); 320 String pwd = scanner.next(); 321 exit: 322 if (map.get(name).getPassWord().equals(pwd)) { 323 System.out.println("请输入新的密码"); 324 String npwd1 = scanner.next(); 325 boolean amend = true; 326 do { 327 if (pwd.equals(npwd1)) { 328 System.out.println("你的新密码就是旧密码,密码替换失败"); 329 break exit; 330 } else { 331 System.out.println("请再次确认新密码"); 332 String npwd2 = scanner.next(); 333 if (npwd1.equals(npwd2)) { 334 yanZhen(); 335 map.get(name).setPassWord(npwd2); 336 amend = false; 337 isAmend = false; 338 } 339 } 340 } while (amend); 341 } else { 342 System.out.println("旧密码输入错误,请重新输入"); 343 break; 344 } 345 346 } while (isAmend); 347 } 348 349 //验证码生成器 350 public void yanZhen() { 351 boolean isYanZhen = true; 352 do { 353 System.out.println("请按提示输入验证码,输入不分大小写"); 354 String verificationCode = verificationCode(); 355 System.out.println(verificationCode); 356 if (scanner.next().equalsIgnoreCase(verificationCode)) { 357 isYanZhen = false; 358 } 359 } while (isYanZhen); 360 } 361 362 //通过密保找回密码 363 public void find(String name, String encrypted) { 364 if (map.get(name).getEncrypted().equals(encrypted)) { 365 System.out.println("你的密码是:" + map.get(name).getPassWord()); 366 } 367 } 368 369 }
1 package cn.work.test; 2 3 /** 4 * @auther::9527 5 * @Description: 程序入口 6 * @program: shi_yong 7 * @create: 2019-08-01 17:50 8 */ 9 10 11 public class Main { 12 public static void main(String[] args) { 13 Frame frame = new Frame(); 14 frame.start(); 15 } 16 }
1 package cn.work.test; 2 3 import java.util.Scanner; 4 5 /** 6 * @auther::9527 7 * @Description: 程序框架 8 * @program: shi_yong 9 * @create: 2019-08-01 17:20 10 */ 11 public class Frame { 12 Scanner scanner = new Scanner(System.in); 13 User user = null; 14 boolean jump = true; 15 16 //程序框架 17 public void start() { 18 Meth meth = new Meth(); 19 exit: 20 do { 21 //一级菜单 1.登录 2.开卡 3.退出 22 Meth.ui_first(); 23 String answer = scanner.next(); 24 boolean second = true; 25 boolean third = true; 26 27 switch (answer) { 28 case "1": 29 //登录 30 do { 31 System.out.println("请输入你的用户名"); 32 String name = scanner.next(); 33 user = meth.login(name); 34 System.out.println("登录成功,你的会员号码是:" + user.getCardId() + 35 "积分是:" + user.getScore()); 36 do { 37 Meth.ui_second(); 38 System.out.println("请选择"); 39 String choose = scanner.next(); 40 switch (choose) { 41 case "1": 42 //积分累计 43 meth.addScore(name); 44 System.out.println(""); 45 break; 46 case "2": 47 //积分兑换 48 meth.exchange(name); 49 break; 50 case "3": 51 //积分查询, 52 meth.inquire(name); 53 break; 54 case "4": 55 //修改密码, 56 meth.amend(name); 57 second = false; 58 third = false; 59 break ; 60 case "5": 61 //退出登录 62 second = false; 63 third = false; 64 break ; 65 66 default: 67 System.out.println("选择不合法,请重新选择"); 68 break; 69 } 70 71 } while (third); 72 73 } while (second); 74 break ; 75 76 case "2": 77 meth.openCard(); 78 // System.out.println(meth.map.keySet()); //测试是否传参成功,已注释掉 79 //开卡 80 System.out.println("返回菜单中------"); 81 break; 82 case "3": 83 //退出 84 System.out.println("欢迎再次光临"); 85 System.out.println("程序结束"); 86 System.exit(1); 87 default: 88 System.out.println("输入不合法,请重新输入"); 89 continue; 90 } 91 92 93 //测试模块 94 // System.out.println("-------测试模块---------按1退出"); 95 // if (scanner.next().equals("1")) { 96 // break; 97 // } 98 } while (jump); 99 System.out.println("程序结束"); 100 } 101 102 }
|--运行结果
已经实现所有功能
|--过程中的错误记录
在同一个类中使用两次parrern要将正则设置为静态常量,否则会报错