Xiaolang discovered the secret of the express cabinet (MVC version)

Realize community express management

insert image description here

MVC

Model View Controller

Hierarchy

The layers are clear. Generally, Xiaolang likes to start from the main function, but the obsessive-compulsive disorder of Piaohong is quick to dodge.
insert image description here

bean package (stores objects)

package work.february.two.bean;

import java.util.Objects;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 10:32
 * @Modified By:
 */
public class Express {
    
    
    /**
     * 快递公司
     * 快递单号
     * 收件人手机号
     * 取件码
     */
    private String company;
    private String number;
    private String phoneNumber;
    private int extractionCode;
    public Express() {
    
    
    }

    public Express(String company, String number, String phoneNumber, int extractionCode) {
    
    
        this.company = company;
        this.number = number;
        this.phoneNumber = phoneNumber;
        this.extractionCode = extractionCode;
    }

    public int getExtractionCode() {
    
    
        return extractionCode;
    }

    public void setExtractionCode(int extractionCode) {
    
    
        this.extractionCode = extractionCode;
    }

    public String getCompany() {
    
    
        return company;
    }

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

    public String getNumber() {
    
    
        return number;
    }

    public void setNumber(String number) {
    
    
        this.number = number;
    }

    public String getPhoneNumber() {
    
    
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
    
    
        this.phoneNumber = phoneNumber;
    }



    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) {
    
    
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
    
    
            return false;
        }
        Express exress = (Express) o;
        return extractionCode == exress.extractionCode && Objects.equals(company, exress.company) && Objects.equals(number, exress.number) && Objects.equals(phoneNumber, exress.phoneNumber);
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(company, number, phoneNumber, extractionCode);
    }

    @Override
    public String toString() {
    
    
        return "Exress{" +
                "company='" + company + '\'' +
                ", number='" + number + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", extractionCode=" + extractionCode +
                '}';
    }
}

control package

package work.february.two.control;


import work.february.two.bean.Express;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 10:33
 * @Modified By:
 */
public class ExpressControl {
    
    
    Random random =new Random();
    private ArrayList<Express> data =new ArrayList<>();
     /**
     * 存储快递
     * @param express
     */
    public void addExpress(Express express) {
    
    
        /**
         * 生成随机数
         */
        int code ;
       p: while (true){
    
    
           code = random.nextInt(900000)+100000;
          for (int i = 0;i < data.size();i++){
    
    
              if(code == data.get(i).getExtractionCode()){
    
    
                  //随机产生的取件码重复时,重复生成
                  continue p;
              }
          }
          break ;
       }
       //设置取件码
        express.setExtractionCode(code);
        //将快递存储到集合中
        data.add(express);
        System.out.println("快递录入成功,取件码:"+code);
    }

    /**
     * 取出快递
     * @param extractionCode
     * @return
     */
    public Express getExpress(int extractionCode) {
    
    
        for (int i = 0;i < data.size();i++){
    
    
            if (extractionCode == data.get(i).getExtractionCode()){
    
    
                //如果有,则取出并从集合中删除
                return data.remove(i);
            }
        }
        //如果没有.则返回空
        return null;
    }

    public List<Express> findAll() {
    
    
        return data;
    }
}

view package

package work.february.two.view;





import work.february.two.bean.Express;

import java.util.List;
import java.util.Scanner;

/**
 * @Author: 杜英杰
 * @Description:
 * @Date Created in 2021-02-02 10:34
 * @Modified By:
 */
public class Views {
    
    
    static   Scanner scanner =new Scanner(System.in);

    /**
     * 主菜单
     * @return 功能按键
     */
    public static int mainMenu() {
    
    
        System.out.println("请根据提示,输入功能序号:");
        System.out.println("1.快递录入:");
        System.out.println("2.快递取出:");
        System.out.println("3.查看所有:");
        System.out.println("0.程序退出:");
        int num =-1;
        try {
    
    
            num = scanner.nextInt();
        }catch (Exception e){
    
    
        }
        if(num<0||num>3){
    
    
            System.out.println("你的输入有误,请重新输入!");
            return mainMenu();
        }
       return num;
    }

