Browse Source

feat(w3): 添加w3文件夹及Car.java、TeatCar.java和实验报告PDF

main
ZhengJiayin 3 weeks ago
parent
commit
285584bd56
  1. 154
      w3/Car.java
  2. BIN
      w3/Java第一次实验报告.pdf
  3. 32
      w3/TestCar.java

154
w3/Car.java

@ -0,0 +1,154 @@
// Car.java
public class Car {
// 私有属性
private final String licensePlate;
private String brand;
private String model;
private double dailyRent;
private boolean isRented;
// 静态变量:统计车辆总数
private static int totalCars = 0;
// 全参构造方法
public Car(String licensePlate, String brand, String model, double dailyRent) {
this.licensePlate = licensePlate;
this.brand = brand;
this.model = model;
setDailyRent(dailyRent);
this.isRented = false;
totalCars++;
}
// 重载构造方法(日租金默认300)
public Car(String licensePlate, String brand, String model) {
this(licensePlate, brand, model, 300.0);
}
// Getter/Setter
public String getLicensePlate() {
return licensePlate;
}
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) {
if (dailyRent > 0) {
this.dailyRent = dailyRent;
} else {
System.out.println("日租金必须大于0,设置失败!");
}
}
public boolean isRented() {
return isRented;
}
// 业务方法:租车
public void rentCar() {
if (isRented) {
System.out.println("车辆已租出,无法再次租用!");
} else {
isRented = true;
System.out.println("车辆已成功租出!");
}
}
public class Car {
private final String licensePlate;
private String brand;
private String model;
private double dailyRent;
private boolean isRented;
private static int totalCars = 0;
// 全参构造方法
public Car(String licensePlate, String brand, String model, double dailyRent) {
this.licensePlate = licensePlate;
this.brand = brand;
this.model = model;
setDailyRent(dailyRent);
this.isRented = false;
totalCars++;
}
// 重载构造方法
public Car(String licensePlate, String brand, String model) {
this(licensePlate, brand, model, 300.0);
}
// Getter/Setter
public String getLicensePlate() { return licensePlate; }
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) {
if (dailyRent > 0) {
this.dailyRent = dailyRent;
} else {
System.out.println("日租金必须大于0,设置失败!");
}
}
public boolean isRented() { return isRented; }
// 租车方法
public void rentCar() {
if (isRented) {
System.out.println("车辆已租出,无法再次租用!");
} else {
isRented = true;
System.out.println("车辆已成功租出!");
}
}
// 还车方法
public void returnCar() {
if (!isRented) {
System.out.println("车辆未被租用,无需归还!");
} else {
isRented = false;
System.out.println("车辆已成功归还!");
}
}
// 计算租金
public double calculateRent(int days) {
return dailyRent * days;
}
// 静态方法:获取总车辆数
public static int getTotalCars() {
return totalCars;
}
// 打印车辆信息
public void displayInfo() {
System.out.println("车牌号:" + licensePlate +
" | 品牌:" + brand +
" | 型号:" + model +
" | 日租金:" + dailyRent +
" | 状态:" + (isRented ? "已出租" : "可租"));
}
}

BIN
w3/Java第一次实验报告.pdf

Binary file not shown.

32
w3/TestCar.java

@ -0,0 +1,32 @@
public class TestCar {
public static void main(String[] args) {
// 创建3个Car对象
Car car1 = new Car("京A12345", "特斯拉", "Model 3", 280.0);
Car car2 = new Car("沪B67890", "比亚迪", "汉EV");
Car car3 = new Car("粤C24680", "丰田", "凯美瑞", 220.0);
// 输出车辆信息
System.out.println("=== 所有车辆信息 ===");
car1.displayInfo();
car2.displayInfo();
car3.displayInfo();
System.out.println("总车辆数:" + Car.getTotalCars());
// 测试租车/还车
System.out.println("\n=== 测试租车/还车 ===");
car1.rentCar();
car1.rentCar();
car1.returnCar();
car1.returnCar();
// 测试租金计算
System.out.println("\n=== 测试租金计算 ===");
double rent = car2.calculateRent(5);
System.out.println("租用5天总租金:" + rent + "元");
// 测试日租金校验
System.out.println("\n=== 测试日租金校验 ===");
car3.setDailyRent(-100.0); // 改为double类型字面量
System.out.println("修改后日租金:" + car3.getDailyRent());
}
}
Loading…
Cancel
Save