标签:util put spl turn 界面 one scan print 功能
需求:
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 boolean login(String name,String password) { 44 Set<String> keys = map.keySet(); 45 for(String s:keys) { 46 if(map.get(s).getUserName().equals(name)) { 47 if((map.get(s).getPassWord().equals(password))) { 48 System.out.println("登录成功!"); 49 return true; 50 }else { 51 System.out.println("用户名或密码不正确!请重新登录!"); 52 return false; 53 } 54 } 55 } 56 System.out.println("用户不存在,请重试输入!"); 57 return false; 58 } 59 }
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 LoginMap login = new LoginMap(); 13 System.out.println("请选择操作:1、登录\t2、注册"); 14 int i = input.nextInt(); 15 switch (i) { 16 case 1: 17 login.userLogin(); 18 break; 19 case 2: 20 login.userEnrol(); 21 break; 22 default: 23 System.out.println("感谢使用!"); 24 break; 25 } 26 } 27 28 //注册 29 public void userEnrol() { 30 31 do { 32 System.out.println("——————————用户注册——————————"); 33 System.out.println("请输入注册账号名:"); 34 String name = input.next(); 35 System.out.println("请输入登录密码:"); 36 String password = input.next(); 37 System.out.println("请再次确认密码:"); 38 String apassword = input.next(); 39 flag = userinit.Judge(name, password, apassword); 40 }while(!false); 41 42 } 43 44 //登录 45 public void userLogin() { 46 do { 47 System.out.println("——————————用户登录——————————"); 48 System.out.println("请输入登录账号名:"); 49 String name = input.next(); 50 System.out.println("请输入登录密码:"); 51 String password = input.next(); 52 flag = userinit.login(name, password); 53 }while(!flag); 54 55 } 56 }
测试运行结果:
标签:util put spl turn 界面 one scan print 功能
原文地址:https://www.cnblogs.com/Dean-0/p/11254405.html