Manually realize the simple version of community express cabinet management system Version 0.1.1

1 project overview

As we all know, the express cabinet is used to store express, and its basic functions include storing express and taking express. The express cabinet system is only for two users - ordinary users and couriers. For ordinary users, it is just the function of taking express delivery, which will not be described here. For couriers, they are not just couriers delivering express, but managers of express. Only by operating the data of the express cabinet can they manage it. Therefore, the express cabinet system does not only contain a function of storing express.

As the manager of express delivery, the express cabinet system should provide four basic functions: addition, deletion, modification and query:
1. Storage of express: it is used for the express clerk to enter the express information (including the express order number and the name of the express company). After the entry, the information is stored in the system and a six digit random number retrieval code is returned for subsequent retrieval by users;
2. Delete express: when the express information is entered incorrectly, the courier needs to delete the entered express information, and the system will delete it according to the entered document number to release the space of the express cabinet;
3. Modify express: when it is found that the express information is wrong and needs to be modified, this function can realize the integration of deletion and storage functions, instead of entering after deletion, which is a waste of time;
4. Check all: as the name suggests, check the information and order number of all express in the cabinet and its placement position.

2 implementation steps

After clarifying the functions of each module, we can start to write our code. This system is a system that can interact with users and store information. Therefore, the whole system is divided into three parts. The first part is the view part to realize the input and output of the console, the second part is the structure and access of express data, and the third part is the scheduling part, which is used for the connection between input and output and data.

2.1 view part

A set of processes of the management system will involve a lot of printing. Welcome will be printed at the beginning, followed by identity selection. When the identity is selected as an ordinary user, it will print a prompt to enter the piece retrieval code. After the entered piece retrieval code is filtered, it will be handed over to the scheduling method for judgment. After judgment, it will print whether the piece retrieval is successful or failed. When selecting a courier, the courier has four operations for the courier: add, delete, modify and query. When storing (adding) express, when the express cabinet is not full, you need to enter the number and company name to complete the storage. At this time, the location and pick-up code of the express cabinet will be output. When the number entered by the courier is repeated with the number in the cabinet, you will be prompted that there is an error. To delete express delivery, you need to judge whether there is data that can be deleted according to the document number. If it does not exist, you will exit the current operation. When it exists, you will be further asked whether you really want to delete it. You can choose to delete it or cancel it. If there are two methods to modify the document number, ask the courier to continue to output the new position. If there are two methods to modify the document number, then ask the courier to continue to output the new position. You can view all the previous express operations.

2.2 data part

The express order number, company and pick-up code are encapsulated into an express class, and the express cabinet is regarded as an array composed of express class objects, and the storage location is the subscript of this two-dimensional array.

2.3 dispatching part

The main play is coming. With the interface to interact with users and the space to store numbers, we also need to schedule them in order to effectively realize the requirements. When the user inputs and preliminarily filters the input content, those with abnormal format or value can be screened out. Therefore, it can enter the scheduling part without error and proceed to the next step. When the user's information is received, the calling method is judged, stored and exported, so the code's reusability is very high, and the code block with high reuse is encapsulated into a method. When compiling the scheduling method, try to be as concise as possible, refine each module into one method, subdivide the sub modules realizing different functions in each module method, further extract them into methods, and so on. In subsequent use, it is more hierarchical and concise, and the maintenance work becomes easier because of clear organization.

3 code details

Because the modified express is deleted first and then modified. When the subsequent storage express finds the cancellation operation, the original data is deleted. Therefore, I need to add a method to store the deleted express. When the input is wrong, the operation is cancelled, that is, the modification is unsuccessful and needs to be restored. Therefore, I need to insert this method to restore the data after the modification is judged to be cancelled.

3.1 source code of view part

package show;

import data.Express;
import data.ExpressMsg;

import java.util.Random;
import java.util.Scanner;


public class View {
    // View part
    // The View class is used for console output prompt statements

    // Save a deleted courier and save the rows and columns
    private Express recycle = null;
    private int line,row;

