java小项目——快递驿站(纯基础白话文,小伙伴门放心食用,简单易懂)
文章目录
-
- 简单介绍
-
- 测试类Test:
- 实体
-
- Express
- ExpressMessage
- 视图界面ExpressView
- 操作类ExpressDao
- 自定义异常类
-
- CantFindExpressException
- ExpressIsExistException
- InputErrorException
- OutOfBoundException
- OutOfSpaceException
写了一段时间的java基础教程了,今天给小伙伴们分享一个仅用javase的基础知识做的一个小项目——快递驿站,功能如下:
简单介绍
快递驿站分两种用户:(由于还没有更新到数据库,所以此处是免登陆的哈)快递小哥(管理员)添加快递删除快递修改快递查看所有快递普通用户 user取出快递实体类entity:快递:快递单号承运公司快递存放信息快递存放信息:存储在快递柜子的第几排存储在快递柜子的第几列视图view:初始界面:管理员界面:用户界面操作dao:一个实体类对应一个dao,但是由于项目比较简单,且两个实体类关联度较高,就采用了一个dao快递操作
文末附上下载链接用于小白们进行测试!!!
测试类Test:
package com.jinhuan.chapter04.no4_4.task_010302_003.test;import com.jinhuan.chapter04.no4_4.task_010302_003.view.ExpressView;/** * @Author jinhuan * @Date 2022/3/20 13:16 * Description: */public class Test { public static void main(String[] args) { //启动类,初始化窗口 ExpressView myView = new ExpressView(); myView.inintMenu(); }}
实体
Express
package com.jinhuan.chapter04.no4_4.task_010302_003.entity;/** * @Author jinhuan * @Date 2022/3/20 12:22 * Description:快递类 */public class Express { /** * transportNum */ private String transportNum; /** * company */ private String company; /** * status */ private Boolean status; /** * pickCode */ private ExpressMessage message; /** * constructor * */ public Express() { } public Express(String transportNum, String company, Boolean status, ExpressMessage message) { this.transportNum = transportNum; this.company = company; this.status = status; this.message = message; } public Express(String transportNum, String company, Boolean status) { this.transportNum = transportNum; this.company = company; this.status = status; } @Override public String toString() { return "运单号:'" + transportNum + '\'' + ", 承运公司:'" + company + '\'' + ", 取件码:'" + message.getPicCode()+ '\'' + "存储在第" +(message.getX()+1)+ "排"+ '\'' + "存第" +(message.getY()+1)+ "列"; } public String getTransportNum() { return transportNum; } public void setTransportNum(String transportNum) { this.transportNum = transportNum; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public Boolean getStatus() { return status; } public void setStatus(Boolean status) { this.status = status; } public ExpressMessage getMessage() { return message; } public void setMessage(ExpressMessage message) { this.message = message; }}
ExpressMessage
package com.jinhuan.chapter04.no4_4.task_010302_003.entity;/** * @Author jinhuan * @Date 2022/3/20 19:21 * Description: */public class ExpressMessage { /** * x */ private Integer x; /** * y */ private Integer y; /** * picCode * */ private String picCode; /** * constructor * */ public ExpressMessage() { } public ExpressMessage(Integer x, Integer y, String picCode) { this.x = x; this.y = y; this.picCode = picCode; } public ExpressMessage(Integer x, Integer y) { this.x = x; this.y = y; } /** * getter and setter * */ public Integer getX() { return x; } public Integer getY() { return y; } public String getPicCode() { return picCode; } public void setX(Integer x) { this.x = x; } public void setY(Integer y) { this.y = y; } public void setPicCode(String picCode) { this.picCode = picCode; }}
视图界面ExpressView
package com.jinhuan.chapter04.no4_4.task_010302_003.view;import com.jinhuan.chapter04.no4_2.exception.CantFindExpressException;import com.jinhuan.chapter04.no4_4.task_010302_003.dao.ExpressDao;import com.jinhuan.chapter04.no4_4.task_010302_003.entity.Express;import com.jinhuan.chapter04.no4_4.task_010302_003.entity.ExpressMessage;import com.jinhuan.chapter04.no4_4.task_010302_003.exception.ExpressIsExistException;import com.jinhuan.chapter04.no4_4.task_010302_003.exception.InputErrorException;import com.jinhuan.chapter04.no4_4.task_010302_003.exception.OutOfBoundException;import com.jinhuan.chapter04.no4_4.task_010302_003.exception.OutOfSpaceException;import java.util.Scanner;/** * @Author jinhuan * @Date 2022/4/22 9:26 * Description: */public class ExpressView { private static Scanner input = new Scanner(System.in); private static ExpressDao expressDao = new ExpressDao(); private static final int TRANSPORTNUM_LENGTH = 6; /** * 初始化菜单 * */ public void inintMenu() { System.out.println("===========欢迎使用Quiet快递驿站服务系统==========="); do { System.out.println("请输入您的身份: 1-管理员 2-用户 0-退出"); String receive = input.nextLine(); try { //调用指令检查方法 if(orderCheck(receive, 0, 2)){ //说明数据输入合法 if("1".equals(receive)){ //进入管理员界面 System.out.println("欢迎进入管理员界面!"); adminstor(); }else if("2".equals(receive)){ System.out.println("欢迎进入用户界面!"); userMenu(); }else { System.out.println("谢谢使用!已安全退出!"); } break; } } catch ( NumberFormatException e) { System.out.println(e.getMessage()); } } while (true); } /** * 管理员界面 * */ public void adminstor(){ do{ System.out.println("请输入指令:1-快递录入 2-删除快递 3-修改快递 4-查看所有快递 0-返回上一级"); String receive = input.nextLine(); if(orderCheck(receive,0,4)){ //说明数据输入合法 switch (receive) { case "1": System.out.println("进入快递录入界面:"); addExpressMenu(); break; case "2": System.out.println("进入快递删除界面:"); deleteExpressMenu(); break; case "3": System.out.println("进入快递修改界面:"); alterExpressMenu(); break; case "4": System.out.println("进入快递查看界面:"); showExpressMenu(); break; default: System.out.println("已返回上一级!"); inintMenu(); break; } break; } }while (true); } /** * 用户界面 */ private void userMenu() { do{ System.out.println("请输入指令:1-取快递 0-返回上一级"); String receive = input.nextLine(); if(orderCheck(receive, 0, 1)){ if ("1".equals(receive)){ do { System.out.println("请输入取件码:"); String code = input.nextLine(); Express findExpress = expressDao.getExpressByPicCode(code); if (findExpress != null){expressDao.deleteExpressByTransportCode(findExpress.getTransportNum());System.out.println("快递已取出!");System.out.println("是否继续取件? 1-是 0-否");String num = input.nextLine();if(orderCheck(num,0,1)){ if("0".equals(num)){ userMenu(); return; }} }else{System.out.println("查询不到对应的快递信息!请检查取件码后重试!"); } }while (true); }else{ inintMenu(); } } }while (true); } /** * 快递录入操作面板 */ private void addExpressMenu() { while (true){ System.out.println("请输入要录入的快递单号及承运公司:"); try { System.out.println("请输入运单号:"); String transPortNum = input.nextLine(); if (checkTransPortNum(transPortNum)){ System.out.println("请输入承运公司:"); String company = input.nextLine(); if (checkInput(company)){ if (expressDao.findExpressByTransportNum(transPortNum) == null) {Express express = new Express(transPortNum, company, true);//获取存储位置ExpressMessage expressMessage = expressDao.add(express);System.out.println("快递存入成功!取件码为:"+expressMessage.getPicCode()+";在第" + (expressMessage.getX() + 1) + "排第" + (expressMessage.getY() + 1) + "列储物柜中!");while (true){ try{ System.out.println("是否继续存快递? 1-是 0-否"); String receiveNum = input.nextLine(); if (checkInput(receiveNum)){ if (orderCheck(receiveNum, 0, 1)) { if ("0".equals(receiveNum)) { adminstor(); return; } } break; } }catch (InputErrorException e){ System.out.println(e.getMessage()); }} }else {throw new ExpressIsExistException("快递单号已存在,请重新录入"); } } } } catch ( ExpressIsExistException | InputErrorException | OutOfSpaceException e) { System.out.println(e.getMessage()); } } } /** * 快递删除修改界面 */ private void deleteExpressMenu() { if (expressDao.getSize() == 0){ System.out.println("当前快递柜为空!"); adminstor(); return; } do{ System.out.println("请输入要删除的快递单号:"); try{ String transportCode = input.nextLine(); if(checkTransPortNum(transportCode)){ if(expressDao.deleteExpressByTransportCode(transportCode)){ while (true){try{ System.out.println("是否继续删除快递? 1-是 0-否"); String receiveNum = input.nextLine(); if (checkInput(receiveNum)){ if (orderCheck(receiveNum, 0, 1)) { if ("0".equals(receiveNum)) { adminstor(); return; } } break; }}catch (InputErrorException e){ System.out.println(e.getMessage());} } }else{ throw new CantFindExpressException("查询不到该快递,请检查单号后重新输入!"); } } }catch (CantFindExpressException e){ System.out.println(e.getMessage()); } }while (true); } /** * 快递修改操作面板 */ private void alterExpressMenu() { if (expressDao.getSize() == 0){ System.out.println("当前快递柜为空!"); adminstor(); return; } while (true) { System.out.println("请输入要修改的快递单号:"); String transportCode = input.nextLine(); if(checkTransPortNum(transportCode)){ try { if(expressDao.alterExpress(transportCode)){ while (true){try{ System.out.println("是否继续修改快递? 1-是 0-否"); String receiveNum = input.nextLine(); if (checkInput(receiveNum)){ if (orderCheck(receiveNum, 0, 1)) { if ("0".equals(receiveNum)) { adminstor(); return; } } break; }}catch (InputErrorException e){ System.out.println(e.getMessage());} } } } catch (CantFindExpressException e) { System.out.println(e.getMessage()); } } } } /** * 快递展示界面 */ private void showExpressMenu() { expressDao.printExpress(expressDao.getExpressList()); adminstor(); } /** * 对运单号格式的检查 * @param transPortNum * @return */ public static boolean checkTransPortNum(String transPortNum) { try { if(checkInput(transPortNum)){ if (transPortNum.length() != TRANSPORTNUM_LENGTH){ System.out.println("请输入6位合法字符!"); return false; }else { return true; } } } catch (InputErrorException e) { System.out.println(e.getMessage()); } return false; } /** * * @param transPortNum * @return 判断结果 * 判断输入是否为空 */ public static boolean checkInput(String transPortNum) throws InputErrorException { if (transPortNum.length() < 1){ throw new InputErrorException("输入不能为空!"); } return true; } /** * @param receive: 用户输入的指令号码 * @param leftBound:指令集左边界 * @param rightBound:指令集右边界 * @return boolean 返回判定结果的合法性 * decription: * 检查用户是否输入了合法指令 */ private static boolean orderCheck(String receive, int leftBound, int rightBound){ try { if(receive.length() < 1){ throw new InputErrorException("指令输入格式有误!不能为空!"); }else { int num = Integer.parseInt(receive); if (num >= leftBound && num <= rightBound){ return true; }else { throw new OutOfBoundException("指令输入有误,请检查后重新输入"+leftBound+"到"+rightBound+"范围之间的数字指令!"); } } } catch (InputErrorException | OutOfBoundException e) { System.out.println(e.getMessage()); }catch (NumberFormatException e) { System.out.println("指令输入有误,请检查后重新输入"+leftBound+"到"+rightBound+"范围之间的数字指令!"); } return false; }}
操作类ExpressDao
package com.jinhuan.chapter04.no4_4.task_010302_003.entity;/** * @Author jinhuan * @Date 2022/3/20 19:21 * Description: */public class ExpressMessage { /** * x */ private Integer x; /** * y */ private Integer y; /** * picCode * */ private String picCode; /** * constructor * */ public ExpressMessage() { } public ExpressMessage(Integer x, Integer y, String picCode) { this.x = x; this.y = y; this.picCode = picCode; } public ExpressMessage(Integer x, Integer y) { this.x = x; this.y = y; } /** * getter and setter * */ public Integer getX() { return x; } public Integer getY() { return y; } public String getPicCode() { return picCode; } public void setX(Integer x) { this.x = x; } public void setY(Integer y) { this.y = y; } public void setPicCode(String picCode) { this.picCode = picCode; }}
自定义异常类
CantFindExpressException
package com.jinhuan.chapter04.no4_4.task_010302_003.exception;/** * @Author jinhuan * @Date 2022/3/20 20:21 * Description: */public class CantFindExpressException extends Throwable{ public CantFindExpressException(String message) { super(message); }}
ExpressIsExistException
package com.jinhuan.chapter04.no4_4.task_010302_003.exception;/** * @Author jinhuan * @Date 2022/3/20 22:12 * Description: */public class ExpressIsExistException extends Throwable{ public ExpressIsExistException(String message) { super(message); }}
InputErrorException
package com.jinhuan.chapter04.no4_4.task_010302_003.exception;/** * @Author jinhuan * @Date 2022/3/21 8:29 * Description:输入指令错误异常 */public class InputErrorException extends Throwable{ public InputErrorException(String message) { super(message); }}
OutOfBoundException
package com.jinhuan.chapter04.no4_4.task_010302_003.exception;/** * @Author jinhuan * @Date 2022/3/20 13:22 * Description: * 自定义的异常类,用于提醒指令输入异常 */public class OutOfBoundException extends Throwable { public OutOfBoundException(String message) { super(message); }}
OutOfSpaceException
package com.jinhuan.chapter04.no4_4.task_010302_003.exception;/** * @Author jinhuan * @Date 2022/3/20 20:40 * Description: */public class OutOfSpaceException extends Throwable{ public OutOfSpaceException(String message) { super(message); }}
以上均为本人个人观点,借此分享,希望能和大家一起进步。如有不慎之处,劳请各位批评指正!鄙人将不胜感激并在第一时间进行修改!
下载链接如下,如果觉得有用的话,给博主个三连支持一下吧:
传送门