今天学习了ArrayList集合和HashMap集合
测试数据从数组更改成ArrayList集合,按编号查找报价单从数组更改成HashMap集合
▼text复制代码private static ArrayList<SmartHomeQuotation> quotations = new ArrayList<>(); private static HashMap<String, SmartHomeQuotation> quotationHashMap = new HashMap<>(); quotations.add(q1); quotationHashMap.put(q1.getQuotationNo(), q1);
quotations是ArrayList类型,存储的是报价单实例化对象,quotations.add()就是把报价单实例化对象保存到quotations(ArrayList类型)中。
HashMap<String, SmartHomeQuotation>是键值对,第一个形参是报价单编号,第二个形参是报价单实例化对象。quotationHashMap.put()就是把键和值一起保存。
▼text复制代码package cn.com.d99714.smart.service; import cn.com.d99714.smart.common.Config; import cn.com.d99714.smart.entity.*; import cn.com.d99714.smart.exception.DiscountRateInvalidException; import cn.com.d99714.smart.strategy.*; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; public class SmartHomeTest { // Test 类私有的交互常量 private static final String MENU_EXIT = "0. 退出"; private static final String MENU_BACK = "0. 返回"; private static final String MAIN_MENU_PROMPT = "请选择(1-7)【0-退出】:"; private static final String DISCOUNT_MENU_PROMPT = "请选择(1-5)【0-返回】:"; private static final String FIND_QUOTATION_BY_ID_PROMPT = "请输入报价单编号:"; private static final String MSG_INVALID = "无效选项"; private static final String MSG_SUCCESS = "创建成功"; private static final String MSG_NO_QUOTATION = "无报价单!"; private static final String MSG_QUOTATION_NOT_EXISTS = "报价单不存在:"; private static final String MSG_EXCEPTION_MAIN_MENU_OUT_OF_RANGE = "序号超出范围(0-7)!"; private static final String MSG_EXCEPTION_SUB_MENU_OUT_OF_RANGE = "序号超出范围(0-5)!"; private static final String MSG_EXCEPTION_INVALID = "输入错误:请输入有效的数字序号!"; private static final String APP_TITLE = "智能家居报价系统"; private static final String APP_VERSION = "Ver 0.1"; private static final String APP_TITLE_CHAR = "="; private static final String APP_EXIT = "Bye. See you next time."; // 模拟数据存储 private static ArrayList<SmartHomeQuotation> quotations = new ArrayList<>(); private static HashMap<String, SmartHomeQuotation> quotationHashMap = new HashMap<>(); private static ArrayList<Person> people = new ArrayList<>(); private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { initTestData(); // 初始化数据 handleMainMenu(); // 菜单循环 } /** * 1. 打印主菜单 */ private static void printMainMenu() { System.out.print(APP_TITLE_CHAR.repeat(30) + " "); System.out.print(APP_TITLE + " " + APP_VERSION); System.out.print(" " + APP_TITLE_CHAR.repeat(30)); System.out.println(); System.out.println("1. 查看报价单列表"); System.out.println("2. 查看报价单详情"); System.out.println("3. 多态角色展示"); System.out.println("4. 完整客户信息"); System.out.println("5. 折扣方案"); System.out.println("6. 异常测试"); System.out.println("7. 按编号查询报价单"); System.out.println(MENU_EXIT); System.out.print(MAIN_MENU_PROMPT); } /** * 2. 执行主菜单 */ private static void handleMainMenu() { while (true) { printMainMenu(); // 打印主菜单 String selected = sc.nextLine().strip(); try { int inputNum = Integer.parseInt(selected); if (inputNum < 0 || inputNum > 7) { System.out.println(MSG_EXCEPTION_MAIN_MENU_OUT_OF_RANGE); continue; } switch (inputNum) { case 1 -> showQuotationList(); case 2 -> showQuotationDetail(); case 3 -> showRoleDemo(); case 4 -> showFullInfoDemo(); case 5 -> handleDiscountMenu(); case 6 -> showExceptionDemo(); case 7 -> findQuotationById(); case 0 -> { System.out.println(APP_EXIT); return; } default -> System.out.println(MSG_INVALID); } System.out.println(); } catch (NumberFormatException e) { System.out.println(MSG_EXCEPTION_INVALID); } } } /** * 3. 打印二级菜单 */ private static void printDiscountMenu() { System.out.print(APP_TITLE_CHAR.repeat(35) + " "); System.out.print("折扣方案"); System.out.print(" " + APP_TITLE_CHAR.repeat(35)); System.out.println(); System.out.println("1. 整单折扣(字段折扣)"); System.out.println("2. 百分比折扣(接口策略)"); System.out.println("3. 固定金额减免"); System.out.println("4. 满减策略"); System.out.println("5. 阶梯折扣"); System.out.println(MENU_BACK); System.out.print(DISCOUNT_MENU_PROMPT); } /** * 4. 执行二级菜单 */ private static void handleDiscountMenu() { while (true) { printDiscountMenu(); String selected = sc.nextLine().strip(); try { int inputNum = Integer.parseInt(selected); if (inputNum < 0 || inputNum > 5) { System.out.println(MSG_EXCEPTION_SUB_MENU_OUT_OF_RANGE); continue; } switch (inputNum) { case 1 -> showDiscountDemo(); case 2 -> showDiscountStategyDemo(); case 3 -> showFixedAmountDemo(); case 4 -> showFullReductionDemo(); case 5 -> showTieredDemo(); case 0 -> { return; } default -> System.out.println(MSG_INVALID); } System.out.println(); } catch (NumberFormatException e) { System.out.println(MSG_EXCEPTION_INVALID); } } } /** * 5. 预置测试数据 */ private static void initTestData() { Company vendor = new Company("张经理", "13829871635", "视声智能", "广州市黄埔区", "KNX全屋智能/灯光/安防"); Person client1 = new Customer("赵今麦", "15138473848", "上海市静安区", 533); Person client2 = new Company("李经理", "13150432864", "杜亚窗帘", "杭州市拱墅区", "智能窗帘/智能门锁"); Person worker = new Worker("王师傅", "13022531893", "电工", 2, 4.2); people.add(vendor); people.add(client1); people.add(client2); people.add(worker); SmartHomeQuotation q1 = new SmartHomeQuotation( Config.QUOTATION_PREFIX + "20260001-000001", "2026-10-02 16:35:11", "赵女士静安府全屋智能", client1, vendor, 35800.0, 5800.0, 6500.0, 4200.0, 2000.0); q1.setTotalDiscountRate(0.85); SmartHomeQuotation q2 = new SmartHomeQuotation( Config.QUOTATION_PREFIX + "20260001-000002", "2026-10-02 16:37:20", "杜亚展厅智能改造", client2, vendor, 28000.0, 3500.0, 4000.0, 2500.0, 1500.0); q2.setTotalDiscountRate(0.80); quotations.add(q1); quotations.add(q2); quotationHashMap.put(q1.getQuotationNo(), q1); quotationHashMap.put(q2.getQuotationNo(), q2); } /** * 6. 创建客户 */ private static void createCustomer() { System.out.println("创建客户"); System.out.print("类型(1-业主 2-公司 3-师傅):"); String type = sc.nextLine().strip(); System.out.print("姓名:"); String name = sc.nextLine().strip(); System.out.print("电话:"); String phone = sc.nextLine().strip(); switch (type) { case "1" -> { System.out.print("房屋地址:"); String addr = sc.nextLine().strip(); System.out.print("房屋面积:"); double area = Double.parseDouble(sc.nextLine().strip()); people.add(new Customer(name, phone, addr, area)); } case "2" -> { System.out.print("公司名称:"); String cName = sc.nextLine().strip(); System.out.print("公司地址:"); String cAddr = sc.nextLine().strip(); people.add(new Company(name, phone, cName, cAddr, "")); } case "3" -> { System.out.print("工种:"); String job = sc.nextLine().strip(); System.out.print("工龄:"); int year = Integer.parseInt(sc.nextLine().strip()); System.out.print("评分:"); double score = Double.parseDouble(sc.nextLine().strip()); people.add(new Worker(name, phone, job, year, score)); } default -> System.out.println("无效类型"); } System.out.println("创建成功!"); } /** * 7. 创建报价单 */ private static void createQuotation() { if (people.size() < 2) { System.out.println("请先创建客户和厂家"); return; } System.out.println("创建报价单"); // 直接选预置数据,不交互选客户 System.out.println("功能开发中(选项3/4查看)"); } /** * 8. 查看报价单列表 */ private static void showQuotationList() { System.out.println("报价单列表"); for (int i = 0; i < quotations.size(); i++) { System.out.println((i + 1) + ". " + quotations.get(i).getSummaryLine()); } if (quotations.isEmpty()) { System.out.println("暂无报价单"); } } /** * 9. 查看报价单详情 */ private static void showQuotationDetail() { if (quotations.isEmpty()) { System.out.println(MSG_NO_QUOTATION); return; } System.out.print("请输入报价单序号(1-" + quotations.size() + "):"); String input = sc.nextLine().strip(); try { int idx = Integer.parseInt(input) - 1; if (idx < 0 || idx >= quotations.size()) { System.out.println("序号超出范围(1-" + quotations.size() + "):"); return; } System.out.println(quotations.get(idx)); } catch (NumberFormatException e) { System.out.println("输入错误:请输入有效的数字序号!"); } } /** * 10. 多态角色展示 */ private static void showRoleDemo() { System.out.println("多态角色展示"); for (Person p : people) { System.out.println(p.showRole() + " " + p.getName()); System.out.println(" " + p.introduce()); } } /** * 11. 完整客户信息 */ private static void showFullInfoDemo() { System.out.println("完整客户信息"); if (quotations.isEmpty()) { System.out.println("暂无报价单"); return; } for (SmartHomeQuotation q : quotations) { // for-each循环 System.out.println(q.getClientFullInfo()); } } /** * 12. 折扣计算演示 */ private static void showDiscountDemo() { System.out.println("折扣计算演示"); for (SmartHomeQuotation q : quotations) { // for-each循环 System.out.printf("%s 原价:%,.2f 元,%s后:%,.2f 元,优惠:%,.2f 元 %n", q.getQuotationNo(), q.calcOriginalTotalAmount(), q.getDiscountDesc(), q.calcDiscountedTotalAmount(), q.calcOriginalTotalAmount() - q.calcDiscountedTotalAmount()); } } /** * 13. 折扣策略接口演示 */ private static void showDiscountStategyDemo() { System.out.println("折扣策略接口演示"); DiscountStrategy s1 = new PercentageDiscountStrategy(0.85); DiscountStrategy s2 = new PercentageDiscountStrategy(0.80); System.out.printf(" %s 折扣接口策略【%s】:%,.2f 元 %n", quotations.get(0).getQuotationNo(), s1.getStrategyName(), quotations.get(0).getDiscountedTotal(s1)); System.out.printf(" %s 折扣接口策略【%s】:%,.2f 元 %n", quotations.get(1).getQuotationNo(), s2.getStrategyName(), quotations.get(1).getDiscountedTotal(s2)); } /** * 14. 固定金额减免策略接口演示 */ private static void showFixedAmountDemo() { System.out.println("固定金额减免接口演示"); DiscountStrategy fixed3000 = new FixedAmountDiscountStrategy(3000); DiscountStrategy fixed8000 = new FixedAmountDiscountStrategy(8000); for (SmartHomeQuotation q : quotations) { // for-each循环 System.out.printf("%s 原价:%,.2f 元 %n", q.getQuotationNo(), q.calcOriginalTotalAmount()); System.out.printf(" 【%s】 后:%,.2f 元 %n", fixed3000.getStrategyName(), q.getDiscountedTotal(fixed3000)); System.out.printf(" 【%s】 后:%,.2f 元 %n", fixed8000.getStrategyName(), q.getDiscountedTotal(fixed8000)); System.out.println(); } } /** * 15. 满减 */ private static void showFullReductionDemo() { System.out.println("满减策略"); DiscountStrategy strategy = new FullReductionStrategy(50000, 5000); for (SmartHomeQuotation q : quotations) { // for-each循环 System.out.printf("%s 原价:%,.2f 元 [%s] 折后:%,.2f 元 %n", q.getQuotationNo(), q.calcOriginalTotalAmount(), strategy.getStrategyName(), q.getDiscountedTotal(strategy)); } } /** * 16. 阶梯 */ private static void showTieredDemo() { System.out.println("阶梯折扣"); DiscountStrategy strategy = new TieredDiscountStrategy(50000, 0.90, 0.80); for (SmartHomeQuotation q : quotations) { // for-each循环 System.out.printf("%s 原价:%,.2f 元 [%s] 折后:%,.2f 元 %n", q.getQuotationNo(), q.calcOriginalTotalAmount(), strategy.getStrategyName(), q.getDiscountedTotal(strategy)); } } /** * 17. 折扣率异常的处理 */ private static void showExceptionDemo() { System.out.println("异常测试"); // 测试1:正常设置 System.out.println("1. 设置合法折扣率 0.90"); try { quotations.get(0).setTotalDiscountRate(0.90); System.out.println(" 设置成功,当前折扣率:" + quotations.get(0).getTotalDiscountRate()); } catch (DiscountRateInvalidException e) { System.out.println(" 捕获异常:" + e.getMessage()); } finally { System.out.println(" finally 执行"); } System.out.println(); // 测试2:非法设置(触发异常) System.out.println("2. 设置非法折扣率 1.50"); try { quotations.get(0).setTotalDiscountRate(1.50); System.out.println(" 设置成功(这行不会执行)"); } catch (DiscountRateInvalidException e) { System.out.println(" 捕获异常:" + e.getMessage()); } finally { System.out.println(" finally 执行"); } System.out.println(); // 验证:异常后数据未被污染 System.out.println("3. 验证当前折扣率:" + quotations.get(0).getTotalDiscountRate()); } /** * 18. 按照编号查找报价单 */ private static void findQuotationById() { System.out.print(FIND_QUOTATION_BY_ID_PROMPT); String id = sc.nextLine().strip(); if (!quotationHashMap.containsKey(id)) { System.out.println(MSG_QUOTATION_NOT_EXISTS + id); return; } System.out.println(quotationHashMap.get(id)); } }
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
