今天学习了try-catch,用法简单,还是似懂非懂
一、if-else/switch 是"选哪条路走",异常是"这条路走不通了,必须停下来报告"。 二、异常能把错误原因从底层(Entity)一直传到顶层(Controller),中间每层都可以选择处理或继续抛。 if-else 做不到这点,错误信息只在当前方法里。 三、业务逻辑和错误处理分离








main(),主菜单、二级菜单用了try-catch异常处理
▼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.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-6)【0-退出】:"; private static final String DISCOUNT_MENU_PROMPT = "请选择(1-5)【0-返回】:"; private static final String MSG_INVALID = "无效选项"; private static final String MSG_SUCCESS = "创建成功"; private static final String MSG_NO_QUOTATION = "无报价单!"; private static final String MSG_EXCEPTION_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 SmartHomeQuotation[] quotations = new SmartHomeQuotation[10]; private static int quotationCount = 0; private static Person[] people = new Person[10]; private static int peopleCount = 0; 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(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 >6){ System.out.println(MSG_EXCEPTION_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 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_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[peopleCount++] = vendor; people[peopleCount++] = client1; people[peopleCount++] = client2; people[peopleCount++] = 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[quotationCount++] = q1; quotations[quotationCount++] = 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[peopleCount++] = 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[peopleCount++] = 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[peopleCount++] = new Worker(name, phone, job, year, score); } default -> System.out.println("无效类型"); } System.out.println("创建成功!"); } /** * 7. 创建报价单 */ private static void createQuotation() { if (peopleCount < 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 < quotationCount; i++) { System.out.println((i + 1) + ". " + quotations[i].getSummaryLine()); } if (quotationCount == 0) { System.out.println("暂无报价单"); } } /** * 9. 查看报价单详情 */ private static void showQuotationDetail() { if (quotationCount == 0){ System.out.println(MSG_NO_QUOTATION); return; } System.out.print("请输入报价单序号(1-" + quotationCount + "):"); String input = sc.nextLine().strip(); try{ int idx = Integer.parseInt(input) - 1; if (idx < 0 || idx >= quotationCount) { System.out.println("序号超出范围(1-" + quotationCount + "):"); return; } System.out.println(quotations[idx]); } catch (NumberFormatException e){ System.out.println("输入错误:请输入有效的数字序号!"); } } /** * 10. 多态角色展示 */ private static void showRoleDemo() { System.out.println("多态角色展示"); for (int i = 0; i < peopleCount; i++) { Person p = people[i]; System.out.println(p.showRole() + " " + p.getName()); System.out.println(" " + p.introduce()); } } /** * 11. 完整客户信息 */ private static void showFullInfoDemo() { System.out.println("完整客户信息"); if (quotationCount == 0) { System.out.println("暂无报价单"); return; } for (int i = 0; i < quotationCount; i++) { System.out.println(quotations[i].getClientFullInfo()); // ← 调 SmartHomeQuotation 的方法 } } /** * 12. 折扣计算演示 */ private static void showDiscountDemo() { System.out.println("折扣计算演示"); for (int i = 0; i < quotationCount; i++) { SmartHomeQuotation q = quotations[i]; 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[0].getQuotationNo(), s1.getStrategyName(), quotations[0].getDiscountedTotal(s1)); System.out.printf(" %s 折扣接口策略【%s】:%,.2f 元 %n", quotations[1].getQuotationNo(), s2.getStrategyName(), quotations[1].getDiscountedTotal(s2)); } /** * 14. 固定金额减免策略接口演示 */ private static void showFixedAmountDemo() { System.out.println("固定金额减免接口演示"); DiscountStrategy fixed3000 = new FixedAmountDiscountStrategy(3000); DiscountStrategy fixed8000 = new FixedAmountDiscountStrategy(8000); for (int i = 0; i < quotationCount; i++) { SmartHomeQuotation q = quotations[i]; 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 (int i = 0; i < quotationCount; i++) { SmartHomeQuotation q = quotations[i]; 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 (int i = 0; i < quotationCount; i++) { SmartHomeQuotation q = quotations[i]; 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[0].setTotalDiscountRate(0.90); System.out.println(" 设置成功,当前折扣率:" + quotations[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[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[0].getTotalDiscountRate()); } }
SmartHomeQuotation业务实体类,修改了个别变量的命名,符合阿里规范。 折扣的setter方法调用了边界判断方法,该方法使用了异常处理(只抛还没处理)
▼text复制代码package cn.com.d99714.smart.entity; import cn.com.d99714.smart.common.Config; import cn.com.d99714.smart.exception.DiscountRateInvalidException; import cn.com.d99714.smart.strategy.DiscountStrategy; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class SmartHomeQuotation { private String quotationNo = ""; private String quotationDate = ""; private String projectName = ""; private Person client; private Company vendor; private double deviceCost = 0.0; private double materialCost = 0.0; private double installCost = 0.0; private double debugCost = 0.0; private double otherCost = 0.0; // 分类折扣 private double deviceDiscountRate = 1.0; // 1.0=原价,0.85=85折 private double materialDiscountRate = 1.0; // 1.0=原价,0.85=85折 private double installDiscountRate = 1.0; // 1.0=原价,0.85=85折 private double debugDiscountRate = 1.0; // 1.0=原价,0.85=85折 private double otherDiscountRate = 1.0; // 1.0=原价,0.85=85折 // 整单折扣 private double totalDiscountRate = 1.0; // 1.0=原价,0.85=85折 public SmartHomeQuotation() { } public SmartHomeQuotation(String quotationNo, String quotationDate, String projectName, Person client, Company vendor, double deviceCost, double materialCost, double installCost, double debugCost, double otherCost) { this.setQuotationNo(quotationNo); this.setQuotationDate(quotationDate); this.setProjectName(projectName); this.setClient(client); this.setVendor(vendor); this.setDeviceCost(deviceCost); this.setMaterialCost(materialCost); this.setInstallCost(installCost); this.setDebugCost(debugCost); this.setOtherCost(otherCost); } public void setQuotationNo(String quotationNo) { this.quotationNo = quotationNo; } public void setQuotationDate(String quotationDate) { this.quotationDate = quotationDate; } public void setProjectName(String projectName) { this.projectName = projectName; } public void setClient(Person client) { this.client = client; } public void setVendor(Company vendor) { this.vendor = vendor; } public void setDeviceCost(double deviceCost) { this.deviceCost = deviceCost; } public void setMaterialCost(double materialCost) { this.materialCost = materialCost; } public void setInstallCost(double installCost) { this.installCost = installCost; } public void setDebugCost(double debugCost) { this.debugCost = debugCost; } public void setOtherCost(double otherCost) { this.otherCost = otherCost; } public void setDeviceDiscountRate(double deviceDiscountRate) { validateDiscountRate(deviceDiscountRate); this.deviceDiscountRate = deviceDiscountRate; } public void setMaterialDiscountRate(double materialDiscountRate) { validateDiscountRate(materialDiscountRate); this.materialDiscountRate = materialDiscountRate; } public void setInstallDiscountRate(double installDiscountRate) { validateDiscountRate(installDiscountRate); this.installDiscountRate = installDiscountRate; } public void setDebugDiscountRate(double debugDiscountRate) { validateDiscountRate(debugDiscountRate); this.debugDiscountRate = debugDiscountRate; } public void setOtherDiscountRate(double otherDiscountRate) { validateDiscountRate(otherDiscountRate); this.otherDiscountRate = otherDiscountRate; } public void setTotalDiscountRate(double totalDiscountRate) { validateDiscountRate(totalDiscountRate); this.totalDiscountRate = totalDiscountRate; } public String getQuotationNo() { return this.quotationNo; } public String getQuotationDate() { LocalDateTime dateTime = LocalDateTime.parse(this.quotationDate, Config.DATE_TIME_INPUT_FORMATTER); return dateTime.format(Config.DATE_TIME_DISPLAY_FORMATTER); } public String getProjectName() { return this.projectName; } public Person getClient() { return this.client; } public Company getVendor() { return this.vendor; } public double getDeviceCost() { return this.deviceCost; } public double getMaterialCost() { return this.materialCost; } public double getInstallCost() { return this.installCost; } public double getDebugCost() { return this.debugCost; } public double getOtherCost() { return this.otherCost; } public double getDeviceDiscountRate() { return this.deviceDiscountRate; } public double getMaterialDiscountRate() { return this.materialDiscountRate; } public double getInstallDiscountRate() { return this.installDiscountRate; } public double getDebugDiscountRate() { return this.debugDiscountRate; } public double getOtherDiscountRate() { return this.otherDiscountRate; } public double getTotalDiscountRate() { return this.totalDiscountRate; } /** * 判断折扣率是否合法(<0 和 >1都不合法) * @param rate 折扣率 0.0-1.0之间 */ private void validateDiscountRate(double rate) { if (rate < Config.MIN_DISCOUNT_RATE || rate > Config.MAX_DISCOUNT_RATE) { throw new DiscountRateInvalidException( Config.MSG_WARN_PREFIX + Config.MSG_DISCOUNT_RATE_INVALID + "当前值:" + rate ); } } /** * 计算原价的总金额 * @return 原价的总金额 */ public double calcOriginalTotalAmount() { return (deviceCost + materialCost + installCost + debugCost + otherCost); } /** * 计算优惠后的总金额 (总金额 = 设备费用 * 折扣 + 材料费用 * 折扣 + 安装费用 * 折扣 + 调试费用 * 折扣 + 其他费用 * 折扣) * 折扣 * * @return 折扣后的总金额 */ public double calcDiscountedTotalAmount() { double dc = deviceCost * deviceDiscountRate; double mc = materialCost * materialDiscountRate; double ic = installCost * installDiscountRate; double bc = debugCost * debugDiscountRate; double oc = otherCost * otherDiscountRate; return (dc + mc + ic + bc + oc) * totalDiscountRate; } /** * 计算优惠的金额(优惠的金额 = 原始的总金额 - 优惠后的总金额) * * @return 优惠的金额 */ public double caclDiscountAmount() { return calcOriginalTotalAmount() - calcDiscountedTotalAmount(); } // TODO:测试,要删除 /** * 试算:不修改字段,只返回给定折扣率的计算结果 * @param discountRate 试算折扣率 * @return 试算金额 */ public double getDiscountedTotal(double discountRate) { if (discountRate < 0.0 || discountRate > 1.0) { System.out.println(Config.MSG_WARN_PREFIX + Config.MSG_DISCOUNT_RATE_INVALID); return calcOriginalTotalAmount(); } return calcOriginalTotalAmount() * discountRate; } /** * 获取客户信息,通过 instanceof来判断客户是 Customer还是 Company * @return 客户类型:Customer或 Company(Config类有配置) */ public String getClientDisplay() { if (client instanceof Customer) { Customer cu = (Customer) client; return cu.getName() + Config.TAG_CUSTOMER; } else if (client instanceof Company) { Company co = (Company) client; return co.getCompanyName() + Config.TAG_COMPANY; } return ""; } /** * 获取客户的完整信息,通过 instanceof来判断客户是 Customer还是 Company * @return 客户信息 */ public String getClientFullInfo() { if (client instanceof Customer) { Customer cu = (Customer) client; return Config.TAG_CUSTOMER + "电话:" + cu.getPhone() + ",地址:" + cu.getHouseAddress() + ",房屋面积:" + cu.getHouseArea() + "m²"; } else if (client instanceof Company) { Company co = (Company) client; return Config.TAG_COMPANY + "联系人:" + co.getName() + ",电话:" + co.getPhone() + ",地址:" + co.getAddress(); } return client.getName(); } /** * 折扣率转中文描述 * 1.0 → 原价, 0.85 → 85折, 0.8 → 8折, 0.0 → 免费 * @return 中文描述 */ public String getDiscountDesc() { if (totalDiscountRate == Config.MAX_DISCOUNT_RATE) return "原价"; if (totalDiscountRate == Config.MIN_DISCOUNT_RATE) return "免费"; long percent = Math.round(totalDiscountRate * 100); // 0.85→85, 0.8→80 if (percent % 10 == 0) { return (percent / 10) + "折"; // 80→8折, 50→5折 } else { return percent + "折"; // 85→85折, 88→88折 } } /** * 报价单摘要展示 * @return 报价单的摘要 */ public String getSummaryLine() { return String.format("%s | %s | %s | 原始金额:%s 元 | 折扣:%.0f%% | 折后金额:%s 元", quotationNo, projectName, getClientDisplay(), String.format(Config.AMOUNT_FORMAT, calcOriginalTotalAmount()), totalDiscountRate * 100, String.format(Config.AMOUNT_FORMAT, calcDiscountedTotalAmount()) ); } /** * 重写 toString() * @return */ @Override public String toString() { double totalAmount = calcDiscountedTotalAmount(); // 调用calcTotalAmount(),计算报价单总金额 StringBuilder sb = new StringBuilder(); // 静态拼接报价单的头 sb.append(Config.LINE_CHAR.repeat(Config.LINE_TITLE_REPEAT_COUNT)).append(Config.QUOTATION_TITLE).append(Config.LINE_CHAR.repeat(Config.LINE_TITLE_REPEAT_COUNT)).append("\n"); sb.append(String.format(Config.LABEL_QUOTATION_NO + "%s%n", getQuotationNo())); sb.append(String.format(Config.LABEL_QUOTATION_DATE + "%s%n", getQuotationDate())); sb.append(String.format(Config.LABEL_VENDOR + "%s%n", vendor.getCompanyName())); sb.append(String.format(Config.LABEL_PROJECT_NAME + "%s%n", getProjectName())); sb.append(String.format(Config.LABEL_CLIENT_INFO + "%s%n", getClientDisplay())); sb.append(Config.LINE_CHAR.repeat(Config.LINE_REPEAT_COUNT * 2)).append("\n"); // 动态拼接报价单金额(按照总金额格式化后的长度右对齐) String totalStr = String.format(Config.AMOUNT_FORMAT, totalAmount); int width = totalStr.length(); // 折后金额 sb.append(String.format(Config.LABEL_DISCOUNTED_AMOUNT + "%," + width + ".2f" + " 元%n", calcDiscountedTotalAmount())); // 优惠金额 sb.append(String.format(Config.LABEL_DISCOUNT_AMOUNT + "%," + width + ".2f" + " 元%n", caclDiscountAmount())); // 原始金额 sb.append(String.format(Config.LABEL_TOTAL_AMOUNT + "%," + width + ".2f" + " 元(" + Config.LABEL_DISCOUNT_RATE + "%.0f %% )%n", calcOriginalTotalAmount() , totalDiscountRate * 100)); sb.append(Config.LINE_CHAR.repeat(Config.LINE_REPEAT_COUNT * 2)).append("\n"); sb.append(Config.LABEL_DETAIL_PREFIX + "\n"); sb.append(String.format(Config.LABEL_DEVICE_COST + "%," + width + ".2f" + " 元(" + Config.LABEL_DISCOUNT_RATE + "%.0f %% )%n", deviceCost * deviceDiscountRate, deviceDiscountRate * 100)); sb.append(String.format(Config.LABEL_MATERIAL_COST + "%," + width + ".2f" + " 元(" + Config.LABEL_DISCOUNT_RATE + "%.0f %%) %n", materialCost * materialDiscountRate, materialDiscountRate * 100)); sb.append(String.format(Config.LABEL_INSTALL_COST + "%," + width + ".2f" + " 元(" + Config.LABEL_DISCOUNT_RATE + "%.0f %% )%n", installCost * installDiscountRate, installDiscountRate * 100)); sb.append(String.format(Config.LABEL_DEBUG_COST + "%," + width + ".2f" + " 元(" + Config.LABEL_DISCOUNT_RATE + "%.0f %% )%n", debugCost * debugDiscountRate, debugDiscountRate * 100)); sb.append(String.format(Config.LABEL_OTHER_COST + "%," + width + ".2f" + " 元(" + Config.LABEL_DISCOUNT_RATE + "%.0f %% )%n", otherCost * otherDiscountRate, otherDiscountRate * 100)); sb.append(Config.LINE_CHAR.repeat(Config.LINE_REPEAT_COUNT * 2)).append("\n"); return sb.toString(); } /** * 获取整单折扣的总金额(调用接口实现类 discountStrategy中的 applyDiscount()方法) * @param discountStrategy 折扣策略(也就是折扣) * @return 整单折扣的总金额(原始金额打折后的金额) */ public double getDiscountedTotal(DiscountStrategy discountStrategy){ return discountStrategy.applyDiscount(calcOriginalTotalAmount()); } }
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