    /**
     * 欢迎界面
     */
    public void welcome() {
    
    
        System.out.println("欢迎使用快递管理系统");
    }

    /**
     * 录入快递界面
     * @return 快递信息
     */
    public Express inMenu() {
    
    
        System.out.println("请根据提示进行快递录入:");
        System.out.println("请输入快递公司:");
        String company =scanner.next();
        System.out.println("请输入快递单号:");
        String expressNumber =scanner.next();
        System.out.println("请输入收件人手机号码");
        String phoneNumber =scanner.next();
        //将输入的快递内容组成成一个快递对象
        Express express =new Express();
        express.setCompany(company);
        express.setNumber(expressNumber);
        express.setPhoneNumber(phoneNumber);
        return express;
    }

    /**
     * 取件界面
     * @return 返回取件码
     */
    public int outMenu() {
    
    
        System.out.println("请输入六位数取件码:");
        int code = -1;
        try {
    
    
            code = scanner.nextInt();
        }catch (Exception e){
    
    

        }
        if (code<100000||code>999999){
    
    
            System.out.println("输入有误!");
            return outMenu();
        }
        return code;
    }


    /**
     * 用于显示取件的快递信息
     *
     * @param express 用于显示的快递对象
     */
    public void printExpress(Express express) {
    
    
        if (express == null){
    
    
            System.out.println("取件码不存在,即将返回主界面");
        }else{
    
    
            System.out.println("取件成功");
            System.out.println("快递公司:"+express.getCompany()+",快递单号:"+express.getNumber()+",收件人手机号码:"+express.getPhoneNumber()+",取件码:"+express.getExtractionCode());
        }
    }

    /**
     * 显示所有快递的视图方法
     * @param data
     */
    public void printAll(List<Express> data) {
    
    
        if (data.size() == 0){
    
    
            System.out.println("暂无快递,即将回到主页");
        }else{
    
    
            System.out.println("快递信息如下:");
            for (int i =0 ;i < data.size();i++){
    
    
                Express express =data.get(i);
                System.out.println("快递公司:"+express.getCompany()+",快递单号:"+express.getNumber()+",收件人手机号码:"+express.getPhoneNumber()+",取件码:"+express.getExtractionCode());
            }
        }
    }
    /**
     * 登录验证
     */
    private boolean Password(){
    
    
        System.out.println("请输入权限密码:(输入错误返回上层目录)");
        String password =scanner.next();
        //内置密码是"1234"
        return "1234".equals(password);
    }

    /**
     * bye - bye
     */
    public void bye() {
    
    
        System.out.println("系统已经退出!");
    }
}

main package

package work.february.two.main;


import work.february.two.bean.Express;
import work.february.two.control.ExpressControl;
import work.february.two.view.Views;

import java.util.List;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 10:33
 * @Modified By:
 */
public class main {
    
    

    public static void main(String[] args) {
    
    
        Views view =new Views();
        ExpressControl control=new ExpressControl();
        //欢迎界面
        view.welcome();
       m: while(true){
    
    
        int menuKey=Views.mainMenu();
       switch (menuKey){
    
    
           case 0:
               //退出程序
               break m;
           case 1: {
    
    
               //录入快递的视图
               Express express = view.inMenu();
               //调度层进行存储
               control.addExpress(express);
               break;
           }
           case 2:{
    
    
               //取出快递 提示用户输入取件码
               int extractionCode = view.outMenu();
               //根据取件码从control中取出快递数据
               Express express =control.getExpress(extractionCode);
               //将取到的数据进行打印
               view.printExpress(express);
               break ;
           }
           case 3:{
    
    
               //显示所有快递
               List<Express> data = control.findAll();
               view.printAll(data);
           }
           default:
               break ;
       }
    }
    //再见页面
        view.bye();
    }
}

insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324278907&siteId=291194637