1 /** 2 * @author Administrator 3 * 登录信息 4 */ 5 public class UserLogin { 6 private String UserName; 7 private String PassWord; 8 9 10 public UserLogin() { 11 super(); 12 } 13 public UserLogin(String userName, String passWord) { 14 super(); 15 UserName = userName; 16 PassWord = passWord; 17 } 18 public String getUserName() { 19 return UserName; 20 } 21 public void setUserName(String userName) { 22 UserName = userName; 23 } 24 public String getPassWord() { 25 return PassWord; 26 } 27 public void setPassWord(String passWord) { 28 PassWord = passWord; 29 } 30 }
1 import java.util.HashMap; 2 import java.util.Map; 3 import java.util.Set; 4 5 /** 6 * @author Administrator 7 * 初始化登录信息以及编写注册和登录验证功能 8 */ 9 public class UserInit { 10 static Map<String, UserLogin> map; 11 //静态代码块初始化用户信息 12 static { 13 map = new HashMap<>(); 14 UserLogin userLogin1 = new UserLogin("Dean", "123456"); 15 UserLogin userLogin2 = new UserLogin("Tom", "123456"); 16 UserLogin userLogin3 = new UserLogin("Jack", "123456"); 17 map.put(userLogin1.getUserName(), userLogin1); 18 map.put(userLogin2.getUserName(), userLogin2); 19 map.put(userLogin3.getUserName(), userLogin3); 20 } 21 22 //注册验证功能 23 public boolean Judge(String name,String password,String apassword) { 24 Set<String> keys = map.keySet(); 25 for(String s:keys) { 26 if(map.get(s).getUserName().equals(name)) { 27 System.out.println("用户名已存在!"); 28 return false; 29 } 30 } 31 if(password.equals(apassword)) { 32 System.out.println("注册成功!"); 33 return true; 34 }else { 35 System.out.println("两次输入密码不一致,请重新输入!"); 36 return false; 37 } 38 39 } 40 41 42 //登录验证功能 43 public int login(String name,String password,String code,String userCode) { 44 Set<String> keys = map.keySet(); 45 for(String s:keys) { 46 if(map.get(s).getUserName().equals(name)) { 47 int i = 1; 48 do{ 49 if((map.get(s).getPassWord().equals(password))) { 50 return verify(code,userCode); 51 }else { 52 if(i == 1) { 53 System.out.println("用户名或密码不正确!请重新输入(您还有两次输入机会):"); 54 }else { 55 System.out.println("用户名或密码不正确!请重新输入(这是您最后一次输入机会):"); 56 } 57 password = LoginMap.input.next(); 58 System.out.println("请输入下方验证码(不区分大小写):"); 59 code = UserInit.getVerify(6); 60 System.out.println(code); 61 userCode = LoginMap.input.next(); 62 if((map.get(s).getPassWord().equals(password))) { 63 return verify(code,userCode); 64 } 65 i++; 66 } 67 if(i>=3) { 68 System.out.println("您连续输入三次密码错误,账号已被冻结!请一天后在尝试!"); 69 return 0; 70 } 71 }while(true); 72 } 73 } 74 System.out.println("用户不存在,请重试输入!"); 75 return 2; 76 } 77 78 //生成验证码 79 public static String getVerify(int length){ 80 String code = ""; 81 String str = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASFGHJKLZXCVBNM"; 82 String[] strs = str.split(""); 83 for(int i = 0;i<length;i++){ 84 code += strs[(int)(Math.random()*strs.length)]; 85 } 86 return code; 87 } 88 89 //校验验证码 90 public static int verify(String code,String userCode) { 91 do { 92 if(code.equalsIgnoreCase(userCode)) { 93 System.out.println("登录成功!"); 94 return 0; 95 }else { 96 System.out.println("验证码错误,请重新输入:"); 97 code = getVerify(6); 98 System.out.println(code); 99 userCode = LoginMap.input.next(); 100 } 101 }while(true); 102 } 103 }
1 import java.util.Scanner; 2 3 /** 4 * @author Administrator 5 * 用户操作界面 6 */ 7 public class LoginMap { 8 static Scanner input = new Scanner(System.in); 9 UserInit userinit = new UserInit(); 10 boolean flag; 11 public static void main(String[] args) { 12 test:do { 13 LoginMap login = new LoginMap(); 14 System.out.println("请选择操作(按其他键退出系统):1、登录 2、注册"); 15 int i = input.nextInt(); 16 switch (i) { 17 //登录 18 case 1: 19 login.userLogin(); 20 System.out.println("欢迎登录系统!"); 21 break test; 22 //注册 23 case 2: 24 login.userEnrol(); 25 break; 26 default: 27 break test; 28 } 29 }while(true); 30 System.out.println("感谢使用!"); 31 } 32 33 //注册 34 public void userEnrol() { 35 36 do { 37 System.out.println("——————————用户注册——————————"); 38 System.out.println("请输入注册账号名:"); 39 String name = input.next(); 40 System.out.println("请输入登录密码:"); 41 String password = input.next(); 42 System.out.println("请再次确认密码:"); 43 String apassword = input.next(); 44 if((password.length()<3) || (password.length()<6) ) { 45 System.out.println("用户名长度不能小于3,密码长度不能小于6!"); 46 flag = false; 47 }else { 48 flag = userinit.Judge(name, password, apassword); 49 if(flag) { 50 UserInit.map.put(name, new UserLogin(name, password)); 51 } 52 } 53 }while(!flag); 54 55 } 56 57 //登录 58 public void userLogin() { 59 int i = -1; 60 test:do { 61 System.out.println("——————————用户登录——————————"); 62 System.out.println("请输入登录账号名:"); 63 String name = input.next(); 64 System.out.println("请输入登录密码:"); 65 String password = input.next(); 66 System.out.println("请输入下方验证码(不区分大小写):"); 67 String code = UserInit.getVerify(6); 68 System.out.println(code); 69 String userCode = input.next(); 70 i = userinit.login(name, password,code,userCode); 71 if(i==0) { 72 break test; 73 } 74 }while(i==2); 75 76 } 77 }
测试运行结果:
登录测试:
三次输入密码错误,冻结账号,强制退出!
登录成功:
注册用户以及登录: