Compare commits

...

2 Commits

  1. BIN
      out/production/Java/Car.class
  2. BIN
      out/production/Java/Testcar.class
  3. 117
      w3/Car.java
  4. 34
      w3/Testcar.java

BIN
out/production/Java/Car.class

Binary file not shown.

BIN
out/production/Java/Testcar.class

Binary file not shown.

117
w3/Car.java

@ -0,0 +1,117 @@
public class Car {
// 1. 私有属性
private final String licensePlate; // 车牌号(不可变)
private String brand; // 品牌
private String model; // 型号
private double dailyRent; // 日租金
private boolean isRented; // 是否已出租
// 静态成员(加分项):统计车辆总数
private static int totalCars = 0;
// 2. 全参构造方法
public Car(String licensePlate, String brand, String model, double dailyRent) {
this.licensePlate = licensePlate;
this.brand = brand;
this.model = model;
this.dailyRent = dailyRent;
this.isRented = false; // 初始状态:未出租
totalCars++; // 每创建一个对象,总数+1
}
// 重载构造方法:只接收车牌号、品牌、型号,日租金默认300
public Car(String licensePlate, String brand, String model) {
this(licensePlate, brand, model, 300.0); // 调用全参构造
}
// 3. Getter/Setter 方法
public String getLicensePlate() {
return licensePlate;
}
// 无setLicensePlate,保证车牌号不可修改
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getDailyRent() {
return dailyRent;
}
public void setDailyRent(double dailyRent) {
// 数据校验:新租金必须大于0
if (dailyRent > 0) {
this.dailyRent = dailyRent;
} else {
System.out.println("错误:日租金必须大于0,修改无效。");
}
}
public boolean isRented() {
return isRented;
}
// 无setIsRented,状态必须通过业务方法改变
// 静态方法:获取总车辆数
public static int getTotalCars() {
return totalCars;
}
// 4. 业务方法
/**
* 租车将车辆状态置为已出租
*/
public void rentCar() {
if (isRented) {
System.out.println("车辆已租出,无法再次租用。");
} else {
isRented = true;
System.out.println("租车成功:" + licensePlate);
}
}
/**
* 还车将车辆状态置为未出租
*/
public void returnCar() {
if (!isRented) {
System.out.println("车辆未被租用,无需归还。");
} else {
isRented = false;
System.out.println("还车成功:" + licensePlate);
}
}
/**
* 计算总租金
* @param days 租用天数假设>0不做校验
* @return 总租金
*/
public double calculateRent(int days) {
return dailyRent * days;
}
// 辅助方法:显示车辆信息
public void displayInfo() {
System.out.println("=== 车辆信息 ===");
System.out.println("车牌号:" + licensePlate);
System.out.println("品牌:" + brand);
System.out.println("型号:" + model);
System.out.println("日租金:" + dailyRent + "元/天");
System.out.println("状态:" + (isRented ? "已出租" : "可租"));
System.out.println();
}
}

34
w3/Testcar.java

@ -0,0 +1,34 @@
public class Testcar { public static void main(String[] args) {
// 1. 创建3个Car对象(使用不同构造方法)
Car car1 = new Car("京A12345", "丰田", "凯美瑞", 280.0);
Car car2 = new Car("沪B67890", "本田", "雅阁"); // 默认日租金300
Car car3 = new Car("粤C24680", "大众", "帕萨特", 320.0);
// 2. 输出所有车辆信息
System.out.println("===== 初始车辆信息 =====");
car1.displayInfo();
car2.displayInfo();
car3.displayInfo();
// 3. 测试租车/还车业务
System.out.println("===== 测试租车/还车 =====");
car1.rentCar(); // 正常租车
car1.rentCar(); // 重复租车(提示错误)
car1.returnCar(); // 正常还车
car1.returnCar(); // 重复还车(提示错误)
// 4. 计算租金(5天)
System.out.println("===== 计算租金 =====");
double rent = car1.calculateRent(5);
System.out.printf("京A12345 租用5天,总租金:%.2f元%n%n", rent);
// 5. 测试非法日租金修改
System.out.println("===== 测试非法租金修改 =====");
car1.setDailyRent(-100); // 输入非法值,提示错误
System.out.println("修改后日租金:" + car1.getDailyRent() + "元/天(保持原值)%n");
// 6. 输出总车辆数(静态成员)
System.out.println("===== 车辆总数 =====");
System.out.println("当前系统共有车辆:" + Car.getTotalCars() + "辆");
}
}
Loading…
Cancel
Save