Java練習程式(六)
阿新 • • 發佈:2019-02-10
package lx4; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Scanner; //使用者 class User{ int id; String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User(int id,String password) { this.id=id; this.password=password; } public boolean equals(Object obj) { User user=(User)obj; return this.id==user.id; } public String toString() { return "{賬號:"+this.id+"密碼:"+this.password+"}"; } } public class Demo3 { static Scanner scanner=new Scanner(System.in); static Collection users=new ArrayList(); public static void main(String[] args) { // TODO Auto-generated method stub while(true) { System.out.println("請選擇功能 A(註冊 ) B(登陸)"); String option= scanner.next(); if("a".equalsIgnoreCase(option)) { reg(); }else if("b".equalsIgnoreCase(option)) { login(); }else { System.out.println("你的選擇有誤,請重新輸入"); } } } //登陸 public static void login() { System.out.println("請輸入賬號:"); int id=scanner.nextInt(); System.out.println("請輸入密碼:"); String password=scanner.next(); boolean isLogin =false; Iterator it = users.iterator(); while(it.hasNext()){ User user = (User) it.next(); if(user.id==id&&user.password.equals(password)) { isLogin=true; } } if(isLogin==true) { System.out.println("歡迎登陸.."); }else { System.out.println("使用者名稱或者密碼錯誤,登陸失敗..."); } } //註冊 public static void reg() { User user=null; while(true) { System.out.println("請輸入賬號:"); int id =scanner.nextInt(); user =new User(id,null); if(users.contains(user)) { System.out.println("該賬號已經存在,請重新輸入賬號"); }else { break; } } System.out.println("請輸入密碼:"); String password=scanner.next(); user.setPassword(password); users.add(user); System.out.println("註冊成功!"); System.out.println("當前註冊的人員:"+users); } }