[java] Simulate user management system

【Case question】

  1. Case description

Write a program to simulate the user management system.
The following function menu is provided when the program is running: registering new users, user login, modifying user information, querying users
(fuzzy query based on user name) and viewing all users.
User information includes: id user number, username user name, password, phone contact number.
In order to facilitate program testing, there are two pieces of test data in the program:
User {id=' A101' , username='Chen Duxiu' , password=' 888888' , phone=' 1351 1 1 1 1 1 1 1' } User {id
= ' A102' , username='Li Dazhao' , password=' 000000' , phone=' 13912345678' }
In order to facilitate user operation, after each function is executed, the function menu is displayed again.

  1. operation result

After the program is run, the function menu is first displayed:
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

【Case Objective】

• Learn to analyze the implementation ideas of "User Management System" program design.

• Independently complete the source code writing, compilation and operation of the "User Management System" program design based on ideas.

• Have knowledge of String class. The methods of the String class that may be used in this experiment are: equals (),
isEmpty (), contains (), indexOf ().

【case analysis】

There are two classes in this project: user class User and demonstration test class UserDemo.
1. User class
member attributes are: id, username, password, phone.
Member methods include: constructors, setters, getters and the overridden String toString () method, which is used to
return strings in the following format.

User {id=×× , username=×× , password=×× , phone=×× }

2. UserDemo class

This class needs to save the user list, and an array of User type is considered here. The integer variable userCount records
the number of saved users. The User type variable userLogin stores the current user who has passed login authentication. therefore,

Member attributes are: (the following are all static attributes)

 User[] userList

 int userCount

 User userLogin。

Member methods are: (the following are all static methods)

 Main method void main()

 Display function menu void showMenu()

 View all users void showAll()

 Register a new user void addUser()

 User login void login()

 Modify user information void alterUser()

 Query user void searchByUsername()

[Run the code]
1.User class

package cn.edu.gpnu.bank.User;

public class User {
    
    
    public String id;
    public String username;
    public String password;
    public String phone;

    public User(String id, String username, String password, String phone) {
    
    
        this.id = id;
        this.username = username;
        this.password = password;
        this.phone = phone;
    }

    public String getId() {
    
    
        return id;
    }

    public void setId(String id) {
    
    
        this.id = id;
    }

    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 getPhone() {
    
    
        return phone;
    }

    public void setPhone(String phone) {
    
    
        this.phone = phone;
    }

    @Override
    public String toString() {
    
    
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}


2.UserDemo class

package cn.edu.gpnu.bank.User;

import java.util.Scanner;

public class UserDemo {
    
    
    static User[] userList = new User[100]; //用数组保存用户信息
    static int userCount = 0; //保存的用户的数量
    static User userLogin = null; //记录当前登录的用户

    public static void main(String[] args) {
    
    
        User user;
        user = new User("A101", "陈独秀", "888888", "13511111111");//测试数据
        userList[userCount++] = user; //保存到数组中,并修改数量值。
        user = new User("A102", "李大钊", "000000", "13912345678");//测试数据
        userList[userCount++] = user; //保存到数组中,并修改数量值。
        Scanner scanner = new Scanner(System.in);
        while (true) {
    
    
            showMenu(); //打印系统操作菜单
            String choice = scanner.nextLine(); //获取用户要执行的命令编号
            switch (choice) {
    
    
                case "1": //显示所有用户信息
                    showAll();
                    break;
                /* ****补充多行代码****。  */
                case "2": //注册新用户
                    addUser();
                    break;
                case "3": //用户登录
                    login();
                    break;
                case "4": //修改用户信息
                    alterUser();
                    break;
                case "5": //查询用户
                    searchByUsername();
                    break;
                case "0": //结束程序
                    System.out.println("程序运行结束。");
                    return;
            }
        }
    }


    public static void showMenu() {
    
    
        System.out.println("*** 欢迎使用用户管理系统 ***");
        System.out.println("1.查看所有用户");
        System.out.println("2.注册新用户");
        System.out.println("3.登录用户");
        System.out.println("4.修改用户信息");
        System.out.println("5.根据用户名模糊查询");
        System.out.println("0.退出程序");
        System.out.print("请选择要执行的操作:");
    }

    public static void showAll() {
    
    
        System.out.println("系统的所有用户信息如下:");
        for (int i = 0; i < userCount; i++) {
    
    
            System.out.println(userList[i].toString());
        }
    }

    public static void addUser() {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用户编号:");
        String id = scanner.nextLine();

        /* ****补充多行代码****。提示:获取注册用户的其他信息,然后添加该用户到列表 */
        System.out.print("请输入用户名:");
        String username = scanner.nextLine();

        System.out.print("请输入用户密码:");
        String password = scanner.nextLine();

        System.out.print("请输入用户联系电话:");
        String phone = scanner.nextLine();

        System.out.println("成功添加用户。");
        User user;
        user = new User(id, username, password, phone);
        userList[userCount++] = user;

    }

    private static void login() {
    
    
        User user = null;
        String id;
        String password;

        System.out.print("请输入用户编号:");
        Scanner scanner = new Scanner(System.in);
        id = scanner.nextLine();
        /* ****补充多行代码****。提示:从用户列表中搜索该用户编号的用户*/
        for (int i = 0; i < userCount; i++) {
    
    
            if (userList[i].id.equals(id)) {
    
    
                user = userList[i];
            }
        }


        if (user == null) {
    
    
            System.out.println("用户不存在!");
            return;
        }
        System.out.print("请输入用户密码:");
        password = scanner.nextLine();

        if (user.password.equals(password) /* ***补充代码****。提示:验证密码是否正确 */) {
    
    
            System.out.println("登录成功");
            /* ***补充代码****。提示:更新当前登录用户。*/
            userLogin = user;
        } else {
    
    
            System.out.println("登录失败");
            userLogin = null;
        }
    }

    private static void alterUser() {
    
    
        if (userLogin == null) {
    
    
            System.out.println("你尚未登录系统,请先登录验证。");
            return;
        }
        String username;
        String password;
        String phone;
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入新的用户名:[" + userLogin.getUsername()
                + "]");
        username = scanner.nextLine();
        userLogin.setUsername(username);

        System.out.print("请输入新的密码:[" + userLogin.getPassword()
                + "]");
        password = scanner.nextLine();
        userLogin.setPassword(password);
        System.out.print("请输入新的联系电话:[" + userLogin.getPhone()
                + "]");
        phone = scanner.nextLine();
        userLogin.setPhone(phone);

        /* ****补充多行代码****。提示:若输入新信息不为空,则保存到 userLogin 对象。  */
        System.out.println("用户信息修改成功。");
    }

    private static void searchByUsername() {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要查询的用户名:");
        String username = scanner.nextLine();
        System.out.println("查询结果如下:");

        /* ****补充多行代码****。提示:参考 showAll()方法。*/
        for (int i = 0; i < userCount; i++) {
    
    
            if (userList[i].username.contains(username) == true) {
    
    
                System.out.println(userList[i]);
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_52703008/article/details/126201772