快递管理控制台简易版——数组存储版(Java)


具体需求

采用二维数组的方式,存储快递,在管理控制台实现快递的存取功能

  • 管理员
    -快递录入
    …柜子位置(系统产生,不能重复)
    …快递单号(输入)
    …快递公司(输入)
    …6位取件码(系统产生,不能重复)
    -删除快递(根据单号)
    -修改快递(根据单号)
    -查看所有快递(遍历)
  • 普通用户
    -快递取出
    …输入取件码:显示快递信息和柜子位置,从柜子中移除快递

任务过程

  • 明确需求,使用面向对象的思想创建快递类,包含快递位置,单号,公
    司,取件码等
  • 使用二维数组保存快递信息,建议数组第一个位置为柜子位置,第二
    个位置为快递对象
  • 根据任务需求,定义相关方法(新增快递,删除快递,修改快递,查看
    所有快递)
  • 创建用户类,定义角色属性,用以区分管理员和普通用户
  • 测试:使用不同用户角色切换,按照任务概述实现功能测试和验收

思路及代码实现

  • view:视图分析
    dao:数据存取
    exception:调度逻辑

一、对象

创建一个pojo包,新建类Express()(快递类)和 Location()(存储坐标,即在快递柜中的位置)

Express(快递)

public class Express {
    private String number;//快递单号
    private String company;//公司
    private int code;//取件码
    
	//快递在快递柜中的坐标
    private int x;//x,即所在快递柜的排数
    private int y;//y,即所在快递柜的列数

    /**
     * 无参构造方法
     */
    public Express() {
    }

    /**
     * 全参构造方法
     * @param number
     * @param company
     */
    public Express(String number, String company ,int code,int x,int y) {
        this.number = number;
        this.company = company;
        this.code = code;
        this.x = x;
        this.y = y;
    }
	/**
	*以下setter和setter方法分别用来设置属性的值以及获取属性的值
	*/
	//x,所在快递柜的排数
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

	//y所在快递柜的列数
    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

	//快递单号
    public String getNumber() {
        return number;
    }

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

	//快递公司
    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }
	
	//取件码
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
	
	/**
	*重写equals方法,比较快递单号
	*/
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Express express = (Express) o;
        return number.equals(express.number);
    }

    @Override
    public int hashCode() {
        return Objects.hash(number);
    }

	/**
	*重写toString方法,返回快递信息
	*/
    @Override
    public String toString() {
        return getNumber() + '-' + getCompany() + '-' + getCode() ;
    }
}

Location(快递的位置)

package com.company.project.pojo;

public class Location {
    private static int x;
    private static int y;
    /**
     * 无参构造方法
     */
    public Location() {
    }
    
	/**
	*以下setter和setter方法分别用来设置属性的值以及获取属性的值
	*/
	
	//下标x,对应快递柜的排数
    public static int getX() {
        return x;
    }

    public static void setX(int x) {
        Location.x = x;
    }

	//下标y,对应快递柜的列数
    public static int getY() {
        return y;
    }

    public static void setY(int y) {
        Location.y = y;
    }
}

二、自定义异常

创建一个exception包,新建OutNumberBoundException

public class OutNumberBoundException extends Throwable {
    public OutNumberBoundException(String s) {
        super(s);
    }
}

三、视图分析

  • 主要负责输入输出的视图交互模块

创建一个view包,新建类View(视图类)

View(视图分析)

  • 编写进入【welcome()】和退出【bye()】控制台的函数
    /**
     * 进入系统
     */
    public  static void welcome(){
        System.out.println("欢迎进入快递管理系统!");
    }

    /**
     * 退出系统
     */
    public static void bye(){
        System.out.println("感谢使用快递管理系统!");
    }
  • 编写**【validNum()】**,判断输入是否为有效数字、是否在有效范围内
    /**
     * 判断输入是否为数字、是否在有效范围内
     * @param s
     * @param begin
     * @param end
     * @return
     * @throws NumberFormatException
     * @throws OutNumberBoundException
     */
    private static int validNum(String s,int begin,int end) throws NumberFormatException,OutNumberBoundException{
        try{
            int num = Integer.parseInt(s);
            if (num < begin || num > end){
                throw new OutNumberBoundException("数字的范围必须在" + begin + "和" + end +"之间");
            }
            return num;
        }catch(NumberFormatException e){
            throw new NumberFormatException("输入的必须是数字!");
        }
    }

进入主界面

