快递驿站(处理异常版)

快递驿站管理系统另外一个版本,着重用来写try catch的版本,显示形式变成陈列式;

具体要求见上篇博文截图,提供代码如下:

数据操作部分:ExpressDao

package com.kkb.dao;

import com.kkb.pojo.Coordinate;
import com.kkb.pojo.Express;

import java.util.Random;

/**
 * 快递的数据操作
 */

public class ExpressDao {
    private Express[][] expressAll = new Express[10][10];
    private int size = 0;

    public ExpressDao() {
        expressAll[0][0] = new Express("S123456", "顺丰", 123456);
        expressAll[2][6] = new Express("Y123456", "圆通", 258769);
        expressAll[6][7] = new Express("Z123456", "中通", 124795);
        size = 3;
    }

    /**
     * 获取快递柜
     *
     * @return
     */
    public Express[][] getAllExpress() {
        return expressAll;
    }

    /**
     * 快递录入
     * @param express
     * @return
     * @throws Exception
     */
    public Coordinate add(Express express) throws Exception {
        Random random = new Random();
        if (size == 100){
            throw new Exception("快递柜已满!");
        }
        //随机生成空位置的坐标
        int x,y;
        do {
            x = random.nextInt(10);
            y = random.nextInt(10);
        }while (expressAll[x][y] != null);
        //随机生成取件码
        int code;
        do {
            code = random.nextInt(900000) + 100000;
        }while (isExistCode(code));//如果重复重新生成
        express.setCode(code);
        expressAll[x][y] = express;
        size++;//添加一个快递 实际数量+1
        return new Coordinate(x,y);
    }

    /**
     * 删除快递的数据操作
     * @param number
     * @return
     */
    public boolean delete(String number) throws Exception {
        Coordinate coordinate = findByNumber(number);

        if (coordinate == null) {
            throw new Exception("快递不存在!");
        }
        expressAll[coordinate.getX()][coordinate.getY()] = null;
        size --;
        return true;
    }
    public Coordinate findByCode(int code){
        for (int i = 0;i < 10;i++){
            for (int j = 0;j < 10;j++){
                if (expressAll[i][j] != null && expressAll[i][j].getCode() == code){
                    return new Coordinate(i,j);
                }
            }
        }
        return null;

    }
    public Express findByCoordinate(int x, int y){
        return expressAll[x][y];
    }

    /**
     * 快递员修改快递
     * @param number
     * @param newExpress
     * @return
     * @throws Exception
     */
    public boolean update(String number,Express newExpress) throws Exception {
        Coordinate coordinate = findByNumber(number);
        if (coordinate == null) {
            throw new Exception("快递号不存在!");
        }
        //根据坐标获取到要修改的快递
        Express expresses = expressAll[coordinate.getX()][coordinate.getY()];
        expresses.setNumber(newExpress.getNumber());
        expresses.setCompany(newExpress.getCompany());
        return true;

    }

    /**
     * 根据快递单号查找快递
     * @param number
     * @return
     */
    public Coordinate findByNumber(String number){
        for (int i = 0;i < 10;i++){
            for (int j = 0;j < 10;j++){
                if (expressAll[i][j] != null && expressAll[i][j].getNumber().equals(number)){
                    return new Coordinate(i,j);
                }
            }
        }
        return null;
    }

    /**
     * 判断取件码是否重复
     * @return
     */
    public boolean isExistCode(int code){
        for (int i = 0;i<expressAll.length;i++){
            for (int j = 0;j<expressAll[i].length;j++){
                if (expressAll[i][j] != null && expressAll[i][j].getCode() == code){
                    return true;
                }
                }
            }
        return false;

    }

    public int getSize() {
        return size;
    }
}

自定义一个异常:OutOfBoundException

package com.kkb.exception;

/**
 * 自定义异常类 超出规定范围
 */

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

实体包当中需要放入两个:Express和Coordinate,分别如下:

Express:

package com.kkb.pojo;//bean entity

/**
 * 快递实体类
 */

public class Express {
    private String number;
    private String company;
    private Integer code;

    public Express() {
    }

    public Express(String number, String company, Integer code) {
        this.number = number;
        this.company = company;
        this.code = code;
    }

    @Override
    public String toString() {
        return number+"-"+company+"-"+code;
    }

    public String getNumber() {
        return number;
    }

    public String getCompany() {
        return company;
    }

    public Integer getCode() {
        return code;
    }

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

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

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

Coordinate:

package com.kkb.pojo;

public class Coordinate {
    private int x;
    private int y;

    public Coordinate() {
    }

    public Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

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

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

视图层:ExressView

package com.kkb.view;

import com.kkb.dao.ExpressDao;
import com.kkb.exception.OutOfBoundException;
import com.kkb.pojo.Coordinate;
import com.kkb.pojo.Express;

import java.util.Scanner;

/**
 * 视图层
 */

public class ExpressView {
    Scanner input = new Scanner(System.in);
    private ExpressDao expressDao = new ExpressDao();

    /**
     * 起始菜单
     * @return
     */
    public int startMenu(){
        int num = 0;
        do {
            System.out.println("----欢迎使用开课吧快递系统----");
            System.out.println("请选择:");
            System.out.println("1、管理员");
            System.out.println("2、用户");
            System.out.println("0、退出");
            String strNum = input.nextLine();
            try {
                num = vaildataNum(strNum, 0, 2);
                break;
            } catch (NumberFormatException e) {
                System.err.println(e.getMessage());
            } catch (OutOfBoundException e) {
                System.err.println(e.getMessage());
            }
        }while(true);
        if (num==1){
            administorMenu();
        }else if(num==2){
            userMenu();
        }else if(num==0){
            System.out.println("谢谢使用!");
        }
        return num;

    }

    /**
     * 用户菜单
     * 
     */
    public void userMenu(){
        int code;
        System.out.println("请输入您的取件码");
        String strcode = input.nextLine();
        do {
            try {
                code = vaildataNum(strcode,100000,900000);
                break;
            } catch (OutOfBoundException e) {
                e.printStackTrace();
            }
        } while (true);
        Coordinate coordinate = expressDao.findByCode(code);
        if (coordinate != null) {
            int x = coordinate.getX();
            int y = coordinate.getY();
            System.out.println("-------快递信息-----");
            System.out.println("快递在第"+(x+1)+"排,第"+(y+1)+"列");
            //通过坐标获取快递信息
            Express express = expressDao.findByCoordinate(x,y);
            try {
                if (expressDao.delete(express.getNumber())) {
                    System.out.println("删除成功!");
                }else {
                    System.out.println("删除失败!");
                }
            } catch (Exception e) {
                System.out.println("删除失败!");
                e.printStackTrace();
            }
        }else{
            System.out.println("该取件码不存在");
        }

    }

    /**
     * 管理员菜单展示
     */
    private void administorMenu(){
        int num;
        do {
            System.out.println("1、快递录入");
            System.out.println("2、快递删除");
            System.out.println("3、快递修改");
            System.out.println("4、查看所有快递");
            System.out.println("0、返回上一级菜单");
            System.out.println("请输入要选择的功能序号");
            String strNum = input.nextLine();
            try {
                num = vaildataNum(strNum, 0, 4);
                break;
            } catch (NumberFormatException e) {
                System.err.println(e.getMessage());
            } catch (OutOfBoundException e) {
                System.err.println(e.getMessage());
            }
        }while(true);
        if (num==1){
           addExpress();
        }else if (num==2){
            delete();
        }else if (num==3){
            update();
        }else if (num==4){
            Express[][] allExpress = expressDao.getAllExpress();
            printByGrid(allExpress);
            //printAllExpress(allExpress);
        }else if (num==0){
            startMenu();
        }
    }

    /**
     * 添加快递
     */
    private void addExpress(){
        Express express =new Express();
        System.out.println("请输入快递单号和快递公司");
        express.setNumber(input.nextLine());
        express.setCompany(input.nextLine());
        try {
            Coordinate coordinate = expressDao.add(express);
            System.out.println("添加成功,放在了第"+(coordinate.getX()+1)+"排第"+(coordinate.getY()+1)+"列");
            System.out.println("快递柜中共有"+expressDao.getSize()+"个快递");
        } catch (Exception e) {
            System.out.println("添加失败!");
            System.err.println(e.getMessage());
            e.printStackTrace();
        }

    }

    /**
     * 删除快递
     */
    public void delete(){
        System.out.println("请输入你要删除的快递单号");
        String number = input.nextLine();
        try {
            if (expressDao.delete(number)) {
                System.out.println("删除成功!");
            }
        } catch (Exception e) {
            System.out.println("删除失败!");
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 修改快递功能
     */
    public void update(){
        System.out.println("请输入要修改的快递单号");
        String number = input.nextLine();
        Coordinate coor = expressDao.findByNumber(number);
        if (coor != null){
            Express newExpress = new Express();
            System.out.println("请输入您修改后的快递单号和快递公司");
            newExpress.setNumber(input.nextLine());
            newExpress.setCompany(input.nextLine());
            try {
                if (expressDao.update(number,newExpress)) {
                    System.out.println("修改成功!");
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }else {
            System.out.println("修改失败!");
        }
    }

    /**
     * 展示所有快递
     * @param allExpress
     */
    public void printAllExpress(Express[][] allExpress){
        for (int i=0;i<allExpress.length;i++){
            for (int j=0;j<allExpress[i].length;j++){
                if (allExpress[i][j] != null){
                    System.out.println(allExpress[i][j]);
                }
            }
        }
    }

    /**
     * 以快递柜排列形式查看所有快递
     * @param allExpress
     */
    public void printByGrid(Express[][] allExpress){
        for (int i=0;i<allExpress.length;i++){
            for (int j=0;j<allExpress[i].length;j++){
                if (allExpress[i][j] != null){
                    System.out.printf("%-20s",allExpress[i][j]);
                }else if (allExpress[i][j] == null){
                    System.out.printf("%-20s","空\t");
                }
            }
            System.out.println("");
        }
    }



    /**
     * 验证用户输入是否合法
     * @param str
     * @param begin
     * @param end
     * @return
     * @throws NumberFormatException
     * @throws OutOfBoundException
     */

    private int vaildataNum(String str,int begin,int end) throws NumberFormatException,OutOfBoundException{
        try{
            int num = Integer.valueOf(str);
            if (num<begin || num>end){
                throw new OutOfBoundException("输入的数字必须在"+begin+"到"+end+"之间");
            }
            return num;
        }
            catch(NumberFormatException exception){

                throw new NumberFormatException("输入的必须是数字!");
        }


    }
}

最后附测试方法:MainTest

package com.kkb.main;

import com.kkb.view.ExpressView;

public class MainTest {
    public static void main(String[] args) {
        ExpressView view = new ExpressView();
        while (true){
           if (view.startMenu()==0)
               break;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值