今天学习了包管理,理解访问权限,取消硬编码,使用Config类

ScreenShot_2026-10-03_000215_806.png

主类,创建不同实例

▼
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", "杜亚窗帘", "杭州市拱墅区", "智能窗帘/智能门锁"); // 报价单:厂家给业主 SmartHomeQuotation quote1 = new SmartHomeQuotation( "20260001-000001", "2026-10-02 16:35:11", "赵女士静安府全屋智能", client1, vendor, 35800.0, 5800.0, 6500.0, 4200.0, 2000.0 ); // 报价单:厂家给商家 SmartHomeQuotation quote2 = new SmartHomeQuotation( "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()); } }

Config类,final类,禁止继承,private构造方法,禁止实例化

▼
text
复制代码
package cn.com.d99714.smart.common; import java.time.format.DateTimeFormatter; public final class Config { // final类,禁止继承 private Config() { } // private构造方法,禁止实例化 // 校验规则组 public static final String PHONE_REGEX = "^1[3-9]\\d{9}$"; public static final String ERROR_PHONE_FORMAT = "手机号码格式不正确,已置空!"; public static final int MIN_WORK_YEAR = 0; public static final int MAX_WORK_YEAR = 70; public static final String ERROR_WORK_YEAR = "工龄0-70,其他无效!已置0"; public static final double MIN_SCORE = 0.0; public static final double MAX_SCORE = 5.0; public static final String ERROR_SCORE = "评分只能0-5,整数或一位小数!已置0"; public static final double MIN_HOUSE_AREA = 0.0; public static final double MAX_HOUSE_AREA = 5000.0; public static final String ERROR_HOUSE_AREA = "面积大于0小于5000,整数或一位小数!"; // ===== 格式规则组 ===== public static final DateTimeFormatter DATE_TIME_INPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static final DateTimeFormatter DATE_TIME_DISPLAY_FORMATTER = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"); public static final String AMOUNT_FORMAT = "%,.2f"; // ===== 报价单标识组 ===== public static final String QUOTATION_PREFIX = "Q-SM-"; public static final String LINE_CHAR = "-"; public static final int LINE_TITLE_REPEAT_COUNT = 50; public static final int LINE_REPEAT_COUNT = 56; public static final String VERSION = "V1.0"; // ===== 显示标签组(预埋多语言,V1.1 进 i18n 表)===== public static final String QUOTATION_TITLE = "智能家居报价单"; public static final String LABEL_QUOTATION_NO = "报价编号:"; public static final String LABEL_QUOTATION_DATE = "报价日期:"; public static final String LABEL_VENDOR = "报价单位:"; public static final String LABEL_PROJECT_NAME = "项目名称:"; public static final String LABEL_CLIENT_INFO = "用户信息:"; public static final String LABEL_TOTAL_AMOUNT = "项目金额:"; public static final String LABEL_DETAIL_PREFIX = "其中:"; public static final String LABEL_DEVICE_COST = "设备金额:"; public static final String LABEL_MATERIAL_COST = "材料金额:"; public static final String LABEL_INSTALL_COST = "安装金额:"; public static final String LABEL_DEBUG_COST = "调试金额:"; public static final String LABEL_OTHER_COST = "其他金额:"; public static final String TAG_CUSTOMER = "(业主)"; public static final String TAG_COMPANY = "(公司)"; }

SmartHomeQuotation业务类,多态,向上/向下转型

▼
text
复制代码
package cn.com.d99714.smart.entity; import cn.com.d99714.smart.common.Config; 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; 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 String getQuotationNo() { return (Config.QUOTATION_PREFIX + 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 String getClientDisplay() { if (client instanceof Customer) { Customer cu = (Customer) client; return Config.TAG_CUSTOMER + cu.getName(); } else if (client instanceof Company) { Company co = (Company) client; return Config.TAG_COMPANY + co.getCompanyName(); } 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 ""; } @Override public String toString() { double totalAmount = getDeviceCost() + getMaterialCost() + getInstallCost() + getDebugCost() + getOtherCost(); 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())); // 动态拼接报价单金额(按照总金额格式化后的长度右对齐) String totalStr = String.format(Config.AMOUNT_FORMAT, totalAmount); int width = totalStr.length(); sb.append(String.format(Config.LABEL_TOTAL_AMOUNT + "%," + width + ".2f" + " 元%n", totalAmount)); sb.append(Config.LABEL_DETAIL_PREFIX + "\n"); sb.append(String.format(Config.LABEL_DEVICE_COST + "%," + width + ".2f" + " 元%n", getDeviceCost())); sb.append(String.format(Config.LABEL_MATERIAL_COST + "%," + width + ".2f" + " 元%n", getMaterialCost())); sb.append(String.format(Config.LABEL_INSTALL_COST + "%," + width + ".2f" + " 元%n", getInstallCost())); sb.append(String.format(Config.LABEL_DEBUG_COST + "%," + width + ".2f" + " 元%n", getDebugCost())); sb.append(String.format(Config.LABEL_OTHER_COST + "%," + width + ".2f" + " 元%n", getOtherCost())); sb.append(Config.LINE_CHAR.repeat(Config.LINE_REPEAT_COUNT * 2)).append("\n"); return sb.toString(); } }

Person父类

▼
text
复制代码
package cn.com.d99714.smart.entity; import cn.com.d99714.smart.common.Config; public class Person { private String name = ""; private String phone = ""; public Person() { } public Person(String name, String phone) { setName(name); setPhone(phone); } public void setName(String name) { this.name = name; } public void setPhone(String phone) { if (phone != null && phone.matches(Config.PHONE_REGEX)) { this.phone = phone; } else { System.out.println(Config.ERROR_PHONE_FORMAT); this.phone = ""; } } public String getName() { return this.name; } public String getPhone() { return this.phone; } @Override public String toString() { return "姓名:" + this.getName() + ",电话:" + this.getPhone(); } public String introduce() { return ""; } }

Company实体类,继承Person

▼
text
复制代码
package cn.com.d99714.smart.entity; public class Company extends Person { private String companyName = ""; private String address = ""; private String serviceScope = ""; public Company() { } public Company(String name, String phone, String companyName, String address, String serviceScope) { super(name, phone); setCompanyName(companyName); setAddress(address); setServiceScope(serviceScope); } public void setCompanyName(String companyName) { this.companyName = companyName; } public void setAddress(String address) { this.address = address; } public void setServiceScope(String serviceScope) { this.serviceScope = serviceScope; } public String getCompanyName() { return this.companyName; } public String getAddress() { return this.address; } public String getServiceScope() { return this.serviceScope; } @Override public String toString() { return "【公司】联系人:" + this.getName() + ",电话:" + this.getPhone() + ",品牌:" + this.getCompanyName() + ",地址:" + this.getAddress() + ",服务范围:" + this.getServiceScope(); } public String introduce() { StringBuilder sb = new StringBuilder(); sb.append("您好,我们是").append(this.getCompanyName()).append(",公司位于").append(this.getAddress()).append(",主营").append(this.getServiceScope()).append(",联系人").append(this.getName()); return sb.toString(); } }

Customer实体类,继承Person类

▼
text
复制代码
package cn.com.d99714.smart.entity; import cn.com.d99714.smart.common.Config; public class Customer extends Person { private String houseAddress = ""; private double houseArea = 0.0; public Customer() { } public Customer(String name, String phone, String houseAddress, double houseArea) { super(name, phone); setHouseAddress(houseAddress); setHouseArea(houseArea); } public void setHouseAddress(String houseAddress) { this.houseAddress = houseAddress; } public void setHouseArea(double houseArea) { if (houseArea > Config.MIN_HOUSE_AREA && houseArea < Config.MAX_HOUSE_AREA) { this.houseArea = houseArea; } else { System.out.println(Config.ERROR_HOUSE_AREA); this.houseArea = 0.0; } } public String getHouseAddress() { return this.houseAddress; } public double getHouseArea() { return this.houseArea; } @Override public String toString() { return "【业主】:" + this.getName() + "," + this.getPhone() + "," + this.getHouseAddress() + "," + this.getHouseArea() + "m²"; } public String introduce() { StringBuilder sb = new StringBuilder(); sb.append("业主").append(this.getName()).append(",房屋面积").append(this.getHouseArea()).append("m²,位于").append(this.getHouseAddress()).append(",需要智能家居报价。"); return sb.toString(); } }

Worker实体类,继承Person类

▼
text
复制代码
package cn.com.d99714.smart.entity; import cn.com.d99714.smart.common.Config; public class Worker extends Person { private String jobType = ""; private int workYear = 0; private double score = 0.0; public Worker() { } public Worker(String name, String phone, String jobType, int workYear, double score) { super(name, phone); setJobType(jobType); setWorkYear(workYear); setScore(score); } public void setJobType(String jobType) { this.jobType = jobType; } public void setWorkYear(int workYear) { if (workYear >= Config.MIN_WORK_YEAR && workYear <= Config.MAX_WORK_YEAR) { this.workYear = workYear; } else { System.out.println(Config.ERROR_WORK_YEAR); this.workYear = 0; } } public void setScore(double score) { if (score >= Config.MIN_SCORE && score <= Config.MAX_SCORE) { this.score = score; } else { System.out.println(Config.ERROR_SCORE); this.score = 0.0; } } public String getJobType() { return this.jobType; } public int getWorkYear() { return this.workYear; } public double getScore() { return this.score; } @Override public String toString() { return "【师傅】:" + this.getName() + "," + this.getPhone() + "," + this.getJobType() + "," + this.getWorkYear() + "," + this.getScore(); } public String introduce() { StringBuilder sb = new StringBuilder(); sb.append("我是").append(this.getJobType()).append(",从业").append(this.getWorkYear()).append("年,评分").append(this.getScore()).append(",电话").append(this.getPhone()); return sb.toString(); } }
0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
下载 APP