需求:
1、登錄賬號唯一,在注冊時驗證輸入的賬號是否可用,若已存在,則不可用,若不存在則可用
2、登錄時使用賬號密碼進行驗證


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 }
測試運行結果:
?
?