编写主界面【mainMenu()】,通过输入来进入对应的操作人员界面
1.管理员
2.普通用户
0.退出

  • 由于输入012三位数才有效,其它值均无效,因此可以自定义函数【vaildNum()】来处理输入、捕获异常,
  • 当输入的值不为012或输入为字符时,输出异常并重新输入,因此用while循环进行输入,当输入值符合时break;
    返回输入的值
 /**
     * 主菜单,系统界面
     * @return
     */
    public static int mainMenu(){
        int mainNum = 0;
        do{
            System.out.println("----------欢迎使用快递管理系统----------");
            System.out.println("请选择您的身份:");
            System.out.println("1.管理员");
            System.out.println("2.普通用户");
            System.out.println("0.退出");
            String s = input.nextLine();
            try{
                mainNum = validNum(s,0,2);
                break;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
        return mainNum;
    }
1——进入管理员界面

编写用户界面【managerMain()】,通过输入来进入对应的功能界面
1.录入快递
2.删除快递
3.修改快递
4.查看所有快递
0.返回上一级界面)

  • 由于输入01234五位数才有效,其它值均无效,因此可以自定义函数【vaildNum()】来处理输入、捕获异常
  • 当输入的值不为01234或输入为字符时,输出异常并重新输入,因此用while循环进行输入,当输入值符合时break;
    返回输入的值
 /**
     * 管理员菜单
     */
    public static int managerMain(){
        int managerNum = 0;
        do{
            System.out.println("尊敬的管理员,您好!");
            System.out.println("请选择您要进行的操作:");
            System.out.println("1.录入快递");
            System.out.println("2.删除快递");
            System.out.println("3.修改快递");
            System.out.println("4.查看所有快递");
            System.out.println("0.返回上一级界面");
            String s = input.nextLine();
            try{
                managerNum = validNum(s,0,4);
                break;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
       return managerNum;
    }


1-录入快递

录入快递【insetExpress()】,根据提示进行输入

  • 输入快递单号
    输入快递公司
  • 创建对象e,将快递单号和快递公司写入
  • 返回对象e
  /**
     * 录入快递
     */
    public static Express insertExpress(){
        System.out.print("请输入快递单号:");
        String number = input.nextLine();
        System.out.print("请输入快递公司:");
        String company = input.nextLine();
        Express e = new Express();
        e.setNumber(number);
        e.setCompany(company);
        return e;
    }
  • 输入快递单号
    /**
     * 输入快递单号
     */
    public static String findByNumber(){
        System.out.print("请输入快递单号:");
        String s = input.nextLine();
        return s;

    }
2-删除快递

删除快递【deleteExpress()】,通过输入来进入对应的功能界面
1.确认删除
0.取消操作

  • 由于输入01两位数才有效,其它值均无效,因此可以自定义函数【vaildNum()】来处理输入、捕获异常
  • 当输入的值不为01或输入为字符时,输出异常并重新输入,因此用while循环进行输入,当输入值符合时break;
    返回输入的值
/**
     * 删除快递
     */
    public static int deleteExpress() throws NumberFormatException,OutNumberBoundException{

        do{
            System.out.println("请根据提示进行操作!");
            System.out.println("是否进行删除操作?");
            System.out.println("1.确认删除");
            System.out.println("0.取消操作");
            String s = input.nextLine();
            int deleteNum = -1;
            try{
                deleteNum = validNum(s,0,1);
                return deleteNum;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
    }
3-修改快递

修改快递【updateExpress()】,根据提示进行输入

  • 输入新的快递单号
    输入新的快递公司
  • 创建对象e,将快递单号和快递公司写入
  • 返回对象e
    /**
     * 修改快递
     */
    public static Express updateExpress(){
        Express e = new Express();
        System.out.print("请输入新的快递单号:");
        String number = input.nextLine();
        System.out.print("请输入新的快递公司:");
        String company = input.nextLine();
        e.setNumber(number);
        e.setCompany(company);
        return e;
    }
4-查看所有快递

查看所有快递

  • 【printAllExpress(Express[][] e)】遍历
  • 如果非空
    【 printExpress(e[i][j])】,打印单个快递信息
    【printExpressLocation(e[i][j])】,输出快递在快递柜中的位置
 /**
     * 打印快递信息,即遍历
     */
    public static void printAllExpress(Express[][] e){
        for(int i = 0;i < 10;i ++){
            for(int j =0;j < 10;j ++){
                if (e[i][j] != null){
                    printExpress(e[i][j]);
                    printExpressLocation(e[i][j]);
                    System.out.println();
                }
            }
        }
    }
 /**
     * 打印单条快递信息
     */
    public static void printExpress(Express e){
        System.out.println("快递信息[" +
                "快递单号:" + e.getNumber() + ' ' +
                ", 快递公司:" + e.getCompany() + ' ' +
                ", 取件码:" + e.getCode() +
                ']');
    }
    /**
     *打印快递位置信息
     */
    public static void printExpressLocation(Express e){
        System.out.println("快递存储在快递柜的第" + (e.getX() + 1) + "排,第" + (e.getY() + 1) + "列!");
    }
2——进入用户界面

编写用户主菜单【userMain()】,通过输入来进入对应的操作人员界面
1.取出快递
0.返回上一界面

  • 由于输入01两位数才有效,其它值均无效,因此可以自定义函【vaildNum()】来处理输入、捕获异常
  • 当输入的值不为01或输入为字符时,输出异常并重新输入,因此用while循环进行输入,当输入值符合时break;
    返回输入的值
    /**
     * 用户菜单
     */
    public static int userMain(){
        int userNum = 0;
        do{
            System.out.println("尊敬的用户,您好!");
            System.out.println("请选择您要进行的操作:");
            System.out.println("1.取出快递");
            System.out.println("0.返回上一级界面");
            String s = input.nextLine();
            try{
                userNum = validNum(s,0,1);
                break;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
        return userNum;
    }
  • 输入取件码
    /**
     * 输入取件码
     */
    public static int findByCode(){
        int code = -1 ;
        do{
            System.out.print("请输入取件码:");
            String s = input.nextLine();
            try{
                code = validNum(s,100000,999999);
                break;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
        return code;
    }
  • 其它操作:
    /**
     * 已经存在
     */
    public static void hasExist(){
        System.out.println("该单号已经存在!添加快递失败!");
    }
    /**
     * 快递不存在
     */
    public static void noExist(){
        System.out.println("快递不存在!");
    }
    /**
     * 操作成功
     */
    public static void success(){
        System.out.println("操作成功!");
    }

四、数据存取

主要负责数据处理的模块

创建一个dao包,新建类ExpressDao

  • 返回快递柜的全部快递
    /**
     *返回快递柜的全部快递
     */
     public Express[][] getExpress(){
        return data;
     }
  • 产生取件码并判断是否存在
 /**
     * 产生取件码
     * @return
     */
     public static int randomCode(){
        int code = 0;
        do{
            code = r.nextInt(900000) + 100000;
        }while(isExist(code));//如果取件码存在则一直循环知道产生新的不重复的取件码
        return code;
     }
    /**
     * 判断是否已经存在这个取件码
     */
     public static boolean isExist(int code){
        for(int i = 0;i < 10;i ++){
            for(int j = 0;j < 10;j++){
                if(data[i][j] !=null && data[i][j].getCode() == code){
                    return true;
                }
            }
        }// end for
         return false;
     }
  • 通过快递单号查询是否存在
/**
     * 通过快递单号查询是否存在
     */
    public static Express findByNumber(String number){
        Express e = new Express();
        e.setNumber(number);
        for(int i = 0;i < 10;i ++){
            for(int j = 0;j < 10;j++){
                if(data[i][j] != null && e.equals(data[i][j])){
                    return data[i][j];//存在
                }
            }
        }// end for
        return null;
    }
  • 通过取件码查询是否存在

    /**
     * 通过取件码查询是否存在
     */
    public static Express findByCode(int code){
        Express e = new Express();
        e.setCode(code);
        for(int i = 0;i < 10;i++){
            for(int j = 0;j < 10;j ++){
                if(data[i][j] != null && data[i][j].getCode() == code){
                    return data[i][j];
                }
            }
        }//end for
        return null;
    }
  • 添加快递
  /**
     * 添加快递
     */
    public static Express addExpress(Express e){
        if(size == 100){
            System.out.println("快递柜已满!");
            return null;
        }
        Location l = randomLocation();//获取坐标
        e.setCode(randomCode());
        size ++;
        data[l.getX()][l.getY()] = e;
        e.setX(l.getX());
        e.setY(l.getY());
        return data[l.getX()][l.getY()];
    }
  • 删除快递
/**
     *删除快递
     */
    public static void moveExpress(Express e){
       p: for(int i = 0;i < 10;i ++){
            for(int j = 0;j < 10;j ++){
                if(data[i][j] != null && e.equals(data[i][j])){
                    data[i][j] = null;
                    break p;
                }
            }
        }
    }
  • 修改快递信息
 /**
     *修改快递信息
     */
    public static void updateExpress(Express oldExpress,Express newExpress){
        moveExpress(oldExpress);
        addExpress(newExpress);
    }
  • 生成坐标
    /**
     * 生成坐标
     */
    public static Location randomLocation(){
        do{
            location.setX(r.nextInt(10));
            location.setY(r.nextInt(10));
            if(data[location.getX()][location.getY()] == null){//当这个位置没有存储快递,返回该位置
                return location;
            }
        }while(true);//循环,避免位置重复
    }



五、主界面

主界面

 public static void main(String[] args) throws OutNumberBoundException {
        v.welcome();

     m: while(true){
            int mainNum = v.mainMenu();//调用主菜单
            switch(mainNum){
                case 0://结束使用
                    break m;
                case 1://进入管理员平台
                    managerPlatform();
                    break ;
                case 2://进入用户平台
                    userPlatform();
                    break ;
            }
        }// end while

        v.bye();
    }//end main

管理员操作界面

 /**
     * 管理员操作界面
     */
    public static void managerPlatform() throws OutNumberBoundException {
      w:while(true){
            int managerNum = v.managerMain();
            switch(managerNum){
                case 0:{//返回上一级页面
                    return;
                }
                case 1:{//1.录入快递
                    insert();
                }
                break w;
                case 2:{//2.删除快递
                    delete();
                }
                break w;
                case 3:{//3.修改快递
                    update();
                }
                break w;
                case 4:{//4.查看所有快递
                    printAll();
                }
                break w;
            }// end switch
        }//end while

    }//end managerPlatform
  • 录入快递
  /**
     * 录入快递
     */
    public static void insert(){
        Express e1 = v.insertExpress();//输入快递信息
        Express e2 = dao.findByNumber(e1.getNumber());//通过快递单号查询是否存在
        if (e2 == null){//此快递柜为空,add,生成取件码
            e2 = dao.addExpress(e1);
            v.printExpress(e2);
            v.printExpressLocation(e2);
            v.success();
        }else{
            v.hasExist();
        }
    }
  • 删除快递
  /**
     * 删除快递
     */
    public static void delete() throws OutNumberBoundException {
        String num = v.findByNumber();
        Express e = dao.findByNumber(num);
        if( e == null){//快递不存在
            v.noExist();
        }else {
            int deleteNum =v.deleteExpress();
            if (deleteNum == 1){//确认删除
                dao.moveExpress(e);
                System.out.println("删除成功!");
                v.success();
            }else if (deleteNum == 0){//取消删除
                System.out.println("取消操作成功!");
            }
        }
    }
  • 修改快递
  /**
     * 修改快递
     */
    public static void update(){
        String num = v.findByNumber();//输入快递单号
        Express e1 = dao.findByNumber(num);//通过快递单号查找快递是否存在
        if( e1 == null){//快递不存在
            v.noExist();
        }else {
            Express e2 = v.updateExpress();
            dao.updateExpress(e1,e2);
            System.out.println("快递信息更新成功!");
            v.printExpress(e2);
            v.printExpressLocation(e2);
            v.success();
        }
    }
  • 查看所有快递
    /**
     * 查看所有快递
     */
    public static void printAll(){
        v.printAllExpress(dao.getExpress());
    }

用户操作界面

    /**
     * 用户操作界面
     */
    public static void userPlatform() throws OutNumberBoundException{
//        w:while(true){
//            int userNum = v.userMain();
//            switch(userNum){
//                case 0:{//返回上一级页面
//                    v.mainMenu();
//                    break w;
//                }
//                case 1:{//1.取出快递
                    int code = v.findByCode();
                    Express e = dao.findByCode(code);
                    if(e == null){//??????取件码有误,重新输入
                        v.noExist();
                    }else {
                        v.printExpress(e);
                        dao.moveExpress(e);
                        v.printExpressLocation(e);
                        System.out.println("取件成功!");
                    }
//                }
//                break w;
//            }// end switch
//        }//end while

    }//end userPlatform

完整代码

一、 对象

Express

public class Express {
    private String number;//快递单号
    private String company;//公司
    private int code;//取件码
    private int x;
    private int y;

    /**
     * 无参构造方法
     */
    public Express() {
    }

    /**
     * 全参构造方法
     * @param number
     * @param company
     */
    public Express(String number, String company ,int code,int x,int y) {
        this.number = number;
        this.company = company;
        this.code = code;
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getNumber() {
        return number;
    }

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

    public String getCompany() {
        return company;
    }

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

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Express express = (Express) o;
        return number.equals(express.number);
    }

    @Override
    public int hashCode() {
        return Objects.hash(number);
    }

    @Override
    public String toString() {
        return getNumber() + '-' + getCompany() + '-' + getCode() ;
    }
}

Location

public class Location {
    private static int x;
    private static int y;

    public Location() {
    }

    public static int getX() {
        return x;
    }

    public static void setX(int x) {
        Location.x = x;
    }

    public static int getY() {
        return y;
    }

    public static void setY(int y) {
        Location.y = y;
    }
}

二、 自定义异常

public class OutNumberBoundException extends Throwable {
    public OutNumberBoundException(String s) {
        super(s);
    }
}

三、 视图分析

public class View {
    public static Scanner input = new Scanner(System.in);
    public static ExpressDao dao = new ExpressDao();
    public static Location location = new Location();

    /**
     * 进入系统
     */
    public  static void welcome(){
        System.out.println("欢迎进入快递管理系统!");
    }

    /**
     * 退出系统
     */
    public static void bye(){
        System.out.println("感谢使用快递管理系统!");
    }

    /**
     * 主菜单,系统界面
     * @return
     */
    public static int mainMenu(){
        int mainNum = 0;
        do{
            System.out.println("----------欢迎使用快递管理系统----------");
            System.out.println("请选择您的身份:");
            System.out.println("1.管理员");
            System.out.println("2.普通用户");
            System.out.println("0.退出");
            String s = input.nextLine();
            try{
                mainNum = validNum(s,0,2);
                break;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
        return mainNum;
    }

    /**
     * 判断输入是否为数字、是否在有效范围内
     * @param s
     * @param begin
     * @param end
     * @return
     * @throws NumberFormatException
     * @throws OutNumberBoundException
     */
    private static int validNum(String s,int begin,int end) throws NumberFormatException,OutNumberBoundException{
        try{
            int num = Integer.parseInt(s);
            if (num < begin || num > end){
                throw new OutNumberBoundException("数字的范围必须在" + begin + "和" + end +"之间");
            }
            return num;
        }catch(NumberFormatException e){
            throw new NumberFormatException("输入的必须是数字!");
        }
    }

    /**
     * 管理员菜单
     */
    public static int managerMain(){
        int managerNum = 0;
        do{
            System.out.println("尊敬的管理员,您好!");
            System.out.println("请选择您要进行的操作:");
            System.out.println("1.录入快递");
            System.out.println("2.删除快递");
            System.out.println("3.修改快递");
            System.out.println("4.查看所有快递");
            System.out.println("0.返回上一级界面");
            String s = input.nextLine();
            try{
                managerNum = validNum(s,0,4);
                break;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
       return managerNum;
    }

    /**
     * 用户菜单
     */
    public static int userMain(){
        int userNum = 0;
        do{
            System.out.println("尊敬的用户,您好!");
            System.out.println("请选择您要进行的操作:");
            System.out.println("1.取出快递");
            System.out.println("0.返回上一级界面");
            String s = input.nextLine();
            try{
                userNum = validNum(s,0,1);
                break;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
        return userNum;
    }

    /**
     * 录入快递
     */
    public static Express insertExpress(){
        System.out.print("请输入快递单号:");
        String number = input.nextLine();
        System.out.print("请输入快递公司:");
        String company = input.nextLine();
        Express e = new Express();
        e.setNumber(number);
        e.setCompany(company);
        return e;
    }

    /**
     * 删除快递
     */
    public static int deleteExpress() throws NumberFormatException,OutNumberBoundException{

        do{
            System.out.println("请根据提示进行操作!");
            System.out.println("是否进行删除操作?");
            System.out.println("1.确认删除");
            System.out.println("0.取消操作");
            String s = input.nextLine();
            int deleteNum = -1;
            try{
                deleteNum = validNum(s,0,1);
                return deleteNum;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
    }

    /**
     * 更新快递
     */
    public static Express updateExpress(){
        Express e = new Express();
        System.out.print("请输入新的快递单号:");
        String number = input.nextLine();
        System.out.print("请输入新的快递公司:");
        String company = input.nextLine();
        e.setNumber(number);
        e.setCompany(company);
        return e;
    }

    /**
     * 输入快递单号
     */
    public static String findByNumber(){
        System.out.print("请输入快递单号:");
        String s = input.nextLine();
        return s;

    }

    /**
     * 输入取件码
     */
    public static int findByCode(){
        int code = -1 ;
        do{
            System.out.print("请输入取件码:");
            String s = input.nextLine();
            try{
                code = validNum(s,100000,999999);
                break;
            }catch(NumberFormatException | OutNumberBoundException e){
                System.out.println(e.getMessage());
            }
        }while(true);
        return code;
    }

    /**
     * 打印单条快递信息
     */
    public static void printExpress(Express e){
        System.out.println("快递信息[" +
                "快递单号:" + e.getNumber() + ' ' +
                ", 快递公司:" + e.getCompany() + ' ' +
                ", 取件码:" + e.getCode() +
                ']');
    }

    /**
     * 打印快递信息,即遍历
     */
    public static void printAllExpress(Express[][] e){
        for(int i = 0;i < 10;i ++){
            for(int j =0;j < 10;j ++){
//                if(e[i][j] == null){
//                    System.out.printf("%-20s","null");
//                }else{
//                    System.out.printf("%-20s",e[i][j].toString());
//                }
                if (e[i][j] != null){
                    printExpress(e[i][j]);
                    printExpressLocation(e[i][j]);
                    System.out.println();
                }
            }
        }
    }

    /**
     *打印快递位置信息
     */
    public static void printExpressLocation(Express e){
        System.out.println("快递存储在快递柜的第" + (e.getX() + 1) + "排,第" + (e.getY() + 1) + "列!");
    }

    /**
     * 已经存在
     */
    public static void hasExist(){
        System.out.println("该单号已经存在!添加快递失败!");
    }

    /**
     * 快递不存在
     */
    public static void noExist(){
        System.out.println("快递不存在!");
    }

    /**
     * 操作成功
     */
    public static void success(){
        System.out.println("操作成功!");
    }

}//end class

四、 数据存取

public class ExpressDao {
    private static Express[][] data = new Express[10][10];
    private static Random r = new Random();
    private static Location location = new Location();
    private static int size = 0;

    /**
     * 初始化快递柜,默认添加几个快递
     */
    public ExpressDao() {
        data[1][3] =new Express("ZT123","中通",629361,1,3);
        data[2][7] = new Express("YT456","圆通",293691,2,7);
        data[6][2] = new Express("YZ789","邮政",693174,6,2);

    }

    /**
     *返回快递柜的全部快递
     */
     public Express[][] getExpress(){
        return data;
     }

    /**
     * 产生取件码
     * @return
     */
     public static int randomCode(){
        int code = 0;
        do{
            code = r.nextInt(900000) + 100000;
        }while(isExist(code));//如果取件码存在则一直循环知道产生新的不重复的取件码
        return code;
     }

    /**
     * 判断是否已经存在这个取件码
     */
     public static boolean isExist(int code){
        for(int i = 0;i < 10;i ++){
            for(int j = 0;j < 10;j++){
                if(data[i][j] !=null && data[i][j].getCode() == code){
                    return true;
                }
            }
        }// end for
         return false;
     }

    /**
     * 通过快递单号查询是否存在
     */
    public static Express findByNumber(String number){
        Express e = new Express();
        e.setNumber(number);
        for(int i = 0;i < 10;i ++){
            for(int j = 0;j < 10;j++){
                if(data[i][j] != null && e.equals(data[i][j])){
                    return data[i][j];//存在
                }
            }
        }// end for
        return null;
    }

    /**
     * 通过取件码查询是否存在
     */
    public static Express findByCode(int code){
        Express e = new Express();
        e.setCode(code);
        for(int i = 0;i < 10;i++){
            for(int j = 0;j < 10;j ++){
                if(data[i][j] != null && data[i][j].getCode() == code){
                    return data[i][j];
                }
            }
        }//end for
        return null;
    }

    /**
     * 添加快递
     */
    public static Express addExpress(Express e){
        if(size == 100){
            System.out.println("快递柜已满!");
            return null;
        }
        Location l = randomLocation();//获取坐标
        e.setCode(randomCode());
        size ++;
        data[l.getX()][l.getY()] = e;
        e.setX(l.getX());
        e.setY(l.getY());
        return data[l.getX()][l.getY()];
    }

    /**
     *删除快递
     */
    public static void moveExpress(Express e){
       p: for(int i = 0;i < 10;i ++){
            for(int j = 0;j < 10;j ++){
                if(data[i][j] != null && e.equals(data[i][j])){
                    data[i][j] = null;
                    break p;
                }
            }
        }
    }

    /**
     *修改快递信息
     */
    public static void updateExpress(Express oldExpress,Express newExpress){
        moveExpress(oldExpress);
        addExpress(newExpress);
    }

    /**
     * 生成坐标
     */
    public static Location randomLocation(){
        do{
            location.setX(r.nextInt(10));
            location.setY(r.nextInt(10));
            if(data[location.getX()][location.getY()] == null){//当这个位置没有存储快递,返回该位置
                return location;
            }
        }while(true);//循环,避免位置重复
    }

}//end class

五、 主界面

public class Main {
    public static View v = new View();
    public static ExpressDao dao = new ExpressDao();
    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) throws OutNumberBoundException {
        v.welcome();

     m: while(true){
            int mainNum = v.mainMenu();//调用主菜单
            switch(mainNum){
                case 0://结束使用
                    break m;
                case 1://进入管理员平台
                    managerPlatform();
                    break ;
                case 2://进入用户平台
                    userPlatform();
                    break ;
            }
        }// end while

        v.bye();
    }//end main

    /**
     * 管理员操作界面
     */
    public static void managerPlatform() throws OutNumberBoundException {
      w:while(true){
            int managerNum = v.managerMain();
            switch(managerNum){
                case 0:{//返回上一级页面
                    return;
                }
                case 1:{//1.录入快递
                    insert();
                }
                break w;
                case 2:{//2.删除快递
                    delete();
                }
                break w;
                case 3:{//3.修改快递
                    update();
                }
                break w;
                case 4:{//4.查看所有快递
                    printAll();
                }
                break w;
            }// end switch
        }//end while

    }//end managerPlatform

    /**
     * 录入快递
     */
    public static void insert(){
        Express e1 = v.insertExpress();//输入快递信息
        Express e2 = dao.findByNumber(e1.getNumber());//通过快递单号查询是否存在
        if (e2 == null){//此快递柜为空,add,生成取件码
            e2 = dao.addExpress(e1);
            v.printExpress(e2);
            v.printExpressLocation(e2);
            v.success();
        }else{
            v.hasExist();
        }
    }

    /**
     * 删除快递
     */
    public static void delete() throws OutNumberBoundException {
        String num = v.findByNumber();
        Express e = dao.findByNumber(num);
        if( e == null){//快递不存在
            v.noExist();
        }else {
            int deleteNum =v.deleteExpress();
            if (deleteNum == 1){//确认删除
                dao.moveExpress(e);
                System.out.println("删除成功!");
                v.success();
            }else if (deleteNum == 0){//取消删除
                System.out.println("取消操作成功!");
            }
        }
    }

    /**
     * 修改快递
     */
    public static void update(){
        String num = v.findByNumber();//输入快递单号
        Express e1 = dao.findByNumber(num);//通过快递单号查找快递是否存在
        if( e1 == null){//快递不存在
            v.noExist();
        }else {
            Express e2 = v.updateExpress();
            dao.updateExpress(e1,e2);
            System.out.println("快递信息更新成功!");
            v.printExpress(e2);
            v.printExpressLocation(e2);
            v.success();
        }
    }

    /**
     * 查看所有快递
     */
    public static void printAll(){
        v.printAllExpress(dao.getExpress());
    }










    /**
     * 用户操作界面
     */
    public static void userPlatform() throws OutNumberBoundException{
//        w:while(true){
//            int userNum = v.userMain();
//            switch(userNum){
//                case 0:{//返回上一级页面
//                    v.mainMenu();
//                    break w;
//                }
//                case 1:{//1.取出快递
                    int code = v.findByCode();
                    Express e = dao.findByCode(code);
                    if(e == null){//??????取件码有误,重新输入
                        v.noExist();
                    }else {
                        v.printExpress(e);
                        dao.moveExpress(e);
                        v.printExpressLocation(e);
                        System.out.println("取件成功!");
                    }
//                }
//                break w;
//            }// end switch
//        }//end while

    }//end userPlatform


}//end main
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Selcouther

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值