面向对象练习:快递柜代码

先看结构:

主程序 Express_3_6

package cloud.windinfo.demo.express_3_6;

import cloud.windinfo.demo.express_3_6.bean.Express;
import cloud.windinfo.demo.express_3_6.dao.ExpressDao;
import cloud.windinfo.demo.express_3_6.view.View;

public class Express_3_6 {
    private static View view = new View(); //建立控制台
    private static ExpressDao expressDao = new ExpressDao();   //建立快递柜
    public static void main(String[] args) {

        Express_3_6 main = new Express_3_6();
        view.wellcome();    //显示欢迎

        while(true){
            switch (view.choiceActerMenu()){    //1-快递员,2-用户,0-退出系统
                case 1:     //快递员
                    while(main.courier(view.courierMenu())){}   //调用快递员方法,返回true一直在快递员功能界面循环
                    break;
                case 2: //用户
                    int code=view.inputCode();  //输入取件码
                    if(code==0){
                        break;
                    }
                    else{
                        Express e=expressDao.findExpressByCode(code);   //根据取件码找快递
                        if (e==null){
                            System.out.println("取件码不存在");
                        }
                        else{
                            expressDao.getExpressOut(e).setNum(null);   //把快递取出来并且删掉单号
                            System.out.println("快递已经成功取出!");
                        }
                    }
                    break;
                case 0:
                    view.goodbye();
                    return;
            }
        }
    }

    /*快递员方法
    * @param 快递员功能界面的选项
    * @return 是否继续留在快递员功能界面
    * */
    private boolean courier(int choice){
        while(true){
            switch (choice){    //1-快递录入,2-删除快递,3-修改快递,4-查看所有快递,5-浏览快递柜,0-返回上级
                case 1: //快递录入
                    String putNum = view.inputNum();    //输入单号
                    if(putNum.equals("0")){
                        return true;
                    }
                    if(expressDao.findExpressByNum(putNum)!=null){  //查询是否已有单号
                        System.out.println("单号已存在!");
                        return true;
                    }
                    String company = view.inputCompany();   //输入公司名
                    if(company.equals("0")){
                        return true;
                    }
                    Express newExpress = new Express(putNum,company);//建立快递对象,取件码和柜号为空
                    expressDao.putExpressIn(newExpress);    //执行存入方法,得到取件码和柜号属性
                    return true;

                case 2:     //删除快递
                    String delNum = view.inputNum();    //输入单号
                    if(delNum.equals("0")){
                        return true;
                    }
                    Express delExpress=expressDao.findExpressByNum(delNum); //根据单号找快递
                    if(delExpress==null){   //没找到
                        System.out.println("单号不存在!");
                    }else{  //找到了
                        expressDao.getExpressOut(delExpress).setNum(null);  //取出来,并删除单号
                        System.out.println("快递删除成功!");
                    }
                    return true;

                case 3: //修改快递
                    String updateNum = view.inputNum(); //  输入单号查找
                    if(updateNum.equals("0")){
                        return true;
                    }
                    Express updateExpress=expressDao.findExpressByNum(updateNum);
                    if(updateExpress==null){    //如果没找到
                        System.out.println("单号不存在!");
                        return true;
                    }else { //找到以后
                        String newNum = view.inputNum();    //输入新单号
                        if(newNum.equals("0")){
                            return true;
                        }
                        String newCompany = view.inputCompany();    //输入新的公司名
                        if(newCompany.equals("0")){
                            return true;
                        }
                        updateExpress.setNum(newNum);   //修改单号
                        updateExpress.setCompanyName(newCompany);   //修改公司名
                        System.out.println("修改快递成功!");
                    }
                    return true;

                case 4: //查看所有快递,遍历,非空打印
                    for (int i = 0; i < 10; i++) {
                        for (int j = 0; j < 10; j++) {
                            if(expressDao.getExpressBox()[i][j]!=null){
                                System.out.println(expressDao.getExpressBox()[i][j].toString());
                            }
                        }
                    }
                    return true;

                case 5: //打印快递柜
                    System.out.println(expressDao.toString());
                    return true;

                case 0: //返回false跳出快递员功能菜单
                    return false;
            }
        }
    }
}

bean(封装)包,快递包裹类

package cloud.windinfo.demo.express_3_6.bean;

import java.util.Arrays;

/*
* 快递类,我不会动,我只有属性
* 属性:单号、取件码、公司名称、箱号(坐标)
* 方法:getter、setter、equals、toString
* */
public class Express {
    //单号
    private String num;
    //公司名
    private String companyName;
    //取件码
    private int code=-1;
    //在快递柜的箱号,0下标为横坐标,1下标为纵坐标,未在快递柜状态为{-1,-1}
    private int[] expressLocation= {-1,-1};

