1 /** 2 * @author Administrator 3 * 用户信息:会员名字、会员密码、会员卡号、会员积分、错误累计、密保答案 4 */ 5 public class User { 6 private String name;//会员名字 7 private String password;//会员密码 8 private String vipID;//会员卡号 9 private int integral;//会员积分 10 private int num;//错误累计 11 private String answer;//密保问题 12 13 public User() {} 14 public User(String name, String password, String vipID, int integral, int num,String answer) { 15 this.name = name; 16 this.password = password; 17 this.vipID = vipID; 18 this.integral = integral; 19 this.num = num; 20 this.answer = answer; 21 } 22 23 24 public String getName() { 25 return name; 26 } 27 public void setName(String name) { 28 this.name = name; 29 } 30 public String getPassword() { 31 return password; 32 } 33 public void setPassword(String password) { 34 this.password = password; 35 } 36 public String getVipID() { 37 return vipID; 38 } 39 public void setVipID(String vipID) { 40 this.vipID = vipID; 41 } 42 public int getIntegral() { 43 return integral; 44 } 45 public void setIntegral(int integral) { 46 this.integral = integral; 47 } 48 public int getNum() { 49 return num; 50 } 51 public void setNum(int num) { 52 this.num = num; 53 } 54 public String getAnswer() { 55 return answer; 56 } 57 public void setAnswer(String answer) { 58 this.answer = answer; 59 } 60 61 62 }
1 import java.util.HashMap; 2 3 /** 4 * @author Administrator 5 * 初始化用户 6 * 7 */ 8 public class UserInit { 9 //创建Map集合,以会员卡号为键,存储会员信息 10 static HashMap<String,User> users = new HashMap<>(); 11 static { 12 //用户信息:String会员名字、String会员密码、String会员卡号、int会员积分、int错误累计、String密保问题 13 User user1 = new User("Verde","123456","56696485",2000,0,"范冰冰"); 14 User user2 = new User("xvzvz","123456","98656232",155,0,"迪丽热巴"); 15 User user3 = new User("gsgsf","123456","13356485",220,0,"赵丽颖"); 16 17 users.put(user1.getName(), user1); 18 users.put(user2.getName(), user2); 19 users.put(user3.getName(), user3); 20 } 21 }
1 import java.util.Set; 2 import java.util.regex.Matcher; 3 import java.util.regex.Pattern; 4 5 /** 6 * @author Administrator 7 * 方法大混战 8 */ 9 public class Judge { 10 static Set<String> set = UserInit.users.keySet(); 11 static boolean flag = false; 12 // 密保判断 13 public static void Secret(String name) { 14 System.out.println("您的爱人是谁:"); 15 do { 16 String answer = View.input.next(); 17 if (UserInit.users.get(name).getAnswer().equals(answer)) { 18 flag = false; 19 System.out.println("您的账号密码为:" + UserInit.users.get(name).getPassword()); 20 } else { 21 flag = true; 22 System.out.println("您的密保不正确,请重新输入:"); 23 UserInit.users.get(name).setNum(UserInit.users.get(name).getNum() + 1); 24 } 25 } while (flag); 26 } 27 28 //登录密码判断 29 public static boolean isPassword(String password,String name) { 30 int i = 3; 31 int num = 0; 32 do { 33 num = UserInit.users.get(name).getNum(); 34 if(UserInit.users.get(name).getPassword().equals(password)) { 35 return true; 36 } 37 UserInit.users.get(name).setNum(num+1); 38 if(num!=2) { 39 System.out.println("您输入的密码不正确,请重新输入(您还有"+(i-1)+"次):"); 40 i--; 41 password = View.input.next(); 42 }else { 43 System.out.println("您已输入三次密码错误,此账号已被暂时冻结,请一天后在重试,谢谢!"); 44 } 45 46 }while(num != 2); 47 return false; 48 } 49 50 //生成验证码 51 public static String getCode(int length){ 52 String code = ""; 53 for(int i=0;i<length;i++){ 54 boolean boo = (int)(Math.random()*2)==0; 55 if(boo){ 56 code += String.valueOf((int)(Math.random()*10)); 57 }else { 58 int temp = (int)(Math.random()*2)==0?65:97; 59 char ch = (char)(Math.random()*26+temp); 60 code += String.valueOf(ch); 61 } 62 } 63 return code; 64 } 65 66 //判断是否为纯数字 67 public static boolean isNumeric(String str) { 68 Pattern pattern = Pattern.compile("[0-9]*"); 69 return pattern.matcher(str).matches(); 70 } 71 72 //判断是否为纯字母 73 public static boolean isAlph(String str) { 74 return str.matches("[a-zA-Z]+"); 75 } 76 77 //判断是否为字母与数字的组合 78 public static boolean isNumAndAlph(String str) { 79 return str.matches("^.*[a-zA-Z]+.*$") && str.matches("^.*[0-9]+.*$") && !str.matches("^.*[/^/$/.//,;:'!@#%&/*/|/?/+/(/)/[/]/{/}]+.*$"); 80 } 81 82 //判断是否有特殊字符 83 public static boolean isSpe(String str) { 84 return str.matches("^.*[/^/$/.//,;:'!@#%&/*/|/?/+/(/)/[/]/{/}]+.*$"); 85 } 86 87 //判断是否含有中文字符 88 public static boolean isContainChinese(String password) { 89 Pattern p = Pattern.compile("[u4e00-u9fa5]"); 90 Matcher m = p.matcher(password); 91 if (m.find()) { 92 return true; 93 } 94 return false; 95 } 96 97 //判断是否有空格 98 public static boolean isBlank(String password) { 99 int i = password.indexOf(" "); 100 if (i != -1) { 101 return true; 102 } 103 return false; 104 } 105 106 //注册判断是否合法 107 public static void isNameLegal() { 108 do { 109 System.out.println("请输入注册名:"); 110 String name = View.input.next(); 111 for(String s:set) { 112 if(UserInit.users.get(s).equals(name)) { 113 flag = true; 114 System.out.println("用户名已存在,请重新输入:"); 115 name = View.input.next(); 116 }else { 117 if(name.length()>=3&&name.length()<=6&&!isNumeric((String) name.subSequence(0, 1))&&!isSpe(name)) { 118 isPwdLegal(name); 119 flag = false; 120 return; 121 }else { 122 flag = true; 123 System.out.println("您的用户名长度不合法(用户名长度为3~6位之间且首字符不能为数字):"); 124 name = View.input.next(); 125 } 126 } 127 } 128 }while(flag); 129 130 } 131 132 //注册密码判断是否合法 133 public static void isPwdLegal(String name) { 134 String password; 135 do { 136 System.out.println("请输入密码:"); 137 password = View.input.next(); 138 System.out.println("请再次确认密码:"); 139 String password1 = View.input.next(); 140 System.out.println("请输入下方验证码:"); 141 String code = getCode(6); 142 System.out.println(code); 143 String userCode = View.input.next(); 144 int length = password.length(); 145 if (length >= 6 && length <= 16 && !isContainChinese(password) && !isBlank(password)) { 146 flag = false; 147 if (password.equals(password1)) { 148 Level(password); 149 flag = verify(code,userCode); 150 String answer = issue(); 151 User user = new User(name,password,vipID(),100,0,answer); 152 UserInit.users.put(name,user); 153 System.out.println("恭喜您注册成功!您的会员卡号为:"+user.getVipID()+" 新注册会员赠送100积分!"); 154 155 } else { 156 flag = true; 157 System.out.println("您两次输入的密码不一致,请重新输!:"); 158 } 159 } else { 160 flag = true; 161 System.out.println("您的用户名长度不合法(用户名长度为3~6位之间且首字符不能为数字),请重新输入,账号:"); 162 password = View.input.next(); 163 } 164 } while (flag); 165 } 166 167 168 // 校验验证码 169 public static boolean verify(String code, String userCode) { 170 do { 171 if (code.equalsIgnoreCase(userCode)) { 172 System.out.println("验证成功!"); 173 return false; 174 } else { 175 System.out.println("验证码错误,请重新输入:"); 176 code = getCode(6); 177 System.out.println(code); 178 userCode = View.input.next(); 179 } 180 } while (true); 181 } 182 183 //随机生成方法 184 public static int getNum(int start, int end) { 185 return (int) (Math.random() * (end - start + 1) + start); 186 } 187 188 //生成8位会员卡号 189 public static String vipID() { 190 String first = String.valueOf(getNum(0, 9999) + 10000).substring(1); 191 String second = String.valueOf(getNum(0, 9999) + 10000).substring(1); 192 return first+second; 193 } 194 195 //密保问题 196 public static String issue() { 197 System.out.println("请设置您的密保问题,以便忘记密码找回"); 198 System.out.println("你最爱的人是谁?"); 199 String answer = View.input.next(); 200 return answer; 201 } 202 203 //修改密码 204 public static boolean changePWd(String name) { 205 System.out.println("请输入旧密码:"); 206 String oldpwd = View.input.next(); 207 if(isPassword(oldpwd,name)) { 208 do { 209 System.out.println("请输入新密码:"); 210 String newPwd = View.input.next(); 211 System.out.println("请再次确认新密码:"); 212 String newPwd1 = View.input.next(); 213 if(newPwd.equals(newPwd1)) { 214 if(UserInit.users.get(name).getPassword().equals(newPwd)) { 215 flag = true; 216 System.out.println("新密码不能与旧密码一致,请重新输入!"); 217 }else { 218 flag = false; 219 UserInit.users.get(name).setPassword(newPwd); 220 System.out.println(UserInit.users.get(name).getPassword()); 221 System.out.println("密码修改成功!请重新登录!"); 222 } 223 }else { 224 flag = true; 225 System.out.println("密码不一致,请重新输入!"); 226 } 227 }while(flag); 228 } 229 return true; 230 } 231 232 public static void Level(String password) { 233 if(isNumeric(password)) { 234 System.out.println("密码强度:低"); 235 }else if(isAlph(password)) { 236 System.out.println("密码强度:低"); 237 }else if(isNumAndAlph(password)) { 238 System.out.println("密码强度:中"); 239 }else { 240 System.out.println("密码强度:高"); 241 } 242 243 } 244 }
1 /** 2 * @author Administrator 3 * 注册 4 */ 5 public class Logon { 6 7 public static void Logon() { 8 Judge.isNameLegal(); 9 } 10 }
1 /** 2 * @author Administrator 3 * 登录操作以及二级界面 4 */ 5 public class Login { 6 //会员登录 7 public static void Login() { 8 System.out.println("请输入会员名:"); 9 String name = View.input.next(); 10 test:do { 11 for(String s:Judge.set) { 12 if(s.equals(name)) { 13 System.out.println("您是否记得密码(y/n):"); 14 String choose = View.input.next(); 15 String password = null; 16 if("n".equalsIgnoreCase(choose)) { 17 Judge.Secret(name); 18 System.out.println("请输入密码:"); 19 password = View.input.next(); 20 }else { 21 System.out.println("请输入密码:"); 22 password = View.input.next(); 23 } 24 if(Judge.isPassword(password, name)) { 25 second(name); 26 break test; 27 }else { 28 return; 29 } 30 } 31 } 32 System.out.println("您输入的用户名不存在,请重新输入:"); 33 name = View.input.next(); 34 }while(true); 35 } 36 37 38 public static void second(String name) { 39 boolean flag = false; 40 System.out.println("尊敬的会员:"+name+"欢迎回来!"); 41 do { 42 System.out.println("1、积分查询 2、积分累计 3、积分兑换 4、修改密码 5、退出系统"); 43 System.out.println("请输入操作选项:"); 44 String choose = View.input.next(); 45 int counts = 0; 46 counts = UserInit.users.get(name).getIntegral(); 47 switch (choose) { 48 //查询积分 49 case "1": 50 System.out.println("您当前积分为:"+counts); 51 break; 52 //积分累计 53 case "2": 54 System.out.println("请输入消费金额(消费一元换一积分,不足一元不算一积分):"); 55 String count = View.input.next(); 56 do { 57 if (Judge.isNumeric(count)) { 58 flag = false; 59 Integer count1 = Integer.parseInt(count); 60 UserInit.users.get(name).setIntegral(counts += count1); 61 System.out.println("您当前积分为:" + counts); 62 } else { 63 flag = true; 64 System.out.println("您输入的不是数字,请重新输入:"); 65 count = View.input.next(); 66 } 67 } while (flag); 68 break; 69 // 积分兑换 70 case "3": 71 System.out.println("您当前积分为:"+counts); 72 System.out.println("请输入您要兑换的积分数(100积分兑换一元,不足100积分不兑换一元):"); 73 String in = View.input.next(); 74 do { 75 if (Judge.isNumeric(in)) { 76 Integer count1 = Integer.parseInt(in); 77 if(counts >= count1) { 78 flag = true; 79 int i = (count1/100); 80 counts-=count1; 81 System.out.println("恭喜您成功兑换"+i+"元,您当前剩余积分为:" + counts); 82 System.out.println("您是否需要继续兑换?(y/n):"); 83 flag = View.input.next().equalsIgnoreCase("y")?true:false; 84 }else { 85 System.out.println("您的积分余额不足,无法兑换!是否重新兑换?(y/n):"); 86 flag = View.input.next().equalsIgnoreCase("y")?true:false; 87 } 88 89 } else { 90 flag = true; 91 System.out.println("您输入的不是数字,请重新输入:"); 92 count = View.input.next(); 93 } 94 } while (flag); 95 break; 96 //修改密码 97 case "4": 98 flag = Judge.changePWd(name); 99 return; 100 //退出系统 101 case "5": 102 flag = true; 103 return; 104 default: 105 System.out.println("非法操作,请重新输入!"); 106 break; 107 } 108 }while(!flag); 109 110 } 111 }
1 import java.util.Scanner; 2 3 /** 4 * @author Administrator 5 * 管理界面 6 * 系统菜单分为两级 7 * 一级菜单:登录、开卡、tuichuxit 8 * 二级菜单(登录之后):积分累积、积分兑换、积分查询、修改密码、退出登录 9 */ 10 public class View { 11 static Scanner input = new Scanner(System.in); 12 public static void main(String[] args) { 13 System.out.println("------欢迎光临皇家专属商城------"); 14 test:do { 15 System.out.println("1、登录 2、开卡 3、退出系统"); 16 String choose = input.next(); 17 switch (choose) { 18 case "1": 19 Login.Login(); 20 break; 21 case "2": 22 Logon.Logon(); 23 break; 24 case "3": 25 System.out.println("感谢使用本系统!"); 26 break test; 27 default: 28 System.out.println("您操作不正常,请重新选择!"); 29 System.out.println("1、登录 2、开卡 3、退出系统"); 30 choose = input.next(); 31 break; 32 } 33 }while(true); 34 } 35 }