Browse Source

上传文件至 'w3'

main
yuangandong 4 weeks ago
parent
commit
622db79633
  1. 78
      w3/w3car
  2. 31
      w3/w3testcar
  3. BIN
      w3/实验报告.docx

78
w3/w3car

@ -0,0 +1,78 @@
package com.rental;
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;
this.dailyRent = 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 void returnCar() {
if (!isRented) {
System.out.println("车辆未被租用,无需归还");
} else {
isRented = false;
System.out.println("车辆已成功归还");
}
}
public double calculateRent(int days) {
return dailyRent * days;
}
public void displayInfo() {
System.out.println("车牌号:" + licensePlate +
",品牌:" + brand +
",型号:" + model +
",日租金:" + dailyRent +
"元/天,状态:" + (isRented ? "已租出" : "可租"));
}
public static int getTotalCars() {
return totalCars;
}
}

31
w3/w3testcar

@ -0,0 +1,31 @@
package com.rental;
public class TestCar {
public static void main(String[] args) {
Car car1 = new Car("豫A12345", "比亚迪", "汉EV", 280.0);
Car car2 = new Car("豫B67890", "特斯拉", "Model 3");
Car car3 = new Car("豫C54321", "丰田", "凯美瑞", 220.0);
System.out.println("=== 初始车辆信息 ===");
car1.displayInfo();
car2.displayInfo();
car3.displayInfo();
System.out.println("\n=== 测试租车/还车 ===");
car1.rentCar();
car1.rentCar();
car1.returnCar();
car1.returnCar();
System.out.println("\n=== 计算5天租金 ===");
double rent = car2.calculateRent(5);
System.out.println("车辆" + car2.getLicensePlate() + "租赁5天总租金:" + rent + "元");
System.out.println("\n=== 测试非法日租金修改 ===");
car3.setDailyRent(-100);
System.out.println("修改后日租金:" + car3.getDailyRent() + "元/天(保持原值)");
System.out.println("\n=== 总车辆数 ===");
System.out.println("当前系统中车辆总数:" + Car.getTotalCars() + "辆");
}
}

BIN
w3/实验报告.docx

Binary file not shown.
Loading…
Cancel
Save