    public Express() {
    }

    //有参构造方法,只传入单号和公司名称,取件码和箱号(坐标)由快递柜生成
    public Express(String num, String company) {
        this.num = num;
        this.companyName = company;

    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public int getCode() {
        return code;
    }

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

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public int[] getExpressLocation() {
        return expressLocation;
    }

    public void setExpressLocation(int[] expressLocation) {
        this.expressLocation = expressLocation;
    }

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

        Express express = (Express) o;

        return num.equals(express.num);
    }

    @Override
    public String toString() {
        return "Express{" +
                "num='" + num + '\'' +
                ", code=" + code +
                ", company='" + companyName + '\'' +
                ", expressLocation=" + Arrays.toString(expressLocation) +
                '}';
    }
}

dao包(存储),快递柜类

package cloud.windinfo.demo.express_3_6.dao;

import cloud.windinfo.demo.express_3_6.bean.Express;

import java.util.Arrays;
import java.util.Random;

/*
* 快递柜类
* 属性:代表快递箱的二维数组,两个维度代表横纵坐标,数组存储Express快递对象
* 方法:getter、setter、存快递putExpressIn、分配快递箱productIndex、生成取件码prudentCode、
*      取快递getExpressOut、根据单号找快递findExpressByNum、根据取件码找快递findExpressByCode\
*       打印快递柜方法toString
* */
public class ExpressDao {
    private Random random = new Random();
    private Express[][] expressBox =new Express[10][10];

    public ExpressDao() {
    }

    public  Express[][] getExpressBox() {
        return expressBox;
    }

    public void setExpressBox(Express[][] expressBox) {
        this.expressBox = expressBox;
    }

    /*存快递方法
    * @param 没存过的快递,expressLocation={-1,-1}
    * @return 存成功:expressLocation 改为快递箱的坐标,存失败仍返回{-1,-1}
    * */
    public Express putExpressIn(Express e){
        //根据单号找快递
        if(findExpressByNum(e.getNum())!=null){ //如果找到了
            System.out.println("这个快递已经有了,下一个"); //这个快递已存过,快递对象原样退回
        }else{  //如果没找到
            int[] index = productIndex();   //分配一个空箱,拿到坐标
            if(index[0]==-1){
                System.out.println("快递柜已满,存件失败!");
                return e;
            }
            expressBox[index[0]][index[1]]=e;   //快递对象存进去
            e.setExpressLocation(index);    //把箱号存入快递对象的箱号属性
            int code = prudentCode();   //产生4位随机取件码
            e.setCode(code);    //把取件码存入快递对象的取件码属性
            System.out.println("快递已存入 第"+e.getExpressLocation()[0]+"行 第"+e.getExpressLocation()[1]+"列 快递箱,单号:"+e.getNum());
            //把存在箱子里的快递对象返回
        }
        return e;
    }

    /*取快递方法
    * @param 要取的快递对象
    * @return 被取出的快递对象 Express e(e.expressLocation= {-1,-1},e.code=-1)
    * */
    public Express getExpressOut(Express e){
        int[] index=e.getExpressLocation();
        expressBox[index[0]][index[1]]=null;
        e.setCode(-1);
        e.setExpressLocation(new int[]{-1,-1});
        return e;    }

    /*分配快递箱方法
    * @param
    * @return 长度2整数数组,下标1代表横坐标,下标2代表纵坐标
    * */
    public int[] productIndex(){
        int[] index={-1,-1}; //定义一个长度2的数组存放快递箱的坐标
        int nullNum=0;  //初始化空箱计数
        for (int i = 0; i < 10; i++) {  //遍历快递柜数空箱
            for (int j = 0; j < 10; j++) {
                if(expressBox[i][j]==null){
                    nullNum++;
                }
            }
        }
        if(nullNum==0){ //没有空箱就退出去
            return index;
        }
        while(true){    //只有分到的箱子是空的,才返回箱子的坐标,否则一直找
            index[0]=random.nextInt(10);
            index[1]=random.nextInt(10);
            if(expressBox[index[0]][index[1]]==null){
                return index;
            }
        }
    }

    /*生成取件码方法
    * @param
    * @return 取件码1000~9999
    * */
    public int prudentCode(){
        int code=random.nextInt(8999)+1000;
        if(findExpressByCode(code)!=null){
            prudentCode();
        }
        return code;
    }

    //根据单号找快递
    /*@param String 单号
    *@return 快递对象
    * */
    public Express findExpressByNum(String num){
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if(expressBox[i][j]!=null&&num.equals(expressBox[i][j].getNum())){
                    return expressBox[i][j];
                }
            }
        }
        return null;
    }

    //根据取件码找快递
    /*@param int 单号
    * @return 快递对象
    * */
    public Express findExpressByCode(int code){
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if(expressBox[i][j]!=null&&code==expressBox[i][j].getCode()){
                    return expressBox[i][j];
                }
            }
        }
        return null;
    }

    //打印快递柜方法
    /*把快递柜每个快递箱存放的内容按行列打印
    * 空箱打印[    ]
    * */

    @Override
    public String toString() {
        System.out.print(String.format("%4s"," "));
        for (int i = 0; i < 10; i++) {
            System.out.print(String.format("%8s",i));
        }
        System.out.println();
        for (int i = 0; i < 10; i++) {
            System.out.print(String.format("%8s",i));
            for (int j = 0; j <10; j++) {
                if(expressBox[i][j]==null){
                    System.out.print(String.format("%8s","[    ]"));
                }else{
                    System.out.print(String.format("%8s",expressBox[i][j].getNum()));
                }
            }
            System.out.println();
        }
        return "快递箱存储单号列表已打印";
    }
}

view(界面)包,人机界面类

package cloud.windinfo.demo.express_3_6.view;

import java.util.Scanner;

//控制台类
public class View {

    private Scanner input =new Scanner(System.in);

    public View() {
    }

    //欢迎
    public void wellcome(){
        System.out.println("欢迎使用Express快递柜3_6版,请根据提示操作^_^");
    }

    //再见
    public void goodbye(){
        System.out.println("感谢您的使用,请随手关闭柜门,Bye~~~");
    }

    /*选择身份
    * @return 身份序号 1、快递员 2、用户 0、退出系统
    * */
    public int choiceActerMenu(){
        int choice=-1;
        while(true){
            try{
                System.out.println("请选择您的身份:1-快递员,2-用户,0-退出系统");
                String choiceStr = input.nextLine();
                choice = Integer.parseInt(choiceStr);   //验证是否数字
                if(choice<0||choice>2){ //验证是否012
                    throw new RuntimeException();   //不是012抛出异常,重新运行方法
                }
                return choice;
            }
            catch (RuntimeException e) {
                System.out.println("输入错误,请重新输入!");
            }
        }
    }

    /*快递员功能菜单
    * @param
    * @return 功能序号
    * */
    public int courierMenu(){
        int choice=-1;
        while(true){
            try{
                System.out.println("请选择您的操作:1-快递录入,2-删除快递,3-修改快递,4-查看所有快递,5-浏览快递柜,0-返回上级");
                String choiceStr = input.nextLine();
                choice = Integer.parseInt(choiceStr);
                if(choice<0||choice>5){
                    throw new RuntimeException();
                }
                return choice;
            }
            catch (RuntimeException e) {
                System.out.println("输入错误,请重新输入!");
            }
        }
    }


    /*输入单号
    * @param
    * @return 字符串格式单号,返回"0"需要回到快递员功能菜单
    * */
    public String inputNum(){
        System.out.println("请输入6位数字单号(0-返回):");
        String num = input.nextLine();
        try{
            if(num.equals("0")){
                return num;
            }
            int numInt=Integer.parseInt(num);
            if(numInt<100000){
                throw new RuntimeException();
            }
        }catch (RuntimeException e) {
            System.out.println("单号输入错误,请重新输入!");
           num= inputNum();
        }
        return num;
    }

    /*快递员输入公司
    * @param
    * @return String 公司名称
    * */
    public String inputCompany(){
        System.out.println("请输入公司名称(0-返回):");
        String company = input.nextLine();
        return company;
    }

    //用户功能菜单
    public int userMenu(){
        return inputCode();
    }

    /*输入取件码方法
    * @param
    * @return int 四位数字取件码 1000
    * */
    public int inputCode(){
        int code=-1;
        while(true){
            try{
                System.out.println("请输入取件码,或输入0退出");
                String choiceStr = input.nextLine();
                code = Integer.parseInt(choiceStr);
                if(code<1000||code>9999){
                    System.out.println("取件码输入错误,请重新输入!");
                    throw new RuntimeException();
                }
                return code;
            }
            catch (RuntimeException e) {
                System.out.println("输入错误,请重新输入!");
            }
        }
    }
}

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值