You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
240 lines
8.1 KiB
240 lines
8.1 KiB
/**
|
|
* 车辆实体类 - 用于车辆管理系统
|
|
* 包含车辆的基本信息,实现完整的封装
|
|
*/
|
|
public class Car {
|
|
// 私有属性,实现封装
|
|
private String brand; // 品牌
|
|
private String model; // 型号
|
|
private int year; // 生产年份
|
|
private double price; // 价格
|
|
private String color; // 颜色
|
|
private String licensePlate; // 车牌号
|
|
|
|
/**
|
|
* 无参构造方法
|
|
*/
|
|
public Car() {
|
|
this.brand = "未知品牌";
|
|
this.model = "未知型号";
|
|
this.year = 2026;
|
|
this.price = 0.0;
|
|
this.color = "未知颜色";
|
|
this.licensePlate = "未上牌";
|
|
}
|
|
|
|
/**
|
|
* 全参构造方法 - 创建车辆对象时必须提供所有信息
|
|
* @param brand 品牌
|
|
* @param model 型号
|
|
* @param year 生产年份
|
|
* @param price 价格
|
|
* @param color 颜色
|
|
* @param licensePlate 车牌号
|
|
*/
|
|
public Car(String brand, String model, int year, double price, String color, String licensePlate) {
|
|
this.brand = brand;
|
|
this.model = model;
|
|
setYear(year); // 使用 setter 方法进行数据校验
|
|
setPrice(price); // 使用 setter 方法进行数据校验
|
|
this.color = color;
|
|
this.licensePlate = licensePlate;
|
|
}
|
|
|
|
/**
|
|
* 简化版构造方法 - 只需提供品牌、型号和价格
|
|
* @param brand 品牌
|
|
* @param model 型号
|
|
* @param price 价格
|
|
*/
|
|
public Car(String brand, String model, double price) {
|
|
this(brand, model, 2026, price, "标准色", "待上牌"); // 调用全参构造方法
|
|
}
|
|
|
|
// Getter 和 Setter 方法(封装的体现)
|
|
|
|
public String getBrand() {
|
|
return brand;
|
|
}
|
|
|
|
public void setBrand(String brand) {
|
|
if (brand != null && !brand.trim().isEmpty()) {
|
|
this.brand = brand;
|
|
} else {
|
|
System.out.println("错误:品牌名称不能为空!");
|
|
}
|
|
}
|
|
|
|
public String getModel() {
|
|
return model;
|
|
}
|
|
|
|
public void setModel(String model) {
|
|
if (model != null && !model.trim().isEmpty()) {
|
|
this.model = model;
|
|
} else {
|
|
System.out.println("错误:型号名称不能为空!");
|
|
}
|
|
}
|
|
|
|
public int getYear() {
|
|
return year;
|
|
}
|
|
|
|
/**
|
|
* 设置生产年份(带数据校验)
|
|
* @param year 生产年份
|
|
*/
|
|
public void setYear(int year) {
|
|
if (year >= 1900 && year <= 2026) {
|
|
this.year = year;
|
|
} else {
|
|
System.out.println("错误:年份必须在 1900-2026 之间!");
|
|
this.year = 2026; // 使用默认值
|
|
}
|
|
}
|
|
|
|
public double getPrice() {
|
|
return price;
|
|
}
|
|
|
|
/**
|
|
* 设置价格(带数据校验)
|
|
* @param price 价格
|
|
*/
|
|
public void setPrice(double price) {
|
|
if (price > 0) {
|
|
this.price = price;
|
|
} else {
|
|
System.out.println("错误:价格必须大于 0!");
|
|
this.price = 0.0; // 使用默认值
|
|
}
|
|
}
|
|
|
|
public String getColor() {
|
|
return color;
|
|
}
|
|
|
|
public void setColor(String color) {
|
|
if (color != null && !color.trim().isEmpty()) {
|
|
this.color = color;
|
|
} else {
|
|
System.out.println("错误:颜色不能为空!");
|
|
}
|
|
}
|
|
|
|
public String getLicensePlate() {
|
|
return licensePlate;
|
|
}
|
|
|
|
public void setLicensePlate(String licensePlate) {
|
|
if (licensePlate != null && !licensePlate.trim().isEmpty()) {
|
|
this.licensePlate = licensePlate;
|
|
} else {
|
|
System.out.println("错误:车牌号不能为空!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 显示车辆信息
|
|
*/
|
|
public void showInfo() {
|
|
System.out.println("\n========== 车辆信息 ==========");
|
|
System.out.println("品牌:" + brand);
|
|
System.out.println("型号:" + model);
|
|
System.out.println("年份:" + year);
|
|
System.out.println("价格:" + price + "万元");
|
|
System.out.println("颜色:" + color);
|
|
System.out.println("车牌号:" + licensePlate);
|
|
System.out.println("=============================\n");
|
|
}
|
|
|
|
/**
|
|
* 测试本类的功能
|
|
* @param args 命令行参数(可选:第一个参数为起始测试编号)
|
|
*/
|
|
static void main(String[] args) {
|
|
System.out.println("╔══════════════════════════════╗");
|
|
System.out.println("║ 车辆管理系统 - Car 类测试 ║");
|
|
System.out.println("╚══════════════════════════════╝\n");
|
|
|
|
// 如果通过命令行传入参数,可以控制测试流程
|
|
int startTest = 1; // 默认从测试 1 开始
|
|
if (args.length > 0) {
|
|
try {
|
|
startTest = Integer.parseInt(args[0]);
|
|
System.out.println("提示:从测试 " + startTest + " 开始执行\n");
|
|
} catch (NumberFormatException e) {
|
|
System.out.println("提示:使用默认测试流程\n");
|
|
}
|
|
}
|
|
|
|
// 测试 1:使用无参构造方法
|
|
if (startTest <= 1) {
|
|
System.out.println("【测试 1】使用无参构造方法:");
|
|
Car car1 = new Car();
|
|
car1.showInfo();
|
|
}
|
|
|
|
// 测试 2:使用简化版构造方法
|
|
if (startTest <= 2) {
|
|
System.out.println("【测试 2】使用简化版构造方法:");
|
|
Car car2 = new Car("丰田", "卡罗拉", 15.8);
|
|
car2.showInfo();
|
|
}
|
|
|
|
// 测试 3:使用全参构造方法
|
|
if (startTest <= 3) {
|
|
System.out.println("【测试 3】使用全参构造方法:");
|
|
Car car3 = new Car("宝马", "530Li", 2024, 45.6, "黑色", "京 A88888");
|
|
car3.showInfo();
|
|
}
|
|
|
|
// 测试 4:使用 this 调用其他构造方法 + 测试 getter 方法
|
|
if (startTest <= 4) {
|
|
System.out.println("【测试 4】修改车辆信息并验证:");
|
|
Car car2 = new Car("丰田", "卡罗拉", 15.8);
|
|
car2.setBrand("Toyota");
|
|
car2.setModel("Corolla");
|
|
car2.setYear(2025);
|
|
car2.setPrice(16.5);
|
|
car2.setColor("白色");
|
|
car2.setLicensePlate("沪 C12345");
|
|
|
|
// 使用 getter 方法验证修改结果
|
|
System.out.println("验证修改结果:");
|
|
System.out.println("品牌:" + car2.getBrand());
|
|
System.out.println("型号:" + car2.getModel());
|
|
System.out.println("年份:" + car2.getYear());
|
|
System.out.println("价格:" + car2.getPrice() + "万元");
|
|
System.out.println("颜色:" + car2.getColor());
|
|
System.out.println("车牌号:" + car2.getLicensePlate());
|
|
car2.showInfo();
|
|
}
|
|
|
|
// 测试 5:数据校验测试
|
|
if (startTest <= 5) {
|
|
System.out.println("【测试 5】数据校验测试:");
|
|
Car car3 = new Car("宝马", "530Li", 2024, 45.6, "黑色", "京 A88888");
|
|
car3.setYear(1800); // 无效年份,会触发校验
|
|
car3.setPrice(-10); // 无效价格,会触发校验
|
|
car3.setBrand(""); // 空品牌,会触发校验
|
|
car3.showInfo();
|
|
}
|
|
|
|
// 测试 6:单独测试 getter 方法
|
|
if (startTest <= 6) {
|
|
System.out.println("【测试 6】测试 getter 方法:");
|
|
Car car1 = new Car();
|
|
System.out.println("car1 品牌:" + car1.getBrand());
|
|
System.out.println("car1 型号:" + car1.getModel());
|
|
System.out.println("car1 年份:" + car1.getYear());
|
|
System.out.println("car1 价格:" + car1.getPrice());
|
|
System.out.println("car1 颜色:" + car1.getColor());
|
|
System.out.println("car1 车牌号:" + car1.getLicensePlate());
|
|
}
|
|
|
|
System.out.println("\n✓ 所有测试完成!");
|
|
}
|
|
}
|
|
|
|
|