增加了DAO、View实现了界面和数据操作分离,这是一个很重要的思想,节约了很多维护时间!
测试程序:
1 /** 2 * 主测试菜单 3 */ 4 5 public class MainTest { 6 public static void main(String[] args) { 7 ExpressView view = new ExpressView(); 8 while (true){ 9 if (view.startMenu()==0){ 10 break; 11 } 12 } 13 } 14 }
1 /** 2 * 快递实体类 3 */ 4 5 public class Express { 6 private String number;//快递单号 7 private String company;//所属公司 8 private Integer code;//取件码 9 10 public Express() { 11 } 12 13 @Override 14 public String toString() { 15 return number + '-' + company + "-" + code; 16 } 17 18 public Express(String number, String company, Integer code) { 19 this.number = number; 20 this.company = company; 21 this.code = code; 22 } 23 24 public String getNumber() { 25 return number; 26 } 27 28 public void setNumber(String number) { 29 this.number = number; 30 } 31 32 public String getCompany() { 33 return company; 34 } 35 36 public void setCompany(String company) { 37 this.company = company; 38 } 39 40 public Integer getCode() { 41 return code; 42 } 43 44 public void setCode(Integer code) { 45 this.code = code; 46 } 47 }
1 import java.util.Random; 2 3 /** 4 * 快递管理Dao,对所有快递数据的操作(快递的录入、删除、修改、查看) 5 */ 6 7 public class ExpressDao { 8 private Express[][] expressArr = new Express[10][10];//表示快递的柜子 9 private int size = 0;//表示快递柜中有多少个快递 10 11 public ExpressDao() { 12 //先初始化几个快递 13 expressArr[0][0] = new Express("S10001","顺丰",123456); 14 expressArr[2][5] = new Express("Y12341","圆通",234567); 15 expressArr[6][7] = new Express("Z10131","中通",345678); 16 size = 3; 17 } 18 19 public int getSize() { 20 return size; 21 } 22 23 public Coordinate add(Express express) throws Exception { 24 if (size == 100){//快递柜满了直接报异常 25 throw new Exception("快递柜已满!不能放入快递了!"); 26 } 27 Random random = new Random(); 28 int x,y; 29 do { 30 x = random.nextInt(10); 31 y = random.nextInt(10); 32 }while (expressArr[x][y]!=null);//该位置如果有快递,重新生成坐标 33 34 //系统生成随机的取件码在100000-999999之间 35 int code = random.nextInt(899999) + 100000; 36 express.setCode(code); 37 expressArr[x][y] = express; 38 size++;//添加一个快递,数量+1 39 return new Coordinate(x,y); 40 } 41 42 //获取快递柜的所有快递信息 43 public Express[][] getAllExpress(){ 44 return expressArr; 45 } 46 47 /** 48 * 根据坐标获取快递 49 * @param x 50 * @param y 51 * @return 52 */ 53 public Express findExpressByCoordinate(int x, int y){ 54 return expressArr[x][y]; 55 } 56 57 /** 58 * 根据快递单号删除 59 * @param number 60 * @return 61 * @throws Exception 62 */ 63 public boolean delete(String number) throws Exception { 64 //判断该单号的快递是否存在 65 Coordinate coordinate = findExpressByNumber(number); 66 if (coordinate == null){ 67 throw new Exception("该快递不存在!"); 68 } 69 expressArr[coordinate.getX()][coordinate.getY()] = null;//清空 70 size--; 71 return true; 72 } 73 74 /** 75 * 通过快递单号查找 76 * @param number 77 * @return 78 */ 79 public Coordinate findExpressByNumber(String number){ 80 for (int i = 0; i < 10; i++) { 81 for (int j = 0; j < 10; j++) { 82 if (expressArr[i][j] != null && expressArr[i][j].getNumber().equals(number)){ 83 return new Coordinate(i,j); 84 } 85 } 86 } 87 return null; 88 } 89 90 /** 91 * 通过取件码查找 92 * @param code 93 * @return 94 */ 95 public Coordinate findExpressByCode(int code){ 96 for (int i = 0; i < 10; i++) { 97 for (int j = 0; j < 10; j++) { 98 if (expressArr[i][j] != null && expressArr[i][j].getCode() == code){ 99 return new Coordinate(i,j); 100 } 101 } 102 } 103 return null; 104 } 105 106 /** 107 * 修改指定坐标的快递 108 * @param coordinate 109 * @param newNumber 110 * @param newCompany 111 * @throws Exception 112 */ 113 public void update(Coordinate coordinate, String newNumber, String newCompany) throws Exception{ 114 //根据坐标获取到要修改的快递 115 Express expresses = expressArr[coordinate.getX()][coordinate.getY()]; 116 //修改该快递的单号和公司 117 expresses.setNumber(newNumber); 118 expresses.setCompany(newCompany); 119 System.out.println("修改成功!"); 120 } 121 122 /** 123 * 判断快递是否存在,存在则返回位置 124 * @param number 125 * @return 126 * @throws Exception 127 */ 128 public Coordinate check(String number) throws Exception{ 129 //判断该单号的快递是否存在 130 Coordinate coordinate = findExpressByNumber(number); 131 if (coordinate == null){ 132 throw new Exception("该快递不存在!"); 133 } 134 return coordinate; 135 } 136 }
1 /** 2 * 快递柜坐标,长X,宽Y 3 */ 4 public class Coordinate { 5 private int x; 6 private int y; 7 8 public Coordinate() { 9 } 10 11 public Coordinate(int x, int y) { 12 this.x = x; 13 this.y = y; 14 } 15 16 public int getX() { 17 return x; 18 } 19 20 public void setX(int x) { 21 this.x = x; 22 } 23 24 public int getY() { 25 return y; 26 } 27 28 public void setY(int y) { 29 this.y = y; 30 } 31 }
1 import java.util.Scanner; 2 3 /** 4 * 视图层,专门跟用户打交道(展示给用户看的和需要用户输入的都在这里操作 5 * 一级菜单 6 * 输入和输出,调用Dao的方法操作数据 7 */ 8 9 public class ExpressView { 10 private Scanner input = new Scanner(System.in); 11 private ExpressDao expressDao = new ExpressDao(); 12 13 /** 14 * 起始菜单 15 * @return 16 */ 17 public int startMenu(){ 18 int num = 0; 19 do { 20 System.out.println("----欢迎来到快递管理系统----"); 21 System.out.println("请选择:"); 22 System.out.println("1、管理员"); 23 System.out.println("2、普通用户"); 24 System.out.println("0、退出"); 25 String strNum = input.nextLine(); 26 try { 27 num = ValidateNum(strNum,0,2); 28 break; 29 } catch (NumberFormatException e) { 30 System.out.println(e.getMessage()); 31 } catch (OutNumberRoundException e) { 32 System.out.println(e.getMessage()); 33 } 34 }while(true); 35 if (num == 1){ 36 System.out.println("=============管理员菜单============="); 37 administratorMenu(); 38 } else if (num == 2){ 39 userMenu(); 40 } else if (num == 0){ 41 System.out.println("谢谢使用!"); 42 } 43 return num; 44 } 45 46 /** 47 * 管理员菜单 48 */ 49 private void administratorMenu(){ 50 int num = 0; 51 do { 52 System.out.println("1、快递录入"); 53 System.out.println("2、快递删除"); 54 System.out.println("3、快递修改"); 55 System.out.println("4、查看所有快递"); 56 String strNum = input.nextLine(); 57 try { 58 num = ValidateNum(strNum,0,4); 59 break; 60 } catch (NumberFormatException e) { 61 System.out.println(e.getMessage()); 62 } catch (OutNumberRoundException e) { 63 System.out.println(e.getMessage()); 64 } 65 }while (true); 66 if (num == 1){ 67 System.out.println("=============快递录入============="); 68 addExpress(); 69 } else if (num == 2){ 70 System.out.println("=============快递删除============="); 71 deleteExpress(); 72 } else if (num == 3){ 73 updateExpress(); 74 System.out.println("=============快递修改============="); 75 } else if (num == 4){ 76 System.out.println("=============查看所有快递============="); 77 Express[][] allExpress = expressDao.getAllExpress(); 78 printAllExpress(allExpress); 79 //printByGrid(allExpress); 80 } else if (num == 0){ 81 System.out.println("谢谢使用!"); 82 } 83 } 84 85 /** 86 * 普通用户菜单 87 */ 88 private void userMenu() { 89 int code; 90 //取件码需要合法 91 do { 92 System.out.println("=============普通用户菜单============="); 93 System.out.println("请输入取件码:"); 94 String strCode = input.nextLine(); 95 try { 96 code = ValidateNum(strCode,100000,900000); 97 break; 98 } catch (OutNumberRoundException e) { 99 e.printStackTrace(); 100 } 101 }while (true); 102 //调用DAO的方法判断取件码对应的快递是否存在 103 Coordinate coordinate = expressDao.findExpressByCode(code); 104 if (coordinate != null){ 105 int x = coordinate.getX(); 106 int y = coordinate.getY(); 107 Express express = expressDao.findExpressByCoordinate(x,y); 108 System.out.println("=============快递信息==============="); 109 System.out.println("所在位置:柜子的第" + (x+1) + "排" + (y+1) + "列"); 110 System.out.println(express); 111 //从柜子中删除 112 try { 113 if (expressDao.delete(express.getNumber())){ 114 System.out.println("取件成功!"); 115 } 116 } catch (Exception e) { 117 System.out.println("取件失败!" + e.getMessage()); 118 } 119 }else { 120 System.out.println("该取件码不存在!"); 121 } 122 } 123 124 /** 125 * 更新快递 126 */ 127 private void updateExpress() { 128 System.out.println("请输入要修改的快递单号:"); 129 String number = input.nextLine(); 130 try { 131 Coordinate coordinate = expressDao.check(number);//寻找对应的快递柜,没有则抛出异常 132 System.out.println("请输入修改后的快递单号:"); 133 String newNumber = input.nextLine(); 134 System.out.println("请输入修改后的公司:"); 135 String newCompany = input.nextLine(); 136 expressDao.update(coordinate, newNumber, newCompany); 137 } catch (Exception e) { 138 System.out.println("修改失败!"); 139 System.out.println(e.getMessage()); 140 } 141 } 142 143 /** 144 * 删除快递 145 */ 146 private void deleteExpress() { 147 System.out.println("请输入要删除的快递单号:"); 148 String number = input.nextLine(); 149 try { 150 if (expressDao.delete(number)){ 151 System.out.println("删除成功!"); 152 System.out.println("现在快递柜中有"+expressDao.getSize()+"个快递"); 153 }else { 154 System.out.println("删除失败!"); 155 } 156 } catch (Exception e) { 157 System.out.println("删除失败!"); 158 System.out.println(e.getMessage()); 159 } 160 } 161 162 /** 163 * 快递录入 164 */ 165 private void addExpress() { 166 Express express = new Express(); 167 System.out.println("请输入快递单号:"); 168 express.setNumber(input.nextLine()); 169 System.out.println("请输入公司:"); 170 express.setCompany(input.nextLine()); 171 try { 172 Coordinate coordinate = expressDao.add(express); 173 System.out.println("添加成功!放在快递柜中第"+(coordinate.getX()+1)+"排第" +(coordinate.getY()+1)+"列"); 174 System.out.println("此时快递柜中有"+expressDao.getSize()+"个快递"); 175 } catch (Exception e) { 176 System.out.println("添加失败!"); 177 System.out.println(e.getMessage()); 178 } 179 } 180 181 /** 182 * 打印快递柜中的所有非空的格子中的快递信息 183 * @param allExpress 184 */ 185 private void printAllExpress(Express[][] allExpress){ 186 System.out.println("快递柜中有"+expressDao.getSize()+"个快递"); 187 System.out.println("==================="); 188 for (int i = 0; i < allExpress.length; i++) { 189 for (int j = 0; j < allExpress[i].length; j++) { 190 if (allExpress[i][j] != null) 191 System.out.println(allExpress[i][j]); 192 } 193 } 194 } 195 196 /** 197 * 按照快递柜的方式打印快递信息 198 * @param allExpress 199 */ 200 private void printByGrid(Express[][] allExpress){ 201 System.out.println("==================="); 202 for (int i = 0; i < allExpress.length; i++) { 203 for (int j = 0; j < allExpress[i].length; j++) { 204 if (allExpress[i][j] == null){ 205 System.out.printf("%-20s","");//printf字符串,30表示留出的空间 206 }else{ 207 System.out.printf("%-19s",allExpress[i][j]); 208 } 209 210 } 211 System.out.println(); 212 } 213 } 214 215 /** 216 * 确保输出的是数字,且范围为0-2 217 * @param strNum 218 * @param begin 219 * @param end 220 * @return 221 * @throws NumberFormatException 222 * @throws OutNumberRoundException 223 */ 224 private int ValidateNum(String strNum, int begin, int end) throws NumberFormatException,OutNumberRoundException{ 225 try { 226 int num = Integer.valueOf(strNum); 227 if (num < begin || num > end){ 228 throw new OutNumberRoundException("数字的范围必须在"+ begin + "和" + end + "之间!"); 229 } 230 return num; 231 } catch (NumberFormatException | OutNumberRoundException e) { 232 throw new NumberFormatException("输入的必须是数字!"); 233 } 234 } 235 }
/** * 自定义异常类:数字超过规定的范围 */ public class OutNumberRoundException extends Throwable { public OutNumberRoundException(String s) { super(s); } }
测试结果:
快递录入:

快递删除:

快递修改:

取出快递:

浙公网安备 33010602011771号