    /**
     * Select the menu of identity, 1 - courier, 2 - ordinary user, 0 - exit
     * @return The value entered represents the identity
     */
    public int menu(){
        int a ;
        System.out.println("Please select identity:");
        System.out.println("1-courier");
        System.out.println("2-Ordinary users");
        System.out.println("0-Exit program");
        Scanner input = new Scanner(System.in);

        try {
            String s = input.nextLine();
            a = Integer.parseInt(s);
            if (a < 0||a > 2) {
                Exception e = new Exception();
                throw e;

            }else {
                return a;
            }

        }catch (Exception e){
            System.out.println("Identity input error, please re-enter");
            return menu();
        }
    }

    /**
     * The operation menu of the courier: 1-add express, 2-delete express, 3-modify express, 4-view all, 0-exit
     * @return The value entered represents the operation
     */
    public int operation(){
        int a = -1;
        System.out.println("As a courier, you can do the following:");
        System.out.println("1-Storage express");
        System.out.println("2-Delete Express");
        System.out.println("3-Modify express information");
        System.out.println("4-View all couriers");
        System.out.println("0-Return to the previous level");
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        try {
            a = Integer.parseInt(s);
            if (a < 0 || a > 4) {
                Exception e = new Exception();
                throw e;
            }
            return a;
        }catch (Exception e){
            return operation();
        }
    }

    /**
     * 1-Add an express operation, pass in an express object, and judge whether there is an express with the same order number
     * @param e The object of express class is compared with the express in the box
     * @return true is returned when saving succeeds and false is returned when saving fails
     */
    public boolean add(Express e){
        // Judge whether there is express delivery with the same order number
        if(this.exist(e))
            return false;
        // Store in express cabinet
        while (true){
            Random random = new Random();
            int i = random.nextInt(10);
            int j = random.nextInt(10);
            if(ExpressMsg.express[i][j] == null) {
                ExpressMsg.express[i][j] = e;
                printLine(e, i, j);
                break;
            }
        }

        ExpressMsg.count++;
        return true;
    }

    /**
     * 2-Delete the express operation, pass in the express object, and judge whether the document number exists
     * @param e Express class object to find out whether the express exists
     * @return true is returned for successful deletion and false is returned for failure
     */
    public boolean delete(Express e){
        x:for (int i = 0;i < 10;i++){
            for (int j = 0;j < 10;j++){
                if(e.equals(ExpressMsg.express[i][j])) {
                    // Save a copy of data before deleting
                    {
                    recycle = ExpressMsg.express[i][j];
                    line = i;
                    row = j;
                    }
                    ExpressMsg.express[i][j] = null;
                    break x;
                }
            }
        }
        ExpressMsg.count--;
        int a;
        return true;

    }

    /**
     * 3-Modify the express operation and pass in two express objects to judge whether the document number exists
     * @param e0 Express to delete
     * @param e1 Modified Express
     * @return true will be returned if the modification is successful, and false will be returned if the modification fails
     */
    public boolean update(Express e0,Express e1){
        //Determine whether express delivery exists, which can be deleted or modified
        if (!delete(e0)){
            return false;
        }
        return add(e1);
    }

    /**
     * 4-View all express operations
     * @return If it is empty, false will be returned; if it is empty, express delivery will be printed
     */
    public boolean findAll(){
        if (ExpressMsg.count == 0){
            return false;
        }
        System.out.println("The express information is as follows, in total"+ExpressMsg.count+"Article:");
        printAll(ExpressMsg.express);
        return true;
    }

    /**
     * Common user interface, requiring input of pick-up code
     * @return Return true if the retrieval is successful, and return false if the input retrieval code fails
     */
    public int user(){
        int a = -1;
        System.out.println("Please enter your six digit pickup Code:");
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        try{
            a = Integer.parseInt(s);
            if(a < 100000 || a > 999999){
                Exception e = new Exception();
                throw e;
            }
        }catch (Exception e){
            return -1;
        }
        return a;
    }

