简易收银台
1 package www.hhy; 2 3 import java.util.Scanner; 4 import java.util.HashMap; 5 import java.util.Map; 6 import java.io.*; 7 import java.time.LocalDate; 8 9 10 /** 11 * @Author: HeHaoYuan 12 * @Date: 2019/7/25 13 * @Description: 14 商品类 15 16 */ 17 class Goods { 18 private String id; 19 private String name; 20 private double price; 21 22 public Goods(String id, String name, double price) { 23 this.id = id; 24 this.name = name; 25 this.price = price; 26 } 27 28 public String getId() { 29 return id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public double getPrice() { 37 return price; 38 } 39 40 @Override 41 public String toString() { 42 return String.format("[%2s] %s %.2f",this.getId(),this.getName(),this.getPrice()); 43 } 44 } 45 46 47 48 49 /** 50 * @Author: HeHaoYuan 51 * @Date: 2019/7/25 52 * @Description: 53 订单类 54 55 */ 56 class Order { 57 //订单创建完成之后,订单编号不能修改 58 private final String orderId; 59 60 //商品信息 -> 商品标号,商品数量 61 //订单创建完成后,goodsInfo属性实例化HashMap 62 private final Map<String, Integer> goodsInfo = new HashMap<>(); 63 64 public Order(String orderId) { 65 this.orderId = orderId; 66 } 67 68 /** 69 * 添加商品 70 * @param goodsId 商品编号 71 * @param count 数量 72 */ 73 public void add(String goodsId, Integer count) { 74 //sum -> 改变后的商品数量 75 Integer sum = this.goodsInfo.get(goodsId); 76 //当前订单中不存在该商品 77 if (sum == null) { 78 sum = count; 79 } else { 80 sum += count; 81 } 82 //将新的商品数量与商品编号关联起来 83 this.goodsInfo.put(goodsId, sum); 84 } 85 86 /** 87 * 减少商品 88 * @param goodsId 89 * @param count 90 */ 91 public void cancel(String goodsId, Integer count) { 92 Integer sum = this.goodsInfo.get(goodsId); 93 //当前订单存在该商品 94 if (sum != null) { 95 sum-=count; 96 if(sum<=0){ 97 this.goodsInfo.remove(goodsId); 98 }else{ 99 this.goodsInfo.put(goodsId,sum); 100 } 101 } 102 } 103 104 /** 105 * 清空所有商品 106 */ 107 public void clear() { 108 System.out.println("清空本次订单中的所有信息..."); 109 this.goodsInfo.clear(); 110 } 111 112 /** 113 * 获得订单编号 114 * @return 115 */ 116 public String getOrderId() { 117 return orderId; 118 } 119 120 /** 121 * 获得订单信息 122 * @return 123 */ 124 public Map<String, Integer> getOrderInfo() { 125 return this.goodsInfo; 126 } 127 } 128 129 130 /** 131 * @Author: HeHaoYuan 132 * @Date: 2019/7/25 133 * @Description: 134 商品管理 135 136 */ 137 interface GoodsCenter { 138 /** 139 * 添加商品 140 * @param good 商品 141 */ 142 void addGoods(Goods good); 143 144 /** 145 * 删除商品(通过商品编号) 146 * @param goodId 147 */ 148 void removeGoods(String goodId); 149 150 /** 151 * 更新商品(修改当前商品编号对应商品的名称) 152 * @param good 153 */ 154 void updateGoods(Goods good); 155 156 /** 157 * 商品是否存在(通过商品编号) 158 * @param goodId 159 * @return 160 */ 161 boolean isExistGoods(String goodId); 162 163 /** 164 * 获取商品(通过商品编号) 165 * @param goodId 166 * @return 167 */ 168 Goods getGoods(String goodId); 169 170 /** 171 * 列出商品信息 172 * @return 173 */ 174 String listGoods(); 175 176 /** 177 * 存储商品信息 178 */ 179 void store(); 180 181 /** 182 * 加载商品信息 183 */ 184 void load(); 185 } 186 187 188 /** 189 * @Author: HeHaoYuan 190 * @Date: 2019/7/25 191 * @Description: 192 订单管理 193 194 */ 195 interface OrderCenter { 196 /** 197 * 添加订单 198 * @param order 订单 199 */ 200 void addOrder(Order order); 201 202 /** 203 * 删除订单 204 * @param orderId 205 */ 206 void removeOrder(String orderId); 207 208 /** 209 * 列出所有订单信息 210 * @return 211 */ 212 String ordersTable(); 213 214 /** 215 * 列出当前订单信息(通过订单编号) 216 * @param orderId 217 * @return 218 */ 219 String orderTable(String orderId); 220 221 /** 222 * 存储订单信息 223 */ 224 void storeOrders(); 225 226 227 /** 228 * 加载订单信息 229 */ 230 String loadOrders(); 231 232 /** 233 * 清空所有订单信息 234 */ 235 void cleanOrders(); 236 } 237 238 239 240 /** 241 * @Author: HeHaoYuan 242 * @Date: 2019/7/25 243 * @Description: 244 245 246 */ 247 248 class GoodsCenterImpl implements GoodsCenter { 249 //创建商品集合,key -> 商品编号,value -> 商品名称 250 private final Map<String, Goods> goodsMap = new HashMap<>(); 251 252 //将商品信息保存至goods.txt 253 private String filePath = System.getProperty("user.dir") + File.separator + "goods.txt"; 254 255 @Override 256 public void addGoods(Goods good) { 257 this.goodsMap.put(good.getId(), good); 258 } 259 260 @Override 261 public void removeGoods(String goodId) { 262 this.goodsMap.remove(goodId); 263 } 264 265 //修改存在商品的名称 266 @Override 267 public void updateGoods(Goods good) { 268 if (this.goodsMap.containsKey(good.getId())) { 269 this.goodsMap.put(good.getId(), good); 270 } 271 } 272 273 @Override 274 public boolean isExistGoods(String goodId) { 275 return this.goodsMap.containsKey(goodId); 276 } 277 278 @Override 279 public Goods getGoods(String goodId) { 280 return this.goodsMap.get(goodId); 281 } 282 283 @Override 284 public String listGoods() { 285 StringBuilder str = new StringBuilder(); 286 str.append("************** 商品信息 **************\n"); 287 str.append("\t编号\t\t\t名称\t\t\t单价\n"); 288 //根据商品名称获取商品信息 289 for (Goods good : this.goodsMap.values()) { 290 str.append("\t" + good.getId() + "\t\t\t" + good.getName() + "\t\t\t" + good.getPrice() + "\n"); 291 } 292 str.append("*************************************\n"); 293 return str.toString(); 294 } 295 296 @Override 297 public void store() { 298 System.out.println("保存所有商品信息到文件,每个商品信息 -> 编号:名称:价格"); 299 //将商品信息输出到goods.txt 300 //输出格式 -> 商品编号:商品名称:商品价格 301 File file = new File(filePath); 302 //自动关闭流 303 try (BufferedWriter writer = new BufferedWriter( 304 new FileWriter(file) 305 )) { 306 for (Goods goods : this.goodsMap.values()) { 307 writer.write(String.format("%s:%s:%.2f\n", goods.getId(), goods.getName(), goods.getPrice())); 308 } 309 } catch (IOException e) { 310 e.printStackTrace(); 311 } 312 } 313 314 @Override 315 public void load() { 316 //读取goods.txt中的商品信息,经过处理,存入商品集合 317 File file = new File(filePath); 318 try (BufferedReader reader = new BufferedReader( 319 new FileReader(file) 320 )) { 321 String read; 322 while((read=reader.readLine())!=null){ 323 String[] values=read.split(":"); 324 if(values.length==3){ 325 Goods good=new Goods(values[0],values[1],Double.parseDouble(values[2])); 326 this.addGoods(good); 327 } 328 } 329 } catch (IOException e) { 330 e.printStackTrace(); 331 } 332 } 333 } 334 335 336 337 338 339 /** 340 * @Author: HeHaoYuan 341 * @Date: 2019/7/25 342 * @Description: 343 344 345 */ 346 class OrderCenterImpl implements OrderCenter { 347 private GoodsCenter goodsCenter; 348 349 public OrderCenterImpl(GoodsCenter goodsCenter) { 350 this.goodsCenter = goodsCenter; 351 } 352 353 //创建订单集合,key -> 订单编号,value -> 订单信息 354 private final Map<String, Order> orderMap = new HashMap<>(); 355 356 private String filePath = System.getProperty("user.dir") + File.separator + "order.txt"; 357 358 @Override 359 public void addOrder(Order order) { 360 this.orderMap.put(order.getOrderId(), order); 361 } 362 363 @Override 364 public void removeOrder(String orderId) { 365 this.orderMap.remove(orderId); 366 } 367 368 @Override 369 public String ordersTable() { 370 StringBuilder str = new StringBuilder(); 371 str.append("=================所有订单=================\n"); 372 str.append("\t编号\t\t\t总价\n"); 373 374 for (Order order : this.orderMap.values()) { 375 Map<String, Integer> totalOrder = order.getOrderInfo(); 376 double everyPrice = 0.0; 377 //商品集合,key -> 商品编号,value -> 商品数量 378 for (Map.Entry<String, Integer> entry : totalOrder.entrySet()) { 379 String goodId = entry.getKey(); 380 Integer goodCount = entry.getValue(); 381 Goods good = goodsCenter.getGoods(goodId); 382 everyPrice += good.getPrice() * goodCount; 383 str.append("\t" + goodId + "\t\t\t" + everyPrice + "\n"); 384 } 385 str.append("=========================================\n"); 386 } 387 return str.toString(); 388 } 389 390 @Override 391 public String orderTable(String orderId) { 392 Order order = this.orderMap.get(orderId); 393 StringBuilder str = new StringBuilder(); 394 str.append("=================订单信息=================\n"); 395 str.append("编号:" + order.getOrderId() + "\n"); 396 str.append("打印时间:" + LocalDate.now().toString()+"\n"); 397 str.append("=========================================\n"); 398 str.append("编号\t\t\t名称\t\t\t数量\t\t\t单价\n"); 399 double totalPrice = 0.0; 400 for (Map.Entry<String, Integer> entry : order.getOrderInfo().entrySet()) { 401 Goods good = goodsCenter.getGoods(entry.getKey()); 402 str.append("" + good.getId() + "\t\t\t" + good.getName() + "\t\t\t"+entry.getValue()+"\t\t\t"+ good.getPrice() + "\n"); 403 totalPrice += entry.getValue() * good.getPrice(); 404 } 405 str.append("=========================================\n"); 406 str.append("总价:" + totalPrice + "\n"); 407 str.append("=========================================\n"); 408 return str.toString(); 409 } 410 411 @Override 412 public void storeOrders() { 413 System.out.println("保存所有订单到文件,每个订单信息 -> 编号和总价"); 414 //将所有订单信息保存至order.txt 415 //输出格式 -> 编号:总价 416 File file = new File(filePath); 417 try (BufferedWriter writer = new BufferedWriter( 418 new FileWriter(file) 419 )) { 420 for(Order order:this.orderMap.values()){ 421 double totalPrice = 0.0; 422 for (Map.Entry<String, Integer> entry : order.getOrderInfo().entrySet()) { 423 totalPrice += entry.getValue() *goodsCenter.getGoods(entry.getKey()).getPrice(); 424 } 425 writer.write(String.format("%s:%.2f\n",order.getOrderId(),totalPrice)); 426 } 427 } catch (IOException e) { 428 e.printStackTrace(); 429 } 430 } 431 432 @Override 433 public String loadOrders() { 434 File file = new File(filePath); 435 StringBuilder str = new StringBuilder(); 436 try (BufferedReader reader = new BufferedReader( 437 new FileReader(file) 438 )) { 439 String read; 440 if ((file.length() == 0)){ 441 System.out.println("请先保存订单信息!!!"); 442 }else{ 443 System.out.println("加载文件中的订单信息..."); 444 double price; 445 str.append("=================所有订单=================\n"); 446 str.append("\t编号\t\t\t总价\n"); 447 while ((read = reader.readLine()) != null) { 448 String[] values = read.split(":"); 449 if (values.length == 2) { 450 Order order = new Order(values[0]); 451 price = Double.parseDouble(values[1]); 452 this.addOrder(order); 453 str.append("\t" + order.getOrderId() + "\t\t\t" + price + "\n"); 454 } 455 } 456 str.append("=========================================\n"); 457 } 458 } catch (IOException e) { 459 e.printStackTrace(); 460 } 461 return str.toString(); 462 } 463 464 @Override 465 public void cleanOrders() { 466 File file = new File(filePath); 467 try (FileWriter fileWriter = new FileWriter(file) 468 ) { 469 fileWriter.write(""); 470 fileWriter.flush(); 471 } catch (IOException e) { 472 e.printStackTrace(); 473 } 474 } 475 } 476 477 478 479 480 /** 481 * @Author: HeHaoYuan 482 * @Date: 2019/7/25 483 * @Description: 484 字符界面收银台(优化版) 485 486 */ 487 class sale { 488 private static Scanner scanner = new Scanner(System.in); 489 490 //商品中心管理 491 private static GoodsCenter goodsCenter = new GoodsCenterImpl(); 492 493 //订单中心管理,先实例化商品中心,因为订单中心的实例化需要传一个商品中心的实例化对象 494 private static OrderCenter orderCenter = new OrderCenterImpl(goodsCenter); 495 496 private static Integer orderId = 0; 497 498 499 public static void helpInfo() { 500 System.out.println("*************** 欢迎使用简易收银台 *****************"); 501 System.out.println(" [U] 使用 [S] 设置 [P] 保存所有信息 [A] 关于 [Q] 退出 "); 502 System.out.println(" 输入: U S P A Q 进入操作 "); 503 System.out.println("*************************************************"); 504 } 505 506 public static void quit() { 507 System.out.println("*************************************************"); 508 System.out.println(" 欢迎使用,下次再见 "); 509 System.out.println("*************************************************"); 510 System.exit(0); 511 } 512 513 public static void about() { 514 System.out.println("******************** 关于 ***********************"); 515 System.out.println(" 名称:简易收银台 "); 516 System.out.println(" 功能:基于字符界面的收银台操作系统 "); 517 System.out.println(" 作者: qqy "); 518 System.out.println(" 版本: v1.0.0 "); 519 System.out.println(" 意见反馈:*********@qq.com "); 520 System.out.println("*************************************************"); 521 } 522 523 public static void usageInfo() { 524 System.out.println("******************* 买单功能 ********************"); 525 System.out.println(" [S] 查看当前订单 [U] 查看所有订单 [A] 下单 [C]清空 "); 526 System.out.println(" [D] 取消 [L] 浏览商品 [P] 保存订单 [R] 返回 "); 527 System.out.println(" 输入: S U A C D L P R 进入操作 "); 528 System.out.println("*************************************************"); 529 } 530 531 public static void settingInfo() { 532 System.out.println("******************* 设置功能 ********************"); 533 System.out.println(" [S] 查看 [A] 上架 [D] 下架 "); 534 System.out.println(" [U] 修改 [P] 保存商品 [R] 返回 "); 535 System.out.println(" 输入: S A D U P R 进入操作 "); 536 System.out.println("*************************************************"); 537 } 538 539 public static void usage() { 540 //创建订单,并且添加到订单管理中心 541 Order order = new Order(String.valueOf(++orderId)); 542 orderCenter.addOrder(order); 543 544 usageInfo(); 545 System.out.println(orderCenter.orderTable(order.getOrderId())); 546 while (true) { 547 String line = scanner.nextLine(); 548 switch (line.trim().toUpperCase()) { 549 case "S": { 550 System.out.println(orderCenter.orderTable(order.getOrderId())); 551 break; 552 } 553 case "A": { 554 System.out.println("请输入下单信息[编号 数量](如下格式:1 2 ):"); 555 String value = scanner.nextLine().trim(); 556 String[] infoArray = value.split(" "); 557 if (infoArray.length == 2) { 558 Goods goods = goodsCenter.getGoods(infoArray[0].trim()); 559 if (goods != null) { 560 order.add(infoArray[0].trim(), Integer.parseInt(infoArray[1].trim())); 561 System.out.println(orderCenter.orderTable(order.getOrderId())); 562 break; 563 } 564 } 565 if (!(goodsCenter.isExistGoods(infoArray[0]))) { 566 System.out.println("!商品还未上架,请重新选择商品"); 567 } 568 System.out.println("请按照格式要求输入信息"); 569 break; 570 } 571 case "D": { 572 System.out.println("请输入取消信息[编号 数量](如下格式:1 2 ):"); 573 String value = scanner.nextLine().trim(); 574 String[] infoArray = value.split(" "); 575 if (infoArray.length == 2) { 576 Goods goods = goodsCenter.getGoods(infoArray[0].trim()); 577 if (goods != null) { 578 order.cancel(infoArray[0], Integer.parseInt(infoArray[1].trim())); 579 System.out.println(orderCenter.orderTable(order.getOrderId())); 580 break; 581 } 582 } 583 System.out.println("请按照格式要求输入信息"); 584 break; 585 } 586 case "U": { 587 System.out.println(orderCenter.loadOrders()); 588 break; 589 } 590 case "L": { 591 System.out.println(goodsCenter.listGoods()); 592 break; 593 } 594 case "C":{ 595 order.clear(); 596 break; 597 } 598 case "P": { 599 //订单信息保存 600 orderCenter.storeOrders(); 601 break; 602 } 603 case "R": { 604 return; 605 } 606 default: { 607 usageInfo(); 608 } 609 } 610 } 611 } 612 613 public static void setting() { 614 settingInfo(); 615 while (true) { 616 String line = scanner.nextLine(); 617 switch (line.toUpperCase()) { 618 case "S": { 619 System.out.println(goodsCenter.listGoods()); 620 break; 621 } 622 case "A": { 623 System.out.println("请输入上架商品信息(如下格式:1 餐巾纸 1.4):"); 624 Goods goods = readGoods(true); 625 if (goods == null) { 626 System.out.println("!请按照格式要求输入信息"); 627 break; 628 } 629 if (goodsCenter.isExistGoods(goods.getId())) { 630 System.out.println("!上架商品已经存在,请重新上架"); 631 } else { 632 goodsCenter.addGoods(goods); 633 } 634 System.out.println(goodsCenter.listGoods()); 635 break; 636 } 637 case "D": { 638 System.out.println("请输入下架商品信息编号(如下格式:1 ):"); 639 Goods goods = readGoods(false); 640 if (goods == null) { 641 System.out.println("!请输入存在的商品"); 642 break; 643 }else{ 644 goodsCenter.removeGoods(goods.getId()); 645 } 646 System.out.println(goodsCenter.listGoods()); 647 break; 648 } 649 case "U": { 650 System.out.println("请输入修改商品信息(如下格式:1 餐巾纸 1.4 )"); 651 Goods goods = readGoods(false); 652 if (goods == null) { 653 System.out.println("!请输入存在的商品"); 654 break; 655 }else {settingInfo(); 656 goodsCenter.updateGoods(goods); 657 } 658 System.out.println(goodsCenter.listGoods()); 659 break; 660 } 661 case "P": { 662 //商品信息保存 663 goodsCenter.store(); 664 break; 665 } 666 case "R": { 667 return; 668 } 669 default: { 670 settingInfo(); 671 } 672 } 673 } 674 } 675 676 public static Goods readGoods(boolean flag) { 677 String value = scanner.nextLine(); 678 String[] infoArray = value.split(" "); 679 if (infoArray.length == 3 || infoArray.length == 1) { 680 if (infoArray.length == 3) { 681 if (!(sale.isNumber(infoArray[0].trim())&& sale.isNumber(infoArray[2].trim()))){ 682 System.out.println("!商品编号或单价有误,请重新输入"); 683 }else { 684 if (!flag) { 685 if (goodsCenter.isExistGoods(infoArray[0])) { 686 Goods goods = new Goods(infoArray[0], infoArray[1], Double.parseDouble(infoArray[2])); 687 return goods; 688 } 689 } 690 else{ 691 Goods goods = new Goods(infoArray[0], infoArray[1], Double.parseDouble(infoArray[2])); 692 return goods; 693 } 694 } 695 } 696 //infoArray.length == 1 697 if (!(sale.isNumber(infoArray[0].trim()))){ 698 System.out.println("!商品编号有误,请重新输入"); 699 }else{ 700 if(goodsCenter.isExistGoods(infoArray[0])){ 701 Goods goods = new Goods(infoArray[0], "", 0.0D); 702 return goods; 703 } 704 } 705 } 706 return null; 707 } 708 709 public static boolean isNumber(String str) { 710 for (int i = str.length(); --i >= 0; ) { 711 if (!Character.isDigit(str.charAt(i))) { 712 return false; 713 } 714 } 715 return true; 716 } 717 718 public static void main(String[] args) { 719 helpInfo(); 720 goodsCenter.load(); 721 //每次打开项目,清空订单记录 722 //新下单后存储信息后,才可查看所有订单。 723 orderCenter.cleanOrders(); 724 while (true) { 725 String line = scanner.nextLine(); 726 switch (line.trim().toUpperCase()) { 727 case "U": 728 usage(); 729 helpInfo(); 730 break; 731 case "S": 732 setting(); 733 helpInfo(); 734 break; 735 case "A": 736 about(); 737 break; 738 739 case "P": 740 goodsCenter.store(); 741 orderCenter.storeOrders(); 742 break; 743 case "Q": 744 quit(); 745 break; 746 default: 747 helpInfo(); 748 } 749 } 750 } 751 }