Implementation of user management system
User attributes: user ID, user name, password, name, age, gender, ID number, user status (check user account is normal, 1 normal, 0 abnormal, for abnormal users can not log in)
There are 2 original users:
User 1: user name: zhang, password: 123; Login status: 1
User 2: user name: wang, password: 111; Login status: 0
Administrator: admin, admin, 1
Requirements:
1: Login: login is successful only when the user name, password and authentication code are correct and the user status is 1. The user status is 0 and cannot log in.
2: After successful login, you will be prompted to welcome: xxx
Possible operations
administrators:
1: View all users: you can select to view all user information and display all user names, passwords, names and user status, - 5
2: Blackout: you can blackout the user according to the user number. Once the user is blackout, the user cannot continue to log in. You need to judge whether the entered user number is reasonable-- ten
3: To view the user's details according to the user number, you need to judge the rationality. If there is no user, the information without this person will be displayed -- 10
4: Fuzzy query is performed according to a word of the user -- all names containing a word are found and displayed. For example: three, all users including three will be displayed, such as Zhang San, Sanzi, Zhang Sanfeng -- 20
Ordinary user: to modify the password, you need to enter the original password before modifying it, and then you can modify it - 5
be careful:
- After each operation, it is necessary to provide the return function - 10 points will be deducted if there is no return function
- Do a good job in the exception handling of each step, and deduct 5 points in case of an exception
package Text6; public class User { private int uid; private String username; private String password; private String name; private int age; private String sex; private String cardId; private String stats; public User(String username, String password, String stats) { this.username = username; this.password = password; this.stats = stats; } public User(int uid, String username, String password, String name, int age, String sex, String cardId, String stats) { this.uid = uid; this.username = username; this.password = password; this.name = name; this.age = age; this.sex = sex; this.cardId = cardId; this.stats = stats; } @Override public String toString() { return "User{" + "uid=" + uid + ", username='" + username + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", cardId='" + cardId + '\'' + ", stats='" + stats + '\'' + '}'; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public String getStats() { return stats; } public void setStats(String stats) { this.stats = stats; } }
package Text6; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Demo { static Scanner sc=new Scanner(System.in); static List<User> users=new ArrayList<>(); static User loginUser=null; public static void main(String[] args) { User user0=new User(0,"admin","admin","administrators",20,"male","10086","1"); User user1=new User(1,"zhang","123","Zhang San",18,"male","10001","1"); User user2=new User(2,"wang","111","Wang Wu",38,"male","10002","0"); users.add(user0); users.add(user1); users.add(user2); index(); } public static void index(){ System.out.println("Please select the desired operation"); System.out.println("1.Sign in"); System.out.println("2.Enter any key to exit"); Scanner sc = new Scanner(System.in); String num = sc.next(); switch(num){ case "1": login(); break; default: System.out.println("Program exit"); System.exit(0); } } public static void login(){ System.out.println("enter one user name:"); String myName = sc.next(); System.out.println("Input password"); String myPwd = sc.next(); String username=null; for(User user : users){ if("1".equals(user.getStats())&&(myName.equals(user.getUsername()))&& (myPwd.equals(user.getPassword()))){ username = myName; System.out.println("Login succeeded,Welcome:"+username); break; } } if(username == null){ System.out.println("Login failed, please login again"); index();//Login failed, return to the home page }else{ if ("admin".equals(username)){ loginUser=users.get(0);//Change========== adminView();//Administrator view }else{ //Get current user login object for (User u:users) { if(u.getUsername().equals(myName)){ //When you come in, u is the login object loginUser=u; } } userView(); } } } private static void userView() { System.out.println("1.Change Password"); System.out.println("2.View my info"); System.out.println("3.Enter 0 to return to the previous level"); int num=sc.nextInt(); if(num==1){ changePwd(); }else if(num==2){ System.out.println(loginUser.getUid()+"\t"+loginUser.getUsername()+"\t"+loginUser.getPassword()+"\t"+loginUser.getAge()+"\t"+loginUser.getSex()+"\t"+loginUser.getCardId()+"\t"+loginUser.getStats()); userView(); }else if(num==0){ index(); } } private static void changePwd() { System.out.println("Please enter the original password:"); String oldPwd=sc.next(); if(oldPwd.equals(loginUser.getPassword())){ System.out.println("Please enter a new password:"); String newPwd=sc.next(); System.out.println("Please confirm the password again:"); String newPwd2=sc.next(); if(newPwd.equals(newPwd2)){ loginUser.setPassword(newPwd); System.out.println("Password modified successfully"); login(); }else { System.out.println("The two passwords are inconsistent. Please operate again"); changePwd(); } System.out.println("Password error, please operate again"); changePwd(); } } private static void adminView() { System.out.println("Welcome, admin"); System.out.println("1: View all users");//check System.out.println("2.block ");//If the status is changed to 0, you need to judge whether the entered user number is reasonable System.out.println("3.View detailed user information");//It is necessary to judge the rationality. If there is no such user, the information of no such person will be displayed System.out.println("4.Fuzzy query");//Fuzzy query is performed according to a word of the user -- all names containing a word are found and displayed. For example: three, all users including three will be displayed, such as Zhang San, Sanzi, Zhang Sanfeng -- 20 System.out.println("0.Back to the home page"); int num=sc.nextInt(); switch (num){ case 1:showUsers(); case 2:blackUsers(); case 3:showDetialUsers(); case 4:blurSelectUsers(); case 0:index(); default: System.out.println("Incorrect input"); adminView(); } } private static void blurSelectUsers() { System.out.println("Please enter the fuzzy query name:"); String bname=sc.next(); String bname0=null; for (User user:users) { if(user.getName().contains(bname)){ bname0=bname; System.out.println(user); } } if(bname0==null){ System.out.println("non-existent,Please re-enter"); blurSelectUsers(); }adminView(); } private static void showDetialUsers() { System.out.println("Please enter user id: "); int x=sc.nextInt(); int id=-1; for (User user:users) { if(x==user.getUid()){ id=x; } } if(id!=-1){ for (User user:users) { if(x==user.getUid()){ System.out.println(user); } } }else { System.out.println("id non-existent"); showDetialUsers(); } adminView(); } private static void blackUsers() { System.out.println("Please enter a black user id"); int id=-1; int x=sc.nextInt(); for (User user:users) { if(x==user.getUid()){ id=x; } } if(id!=-1){ for (User user:users) { if(x==user.getUid()){ user.setStats("0"); } } }else { System.out.println("id non-existent"); blackUsers(); } System.out.println("Pull black success"); adminView(); } private static void showUsers() { for (User user:users) { System.out.println("user name:"+user.getUsername()+"password:"+user.getPassword()+"full name:"+user.getName()+"User status:"+user.getStats()); } System.out.println("Enter 0 to return to the previous level"); int num=sc.nextInt(); if(num==0){ adminView(); }else { System.out.println("Enter 0 to return to the previous level"); } } }
Operation results: