java basic programming -- aviation user management system

Title Content:

An airline has different offices in different countries and places where its flights arrive. This project requires the development of an automated software system, which will be provided to the managers (role=1) and ordinary users (role=0) of these offices to manage flight information. According to the above description, the user module and office module of the system are required to be implemented, including the following functions (Note: the system has a default administrator admin/admin123):

User module:
1. User add
2. Password modification
3. View personal information
4. Account status modification (disable 0, enable 1)
5. User login (disabled account cannot log in and prompt friendly message)
6. Modify user role (set or cancel administrator)
7. User list
8. Query the employees of the designated office
9. Delete user

Office module:
1. Add office
2. List of offices

Note: administrators have all the above functions. Ordinary users only have the functions of password modification and personal information viewing

The reference information is as follows:
User class:
id, account (username), password (passord), age (age), role (role), email (email), office id(officeID), account status (status)

Office class:
id, office name (officeName)

The required technical parameters are as follows:
1. Branch and loop
2. Array or ArrayList
3. Method
4. Constructor
5.setter/getter
6. Abstract class or interface
7. Polymorphism
8.Scanner class

analysis:

1. The functions of administrators and users in the title are different. Ordinary users only have the functions of logging in to the system, password modification and personal information viewing, while administrators realize more functions, including all the functions of ordinary users. Then I can distinguish them when logging in. Different users get different function menu interfaces.

2. The administrator can set the status. If the status is disabled, you cannot log in. (the implementation method can be placed in the user)

3. The default administrator admin/admin123 (you can set an initialization block, which is also called a free block. It is a code block without a name. The execution time is generally executed before creating the object execution constructor, and the execution times depend on the creation times of the object, which is used to extract the repeated codes in multiple constructors for unified execution.)

4. Personal information viewing can only view personal information, but can't see the information of other users. (set a static variable, the user name at login is stored in it, and the personal information is found in the information storage through this static variable)

5. Interface (the interface is not written well this time. You can put all the methods in UserMange into the interface and implement them in UserMange class, as well as in OfficeMange class)
 

Content realization:

User class:

package com.softeem.j2106.work;

/**
 * @author admin
 * 2021/7/17
 */
public class User {
    private int id;
    private String username;
    private String password;
    private int age;
    private boolean role;
    private String email;
    private int officeID;
    private boolean status;

    public User() {

    }

    public User(int id, String username, String password, int age, boolean role, String email, int officeID, boolean status) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
        this.role = role;
        this.email = email;
        this.officeID = officeID;
        this.status = status;
    }

    public int getId() {
        return id;
    }

    public void setId(int 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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }



    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getOfficeID() {
        return officeID;
    }

    public void setOfficeID(int officeID) {
        this.officeID = officeID;
    }

    public boolean isRole() {
        return role;
    }

    public void setRole(boolean role) {
        this.role = role;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", role=" + role +
                ", email='" + email + '\'' +
                ", officeID=" + officeID +
                ", status=" + status +
                '}';
    }
}

office class:

package com.softeem.j2106.work;

/**
 * @author admin
 * 2021/7/17
 */
public class Office {
    private int officeID;
    private String officeName;

    public Office() {
    }

    public Office(int officeID, String officeName) {
        this.officeID = officeID;
        this.officeName = officeName;
    }

    public int getOfficeID() {
        return officeID;
    }

    public void setOfficeID(int officeID) {
        this.officeID = officeID;
    }

    public String getOfficeName() {
        return officeName;
    }

    public void setOfficeName(String officeName) {
        this.officeName = officeName;
    }

    @Override
    public String toString() {
        return "Office{" +
                "officeID=" + officeID +
                ", officeName='" + officeName + '\'' +
                '}';
    }
}

Inter interface:

package com.softeem.j2106.work;

public interface Inter {

    public void ShowAll();
}

usermange:

package com.softeem.j2106.work;

import java.util.Objects;

/**
 * @author admin
 * 2021/7/17
 */
public class UserManage implements Inter{
    private User[] users = new User[10];
    private int index=1;

    {
        users[0] = new User(0,"admin","admin123",20,true,"@163.com",0,true);
    }

    /**
     * User login
     */
    public int sign(String username, String password) {
        for (int i = 0; i < users.length; i++) {
            User s = users[i];
                if ((Objects.nonNull(s))&& s.getUsername().equals(username) && s.getPassword().equals(password)) {
                    if ((s.isRole())&& s.isStatus()) {
                        return 1;
                    } else if ((!s.isRole()) && s.isStatus()) {
                        return 0;
                    } else if (!s.isStatus()){
                        return -2;
                    }
                }
        }
        return -1;
    }


    /**
     * User add
     */
    public boolean add(User u) {
        if (index >= users.length) {
            return false;
        }
        users[index++] = u;
        return true;
    }

    /**
     * Password modification
     */
    public boolean updatePassword(String password) {
        for (int i = 0; i < users.length; i++) {
            User user = this.users[i];
            if ((Objects.nonNull(user))&&user.getPassword() != null) {
                users[i].setPassword(password);
                return true;
            }
        }
        return false;
    }


    /**
     * View personal information
     */
    public User SearchByID(String username) {
        User user = new User();
        for (User user1 : users) {
            if ((Objects.nonNull(user))&&user1.getUsername().equals(username)) {
                user = user1;
                break;
            }
        }
        return user;
    }

    /**
     * Account status modification
     */
    public boolean changeStatus(String username, boolean status) {
        User user = SearchByID(username);
        if (user != null) {
            user.setStatus(status);
            return true;
        }
        return false;
    }

    /**
     * Modify user roles
     */
    public boolean changeAdmin(String username, boolean role) {
        User user = SearchByID(username);
        if (user != null) {
            user.setRole(role);
            return true;
        }
        return false;
    }


    /**
     * Query employees in designated offices
     */
    public boolean searchofficeID(int officeId) {
        for (User user : users) {
            if ((Objects.nonNull(user))&&officeId == user.getOfficeID()) {
                System.out.println(user);
                return true;
            }
        }
        return false;
    }

    /**
     * delete user
     */
    public boolean delete(int id) {
        for (int i = 0; i < users.length; i++) {
            User s = users[i];
            if (Objects.nonNull(s) && Objects.equals(s.getId(), id)) {
                //Leave the current element empty
//                stus[i] = null;
                //Subsequent elements move forward to cover the blank position (avoid fragmentation)
                System.arraycopy(users, i + 1, users, i, users.length - index - 1);
                index--;
                return true;
            }
        }
        return false;
    }

    /**
     * User list
     */
    @Override
    public void ShowAll() {
        for (User user : users) {
            if (user != null) {
                System.out.println(user);
            }
        }
    }
}

officeMange class:

package com.softeem.j2106.work;

/**
 * @author admin
 * 2021/7/17
 */
public class OfficeMange implements Inter {
    private static Office[] off = new Office[10];
    private int index;

    /**
     * Add office
     */
    public boolean officeAdd(Office o) {
        if (index >= off.length) {
            return false;
        }
        off[index++] = o;
        return true;
    }
    /**List of offices*/
    @Override
    public void ShowAll() {
        for (Office office : off) {
            if (office != null) {
                System.out.println(office);
            }
        }
    }
}





tset class: (Implementation)

package com.softeem.j2106.work;

import java.util.Scanner;

/**
 * @author admin
 * 2021/7/17
 */
public class Test {
    static String loginname;
    static UserManage a = new UserManage();
    static OfficeMange b = new OfficeMange();
    static Scanner sc = new Scanner(System.in);


    public static void start() {
        msg("==============SOFTEEM User login==============");
        msg("=========================================");
        msg("Please enter the account number:");
        String username = sc.next();
        loginname = username;
        msg("Please input a password:");
        String password = sc.next();
        if (a.sign(username, password) == 1) {
            sign1();
        } else if (a.sign(username, password) == 0) {
            sign2();
        } else if (a.sign(username, password) == -1) {
            msg("Login failed!");
            start();
        } else if (a.sign(username, password) == -2) {
            msg("Account disabled!");
            start();
        }
    }


    public static void sign1() {
        msg("=========SOFTEEM Administrator management system=========");
        msg("[1] User add");
        msg("[2] Password modification");
        msg("[3] View personal information");
        msg("[4] Account status modification");
        msg("[5] Modify user roles");
        msg("[6] User list");
        msg("[7] Query employees in designated offices");
        msg("[8] Delete employee");
        msg("[9] User login");
        msg("[10] Add office");
        msg("[11] List of offices");
        msg("[0] Exit the system");
        msg("====================================");
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        switch (i) {
            case 1:
                addUser();
                break;
            case 2:
                pwd();
                sign1();
                break;
            case 3:
                selectbyid();
                sign1();
                break;
            case 4:
                updateStatus();
                break;
            case 5:
                updateRole();
                break;
            case 6:
                listUser();
                break;
            case 7:
                Search();
                break;
            case 8:
                delUser();
                break;
            case 9:
                start();
                break;
            case 10:
                addOffice();
                break;
            case 11:
                listOffice();
                break;
            case 0:
                msg("Thanks for using. Bye!");
                //System exit (shut down JVM)
                System.exit(0);
                break;
            default:
                msg("Instruction error,Please re-enter");
                sign1();
                break;
        }
    }

    public static void sign2() {
        msg("==========SOFTEEM User management system==========");
        msg("[1] Personal view");
        msg("[2] Password modification");
        msg("[0] Exit the system");
        msg("====================================");
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        switch (i) {
            case 1:
                selectbyid();
                sign2();
                break;
            case 2:
                pwd();
                sign2();
                break;
            case 0:
                msg("Thanks for using. Bye!");
                //System exit (shut down JVM)
                System.exit(0);
                break;
            default:
                msg("Instruction error,Please re-enter");
                start();
                break;
        }
    }


    public static void selectbyid() {
        User u = a.SearchByID(loginname);
        if (u == null) {
            msg("The user you entered id non-existent");
        }
        System.out.println(u);
    }


    public static void pwd() {
        msg("Please enter a new password:");
        String password = sc.next();
        if (a.updatePassword(password)) {
            msg("Modified successfully");
        } else {
            msg("Modification failed");
        }
    }


    private static void addUser() {
        msg("Please enter ID:");
        int id = sc.nextInt();
        msg("enter one user name:");
        String name = sc.next();
        msg("Please input a password:");
        String password = sc.next();
        msg("Please enter age:");
        int age = sc.nextInt();
        msg("Set to administrator [1] Yes: [0]no");
        int i = sc.nextInt();
        boolean role = true;
        if (i == 1){
            role = true;
        }else if (i == 0){
            role = false;
        }else {
            msg("Please enter the correct instruction");
        }
        msg("Please enter email address:");
        String emial = sc.next();
        msg("Please enter the Office ID:");
        int officeid = sc.nextInt();
        msg("Setting status [1] enabled: [0]Disable");
        int j = sc.nextInt();
        boolean status = true;
        if (j == 1){
            status = true;
        }else if (j == 0){
            status = false;
        }else {
            msg("Please enter the correct instruction");
        }
        User u = new User(id, name, password, age, role, emial, officeid, status);
        if (a.add(u)) {
            msg("Added successfully!!");
        } else {
            msg("Insufficient capacity!!");
        }
        //Return to main menu
        sign1();
    }

    /**Add office*/
    public static void addOffice(){
        msg("Please enter officeID:");
        int id = sc.nextInt();
        msg("Please enter the office name:");
        String name = sc.next();
        Office o = new Office(id,name);
       if (b.officeAdd(o)){
           msg("Added successfully!!");
       } else {
           msg("Insufficient capacity!!");
       }
       sign1();
    }
    public static void updateStatus() {
        msg("Please enter the user name to modify:");
        String username = sc.next();
        msg("Please modify the user's account status(Disable 0/Enable 1):");
        int j = sc.nextInt();
        boolean status = true;
        if (j == 1){
            status = true;
        }else if (j == 0){
            status = false;
        }else {
            msg("Please enter the correct instruction");
        }
        if (a.changeStatus(username, status)) {
            msg("Modified successfully");
        } else {
            msg("Modification failed");
        }
        sign1();
    }
/**Modify user's role information*/
    public static void updateRole() {
        msg("Please enter the user name to modify:");
        String username = sc.next();
        msg("Please modify the user's role information(Disable 0/Enable 1):");
        int i = sc.nextInt();
        boolean role = true;
        if (i == 1){
            role = true;
        }else if (i == 0){
            role = false;
        }else {
            msg("Please enter the correct instruction");
        }
        if (a.changeAdmin(username, role)) {
            msg("Modified successfully");
        } else {
            msg("Modification failed");
        }
        sign1();
    }

    /**User delete*/
    public static void delUser() {
        msg("Please enter ID:");
        int Id = sc.nextInt();
        if (a.delete(Id)) {
            msg("Delete succeeded!!");
        } else {
            msg("user does not exist!!");
        }
        //Return to the previous level
        sign1();
    }

    /**User list*/
    public static void listUser() {
        a.ShowAll();
        //Return to the previous level
        sign1();
    }

    /**List of offices*/
    public static void listOffice(){
        b.ShowAll();
        sign1();
    }

    private static void Search() {
        msg("Please enter ID:");
        int ID = sc.nextInt();
        if (a.searchofficeID(ID)){

        }else {
            msg("Unknown query");
        }

        sign1();
    }

    public static void msg(String msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) {
        start();
    }
}

User login:

Administrator login:

User login:

 

 

It's not written very well, and the code repeats more. I hope all friends can give advice

Basically realize the practice requirements. Interested friends can try it by themselves

Keywords: intellij-idea

Added by jreed2132 on Mon, 17 Jan 2022 05:18:53 +0200