【0729作业】会员注册
1、注册和登录功能,使用已知的帐号和密码可以直接登录,如果没有帐号,注册后可以用刚注册的帐号登录
2、注册的时候,如果帐号已经存在,不能注册
3、注册的时候,设置两次密码,如果两次密码设置得不同,不能注册
4、登录的时候,密码输错三次,锁定帐号
5、登录的时候,如果没有这个帐号,则提醒用户注册,注册后可以登录
6、登录的时候,帐号密码都正确要输入验证码,系统给出的验证码和用户输入一致时,允许登录(忽略大小写)
7、登录之后,用户可以看到用户的预留信息
1 package com.login3; 2 3 public class User { 4 private String accounts;//登录账户 5 private String password;//登录密码 6 private String phone;//手机号 7 public String getPhone() { 8 return phone; 9 } 10 public void setPhone(String phone) { 11 this.phone = phone; 12 } 13 public String getAccounts() { 14 return accounts; 15 } 16 public void setAccounts(String accounts) { 17 this.accounts = accounts; 18 } 19 public String getPassword() { 20 return password; 21 } 22 public void setPassword(String password) { 23 this.password = password; 24 } 25 26 public Login() { 27 // TODO Auto-generated constructor stub 28 29 } 30 public Login(String accounts, String password, String phone) { 31 super(); 32 this.accounts = accounts; 33 this.password = password; 34 this.phone = phone; 35 } 36 }
1 package com.login3; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Set; 6 7 public class Login { 8 static Map<String, User> map; 9 //初始化用户信息 10 static { 11 map = new HashMap<>(); 12 User user1 = new User("asd00000", "123456", "13900000000"); 13 User user2 = new User("qwe00000", "234567", "15900000000"); 14 User user3 = new User("zxc00000", "345678", "13800000000"); 15 16 map.put(user1.getAccounts(), user1); 17 map.put(user2.getAccounts(), user2); 18 map.put(user3.getAccounts(), user3); 19 } 20 21 //注册验证 22 public boolean Judge(String account,String password,String password2) { 23 Set<String> keys = map.keySet(); 24 for(String s:keys) { 25 if(map.get(s).getAccounts().equals(account)) { 26 System.out.println("账户已存在,注册失败!"); 27 return false; 28 } 29 } 30 if(password.equals(password2)) { 31 System.out.println("注册成功!"); 32 System.out.println("您的登录账户为:" + account + ",登录密码为:" + password ); 33 34 return true; 35 }else { 36 System.out.println("两次输入密码不一致,请重新输入!"); 37 return false; 38 } 39 40 } 41 42 43 //登录验证 44 public int login(String account,String password,String code,String userCode) { 45 Set<String> keys = map.keySet(); 46 for(String s:keys) { 47 if(map.get(s).getAccounts().equals(account)) { 48 int i = 1; 49 do{ 50 if((map.get(s).getPassword().equals(password))) { 51 return verify(code,userCode); 52 }else { 53 if(i == 1) { 54 System.out.println("用户名或密码不正确!请重新输入(剩余两次机会):"); 55 }else { 56 System.out.println("用户名或密码不正确!请重新输入(剩余一次机会):"); 57 } 58 password = LoginSystem.sc.next(); 59 System.out.println("请输入下方验证码(不区分大小写):"); 60 code = Login.getVerify(6); 61 System.out.println(code); 62 userCode = LoginSystem.sc.next(); 63 if((map.get(s).getPassword().equals(password))) { 64 return verify(code,userCode); 65 } 66 i++; 67 } 68 if(i>=3) { 69 System.out.println("连续三次输入密码错误,登录失败!!"); 70 return 0; 71 } 72 }while(true); 73 } 74 } 75 System.out.println("用户不存在,请重试输入!"); 76 return 2; 77 } 78 79 //生成验证码 80 public static String getVerify(int length){ 81 String code = ""; 82 String str = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASFGHJKLZXCVBNM"; 83 String[] strs = str.split(""); 84 for(int i = 0;i<length;i++){ 85 code += strs[(int)(Math.random()*strs.length)]; 86 } 87 return code; 88 } 89 90 //校验验证码 91 public static int verify(String code,String userCode) { 92 do { 93 if(code.equalsIgnoreCase(userCode)) { 94 System.out.println("登录成功!!正在加载系统..."); 95 return 0; 96 }else { 97 System.out.println("验证码错误,请重新输入:"); 98 code = getVerify(6); 99 System.out.println(code); 100 userCode = LoginSystem.sc.next(); 101 } 102 }while(true); 103 } 104 }
1 package com.login3; 2 3 import java.util.Scanner; 4 5 public class LoginSystem { 6 static Scanner sc = new Scanner(System.in); 7 Login login = new Login(); 8 boolean flag; 9 10 public static void main(String[] args) { 11 Out: do { 12 LoginSystem login = new LoginSystem(); 13 System.out.println("***欢迎进入系统大厅***"); 14 System.out.println("请选择:1、登录 2、注册、3、退出系统"); 15 int i = sc.nextInt(); 16 switch (i) { 17 case 1: 18 login.userLogin(); 19 break Out; 20 case 2: 21 login.adduUser(); 22 break; 23 default: 24 break Out; 25 } 26 } while (true); 27 } 28 29 // 注册 30 public void adduUser() { 31 32 do { 33 System.out.println("请输入注册账号:"); 34 String account = sc.next(); 35 System.out.println("请输入注册密码:"); 36 String password = sc.next(); 37 System.out.println("请再次输入密码:"); 38 String password2 = sc.next(); 39 if ((password.length() < 3) || (password.length() < 6)) { 40 System.out.println("用户名长度不能小于3,密码长度不能小于6!"); 41 flag = false; 42 } else { 43 flag = login.Judge(account, password, password2); 44 if (flag) { 45 Login.map.put(account, new User(account, password, password2)); 46 } 47 } 48 } while (!flag); 49 50 } 51 52 // 登录 53 public void userLogin() { 54 int i = -1; 55 Out: do { 56 System.out.print("请输入登录账户:"); 57 String account = sc.next(); 58 System.out.print("请输入登录密码:"); 59 String password = sc.next(); 60 System.out.println("请输入下方验证码(不区分大小写):"); 61 String code = Login.getVerify(6); 62 System.out.println(code); 63 String userCode = sc.next(); 64 i = login.login(account, password, code, userCode); 65 if (i == 0) { 66 break Out; 67 } 68 } while (i == 2); 69 70 } 71 }
还得在琢磨琢磨.....