    /**
     * Query whether a courier exists
     * @param e Express object
     * @return true if it exists; false if it does not exist
     */
    public boolean exist(Express e){
        for (int i = 0;i < 10;i++){
            for (int j = 0;j < 10;j++){
                if(e.equals(ExpressMsg.express[i][j])){
                    printLine(ExpressMsg.express[i][j],i,j);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Called when the express cannot be found
     */
    public void notFound(){
        System.out.println("Sorry, there is no express you inquired about in the cabinet");
    }

    /**
     * Used to print an express message
     * @param e Express object
     * @param i Number of rows
     * @param j Number of columns
     */
    public void printLine(Express e,int i,int j){
        System.out.println("The first"+i+"OK, page"+j+"column\n"
                +"Express order No.:"+e.getNumber()+",Company name:"+e.getCompany()+",The piece code is:"+e.getCode());
    }

    /**
     * It is used to view all express method calls and print all express information
     */
    public void printAll(Express[][] e){
        for(int i = 0;i < 10;i++){
            for(int j = 0;j < 10;j++){
                if (e[i][j] != null){
                    printLine(e[i][j],i,j);
                }
            }
        }
    }


    public boolean goNext(){
        int a = -1;
        System.out.println("Your current operation is irreversible! Please choose whether to continue:");
        System.out.println("1-Continue operation");
        System.out.println("0-Cancel operation");
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        try {
            a = Integer.parseInt(s);
            if (a != 1) {
                Exception e = new Exception();
                throw e;
            }
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * Enter order number information
     * @return
     */
    public Express inputMsg(){
        System.out.println("Please enter the courier number:");
        Scanner inputNum = new Scanner(System.in);
        String num = inputNum.nextLine();
        Express e = new Express(num,null);
        return e;
    }

    /**
     * Recover data
     */
    public void resume(){
        ExpressMsg.express[line][row] = recycle;
    }

    /**
     * Print operation failed
     */
    public void fail(){
        System.out.println("Operation failed!");
    }

    /**
     * Print operation succeeded
     */
    public void success(){
        System.out.println("Congratulations, successful operation!");
    }

    /**
     * Express cabinet full
     */
    public void full(){
        System.out.println("The express cabinet is full!");
    }

    /**
     * Express cabinet empty
     */
    public void empty(){
        System.out.println("There are no objects in the express cabinet!");
    }

    /**
     * Welcome statement
     */
    public void welcome(){
        System.out.println("Welcome to the express access cabinet in the community");
    }

    /*
     * Print at end of run
     */
    public void bye(){
        System.out.println("Thank you for your use. Bye!");
    }
}

3.2 code of data part

3.2.1 writing Express

The express class contains three private attributes: express order number, company name and pick-up code
The methods include the construction method for accessing express, the method for generating random number pick-up code, and the express comparison method for rewriting equals.

package data;

import java.util.Objects;
import java.util.Random;

public class Express {
    // courier number
    private String number;
    // corporate name
    private String company;
    // Pick up code
    private int code;

    // Nonparametric construction method
    public Express() {
    }

    /**
     * One parameter construction method is only used when taking parts
     * @param code
     */
    public Express(int code) {
        this.code = code;
    }

    // Two parameter construction method
    public Express(String number, String company) {
        this.number = number;
        this.company = company;
        this.code = setCode();
    }

    public String getNumber() {
        return number;
    }

    public String getCompany() {
        return company;
    }

    public int getCode() {
        return code;
    }

    /**
     * Generate six digit random number retrieval code
     * @return Return value six digit pickup code
     */
    public int setCode(){
        Random random = new Random();
        int a = random.nextInt(900000)+100000;
        return a;
    }


    public void setCompany(String company){
        this.company = company;
    }

    /**
     * Rewrite the equals method. If the express order number or pick-up code is the same, it is considered to be the same express
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Express express = (Express) o;
        return code == express.code ||
                Objects.equals(number, express.number);
    }


}

3.2.2 realize storage function

Set the express cabinet as M rows and N columns, so the express stored in the express cabinet can be represented by an array of M*N, and the initial express quantity is 0

package data;

import java.util.Scanner;

public class ExpressMsg {
    /**
     * The total number of rows, express objects and quantity of express cabinets,
     */
    private static final int M = 10;
    private static final int N = 10;
    public static Express[][] express = new Express[M][N];
    public static int count = 0;


}

3.3 source code of scheduling part

By receiving the results of identity, operation, additions and deletions, the stored data is processed. After processing, the processing method is invoked to prompt the user's operation. After the operation is finished, it will enter the choice of the next round of identity or operation.

package data;

import show.View;

import java.util.Scanner;

public class Judge {
    //Used to judge

    private Express exp = new Express();
    private View v = new View();

    /**
     * Method of judging identity
     */
    public void id(){
        w:while (true){
            int m = v.menu();
            switch(m){
                case 1:{//courier
                    int o = 0;
                    do{
                        o = v.operation();
                        this.select(o);
                    }while (o != 0);
                }break;
                case 2:{//Ordinary users
                    int u = v.user();
                    if(u == -1){
                        System.out.println("Invalid verification code entered");
                    }else{
                        Express ex = new Express(u);
                        if(v.exist(ex)){
                            v.delete(ex);
                            v.success();
                        }else {
                            v.notFound();
                        }
                    }
                }break;
                case 0:{//Exit and jump out of the while loop
                }break w;
            }
        }
    }

    /**
     * Judge courier operation
     * @param b
     */
    public void select(int b){
        switch (b){
            case 1:{//Increase express delivery
                if(ExpressMsg.count == 100){
                    v.full();
                }else {
                    putIn(exp);
                }
            }break;
            case 2:{//Delete Express
                if(ExpressMsg.count == 0){
                    v.empty();
                }else{
                    deleteGoods();
                }
            }break;
            case 3:{//Modify Express
                if(ExpressMsg.count == 0){
                    v.empty();
                }else{
                    reFlash();
                }
            }break;
            case 4:{//View all
                if(ExpressMsg.count == 0){
                    v.empty();
                }else{
                    v.findAll();
                }
            }break;
            case 0:{//sign out
            }break;

        }
    }



    /**
     * Judge whether the pick-up code is correct
     * @param d
     */
    public boolean code(int d){
        Express expr = new Express(d);
        return v.exist(expr);
    }

    /**
     * This method is used for express delivery
     * @param exp Express to be deposited
     * @return
     */
    public boolean putIn(Express exp){
        exp = v.inputMsg();
        if(v.exist(exp)){
            System.out.println("The express order number already exists, please check your input or do not store it repeatedly");
            v.fail();
            return false;
        }else {
            System.out.println("Please enter company name:");
            Scanner inputCom = new Scanner(System.in);
            String com = inputCom.nextLine();
            exp.setCompany(com);
            v.add(exp);
            v.success();
            return true;
        }
    }

    /**
     * Delete Express
     */
    public void deleteGoods(){
        System.out.println("About to delete");
        exp =  v.inputMsg();
        if (v.exist(exp)){
            if(v.goNext()){
                v.delete(exp);
                v.success();
            }else {
                System.out.println("The delete operation was cancelled");
            }
        }else {
            v.notFound();
            v.fail();
        }
    }

    /**
     * Modify Express
     */
    public void reFlash(){
        System.out.println("About to modify");
        exp =  v.inputMsg();
        if(v.exist(exp)){
            if(v.goNext()){
                v.delete(exp);
                // Prevent unsuccessful modification and accidental deletion of express information
                if (this.putIn(exp)){
                    ;//Correct input, no operation here
                }else {
                    // Input error, restore the original data
                    v.resume();
                }
            }else {
                System.out.println("Modification cancelled");
            }
        }else {
            v.notFound();
        }
    }

}

The following is the main method, program entry part

package method;

import data.Express;
import data.ExpressMsg;
import data.Judge;
import show.View;

public class Main {
    public static void main(String[] args) {
        View v = new View();
        Judge g = new Judge();
        v.welcome();
        g.id();
        v.bye();
    }
}

Thanks for watching!

Keywords: Java

Added by justAnoob on Wed, 09 Feb 2022 15:22:44 +0200