1 public class User { 2 3 private String name; 4 private String password; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public String getPassword() { 15 return password; 16 } 17 18 public void setPassword(String password) { 19 this.password = password; 20 } 21 22 public User() { 23 24 } 25 26 public User(String name, String password) { 27 this.name = name; 28 this.password = password; 29 } 30 }
//登录 public static int isLogin(ArrayList<User> user) { if(user.size() == 0) { System.out.println("当前没有用户,登录失败!"); System.exit(0); } Scanner sc = new Scanner(System.in); System.out.println("输入用户名:"); String name = sc.next(); System.out.println("输入密码:"); String password = sc.next(); boolean isLogin = false; Iterator it = user.iterator(); while(it.hasNext()) { User u = (User)it.next(); if(u.getName().equals(name) && u.getPassword().equals(password)) { isLogin = true; } if(isLogin == true) { System.out.println("登录成功!"); }else { System.out.println("用户名或密码输入有误,请重新输入!"); } } return 0; } //注册 public static void addUser(ArrayList<User> user) { Scanner sc = new Scanner(System.in); System.out.println("输入用户名:"); String name = sc.next(); System.out.println("输入密码:"); String password = sc.next(); User u = new User(name,password); user.add(u); System.out.println("注册成功!"); } //查看 public static void queryUser(ArrayList<User> user) { if(user.size() == 0) { System.out.println("没有管理员账户,请添加用户后查询!"); return; } System.out.println("序号\t用户名\t密码"); for(int i = 0; i < user.size(); i++) { User u = user.get(i); System.out.println(i+"\t"+u.getName()+ "\t" + u.getPassword()); } } //修改 public static void updateUser(ArrayList<User> user) { queryUser(user); Scanner sc = new Scanner(System.in); System.out.println("输入要修改的用户序号:"); int sum = sc.nextInt(); if(sum > user.size()) { System.out.println("输入有误!"); return; } System.out.println("输入新的用户名:"); String name = sc.next(); System.out.println("输入新的密码:"); String password = sc.next(); User u = new User(name,password); user.set(sum, u); System.out.println("修改成功"); } //删除 public static void deleteUser(ArrayList<User> user) { queryUser(user); Scanner sc = new Scanner(System.in); System.out.println("输入要删除的用户序号:"); int sum = sc.nextInt(); user.remove(sum); System.out.println("删除成功!"); } }