今天学习了接口,新增业务逻辑,只增加策略类实现接口,不改其他类




main()只有两行代码,各用两个方法(print和handle)实现主菜单和二级菜单
▼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.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-5):"; private static final String DISCOUNT_MENU_PROMPT = "请选择(1-5):"; private static final String MSG_INVALID = "无效选项"; private static final String MSG_SUCCESS = "创建成功"; 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(MENU_EXIT); System.out.print(MAIN_MENU_PROMPT); } /** * 2. 执行主菜单 */ private static void handleMainMenu() { while (true) { printMainMenu(); // 打印主菜单 String selected = sc.nextLine().strip(); switch (selected) { case "1" -> showQuotationList(); case "2" -> showQuotationDetail(); case "3" -> showRoleDemo(); case "4" -> showFullInfoDemo(); case "5" -> handleDiscountMenu(); case "0" -> { System.out.println(APP_EXIT); return; } default -> System.out.println(MSG_INVALID); } System.out.println(); } } /** * 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 choice = sc.nextLine().trim(); switch (choice) { 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(); } } /** * 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.setTotalDiscount(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.setTotalDiscount(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() { System.out.print("请输入报价单序号(1-" + quotationCount + "):"); int idx = Integer.parseInt(sc.nextLine().strip()) - 1; if (idx < 0 || idx >= quotationCount) { System.out.println("序号无效"); return; } System.out.println(quotations[idx]); } /** * 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)); } } }
DiscountStrategy接口,策略接口
▼text复制代码package cn.com.d99714.smart.strategy; public interface DiscountStrategy { // 获取整单折扣后的金额,相当于SmartHomeQuotation类中的getDiscountedTotal() double applyDiscount(double amount); // 获取折扣的中文描述,相当于SmartHomeQuotation类中的getDiscountDesc() String getStrategyName(); }
FixedAmountDiscountStrategy类,实现接口,固定金额减免策略
▼text复制代码package cn.com.d99714.smart.strategy; public class FixedAmountDiscountStrategy implements DiscountStrategy { private double amount; public FixedAmountDiscountStrategy(double amount) { this.amount = amount; } @Override public double applyDiscount(double total) { return Math.max(0, total - amount); // 不能减为负数,最小为 0 } @Override public String getStrategyName() { return "减免" + (int) amount + "元"; } }
FullReductionStrategy类,实现接口,满减策略
▼text复制代码package cn.com.d99714.smart.strategy; public class FullReductionStrategy implements DiscountStrategy { private double threshold; // 满多少 private double reduction; // 减多少 public FullReductionStrategy(double threshold, double reduction) { this.threshold = threshold; this.reduction = reduction; } @Override public double applyDiscount(double amount) { return amount >= threshold ? amount - reduction : amount; } @Override public String getStrategyName() { return "满" + (int) threshold + "减" + (int) reduction; } }
PercentageDiscountStrategy类,实现接口,百分比策略
▼text复制代码package cn.com.d99714.smart.strategy; public class PercentageDiscountStrategy implements DiscountStrategy{ private double rate; public PercentageDiscountStrategy(double rate){ this.rate = rate; } @Override public double applyDiscount(double amount){ return amount * rate; } @Override public String getStrategyName(){ return (int)(rate * 100) + "%折扣"; } }
TieredDiscountStrategy类,实现接口,阶梯减策略
▼text复制代码package cn.com.d99714.smart.strategy; public class TieredDiscountStrategy implements DiscountStrategy { private double threshold; // 阶梯门槛 private double lowerRate; // 门槛内折扣 private double upperRate; // 超出部分折扣 public TieredDiscountStrategy(double threshold, double lowerRate, double upperRate) { this.threshold = threshold; this.lowerRate = lowerRate; this.upperRate = upperRate; } @Override public double applyDiscount(double amount) { if (amount <= threshold) { return amount * lowerRate; } else { double lowerPart = threshold * lowerRate; double upperPart = (amount - threshold) * upperRate; return lowerPart + upperPart; } } @Override public String getStrategyName() { return (int) (lowerRate * 10) + "折+超出" + (int) (upperRate * 10) + "折"; } }
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
