为了彰显代码师的地道,从main()中提取方法

ScreenShot_2026-10-04_003940_965.png

main()中的代码抽取为方法

▼
text
复制代码
package cn.com.d99714.smart.service; import cn.com.d99714.smart.common.Config; import cn.com.d99714.smart.entity.*; import java.util.Scanner; public class SmartHomeTest { // Test 类私有的交互常量 private static final String MENU_TITLE = "===== 智能家居报价系统 V0.x ====="; private static final String MENU_EXIT = "0. 退出"; private static final String MSG_INVALID = "无效选项"; private static final String MSG_SUCCESS = "创建成功"; // 模拟数据存储 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 scanner = new Scanner(System.in); public static void main(String[] args) { // 预置测试数据 initTestData(); // 初始化数据 // 菜单循环 while (true) { printMenu(); // 输出菜单 String selected = scanner.nextLine().trim(); switch (selected) { case "1" -> createCustomer(); // 创建客户 case "2" -> createQuotation(); // 创建报价单 case "3" -> showQuotationList(); // 查看报价单列表(摘要) case "4" -> showQuotationDetail(); // 查看报价单详情 case "5" -> showRoleDemo(); // 多态角色展示 case "6" -> discountDemo(); // 折扣计算演示 case "0" -> { // 退出 System.out.println("Bye. See you next time."); return; } default -> System.out.println("无效选项,请重新输入"); } System.out.println(); } } /** * 1. 打印菜单(方法提取) */ private static void printMenu() { System.out.println("========== 智能家居报价系统 v0.1 =========="); 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("0. 退出"); System.out.print("请选择(1-6)【0-退出】:"); } /** * 2. 预置测试数据(方法提取) */ 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; } /** * 3. 创建客户(方法提取) */ private static void createCustomer() { System.out.println("创建客户"); System.out.print("类型(1-业主 2-公司 3-师傅):"); String type = scanner.nextLine().trim(); System.out.print("姓名:"); String name = scanner.nextLine().trim(); System.out.print("电话:"); String phone = scanner.nextLine().trim(); switch (type) { case "1" -> { System.out.print("房屋地址:"); String addr = scanner.nextLine().trim(); System.out.print("房屋面积:"); double area = Double.parseDouble(scanner.nextLine().trim()); people[peopleCount++] = new Customer(name, phone, addr, area); } case "2" -> { System.out.print("公司名称:"); String cName = scanner.nextLine().trim(); System.out.print("公司地址:"); String cAddr = scanner.nextLine().trim(); people[peopleCount++] = new Company(name, phone, cName, cAddr, ""); } case "3" -> { System.out.print("工种:"); String job = scanner.nextLine().trim(); System.out.print("工龄:"); int year = Integer.parseInt(scanner.nextLine().trim()); System.out.print("评分:"); double score = Double.parseDouble(scanner.nextLine().trim()); people[peopleCount++] = new Worker(name, phone, job, year, score); } default -> System.out.println("无效类型"); } System.out.println("创建成功!"); } /** * 4. 创建报价单(方法提取) */ private static void createQuotation() { if (peopleCount < 2) { System.out.println("请先创建客户和厂家"); return; } System.out.println("创建报价单"); // 直接选预置数据,不交互选客户 System.out.println("功能开发中,请使用预置数据(选项3/4查看)"); } /** * 5. 查看报价单列表(方法提取) */ 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("暂无报价单"); } } /** * 6. 查看报价单详情(方法提取) */ private static void showQuotationDetail() { System.out.print("请输入报价单序号(1-" + quotationCount + "):"); int idx = Integer.parseInt(scanner.nextLine().trim()) - 1; if (idx < 0 || idx >= quotationCount) { System.out.println("序号无效"); return; } System.out.println(quotations[idx]); } /** * 7. 多态角色展示(方法提取) */ 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()); } } /** * 8. 折扣计算演示(方法提取) */ private static void discountDemo() { 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()); } } }

原main()中的代码

▼
text
复制代码
package cn.com.d99714.smart.service; import cn.com.d99714.smart.common.Config; import cn.com.d99714.smart.entity.*; public class SmartHomeTest { public static void main(String[] args) { // 厂家 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); // 报价单:厂家给业主 SmartHomeQuotation quote1 = 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 ); // 报价单:厂家给商家 SmartHomeQuotation quote2 = 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 ); // 打印报价单 System.out.println(quote1); System.out.println(); System.out.println(quote2); // 打印完整信息 System.out.println(); System.out.println("完整客户信息(点击查看)"); System.out.println(quote1.getClientFullInfo()); System.out.println(quote2.getClientFullInfo()); // 折扣测试 System.out.println(); System.out.println("折扣测试"); // 设置折扣 quote1.setTotalDiscount(0.85); quote2.setTotalDiscount(0.80); // 测试 calcTotal() + getDiscountedTotal() System.out.printf("%s 原始金额:%,.2f 元,%s后金额:%,.2f 元,优惠金额:%.2f 元%n", quote1.getQuotationNo(), quote1.calcOriginalTotalAmount(), quote1.getDiscountDesc(), // "85折" quote1.calcDiscountedTotalAmount(), quote1.calcOriginalTotalAmount() - quote1.calcDiscountedTotalAmount()); System.out.printf("%s 原始金额:%,.2f 元,%s后金额:%,.2f 元,优惠金额:%.2f 元%n", quote2.getQuotationNo(), quote2.calcOriginalTotalAmount(), quote2.getDiscountDesc(), // "8折" quote2.calcDiscountedTotalAmount(), quote2.calcOriginalTotalAmount() - quote2.calcDiscountedTotalAmount()); // 越界折扣测试 System.out.println(); System.out.println("越界折扣测试"); double invalidResult = quote1.getDiscountedTotal(1.5); System.out.printf("传入1.5(越界),返回:%,.2f 元%n", invalidResult); // Day40 批量汇总(数组遍历) System.out.println(); System.out.println("批量汇总"); SmartHomeQuotation[] quotes = {quote1, quote2}; double allTotal = 0; double allFinal = 0; for (SmartHomeQuotation q : quotes) { System.out.printf("%s 原价:%,.2f 元,%s后:%,.2f 元%n", q.getQuotationNo(), q.calcOriginalTotalAmount(), q.getDiscountDesc(), q.calcDiscountedTotalAmount()); allTotal += q.calcOriginalTotalAmount(); allFinal += q.calcDiscountedTotalAmount(); } System.out.printf("总优惠:%,.2f 元%n", allTotal - allFinal); // 一行摘要 System.out.println(); System.out.println("报价单摘要"); System.out.println(quote1.getSummaryLine()); System.out.println(quote2.getSummaryLine()); // Day41:多态遍历测试 System.out.println(); System.out.println("多态遍历测试"); Person[] people = {client1, client2, worker, vendor}; for (Person p : people) { System.out.println(p.showRole() + " " + p.getName()); System.out.println("自我介绍:" + p.introduce()); System.out.println(); } } }
0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
下载 APP