Browse Source

上传文件至 'W3'

main
JiaoYan 4 weeks ago
parent
commit
a8c28ba736
  1. BIN
      W3/Car.class
  2. 77
      W3/Car.java
  3. BIN
      W3/TestCar.class
  4. 25
      W3/TestCar.java
  5. BIN
      W3/实验报告.doc

BIN
W3/Car.class

Binary file not shown.

77
W3/Car.java

@ -0,0 +1,77 @@
package com.rental;
public class Car {
private 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++;
}
public Car(String licensePlate, String brand, String model) {
this(licensePlate, brand, model, 300.0);
}
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("Daily rent must be greater than 0, modification failed");
}
}
public boolean isRented() {
return isRented;
}
public void rentCar() {
if (isRented) {
System.out.println("The car is already rented and cannot be rented again");
} else {
isRented = true;
System.out.println("Car rented successfully");
}
}
public void returnCar() {
if (!isRented) {
System.out.println("The car is not rented, no need to return");
} else {
isRented = false;
System.out.println("Car returned successfully");
}
}
public double calculateRent(int days) {
return dailyRent * days;
}
public static int getTotalCars() {
return totalCars;
}
public void displayInfo() {
System.out.println("License Plate: " + licensePlate
+ ", Brand: " + brand
+ ", Model: " + model
+ ", Daily Rent: " + dailyRent
+ ", Status: " + (isRented ? "Rented" : "Available"));
}
}

BIN
W3/TestCar.class

Binary file not shown.

25
W3/TestCar.java

@ -0,0 +1,25 @@
package com.rental;
public class TestCar {
public static void main(String[] args) {
Car car1 = new Car("XiangA12345", "BYD", "Han EV", 280.0);
Car car2 = new Car("XiangB67890", "Tesla", "Model 3");
Car car3 = new Car("XiangC00001", "Toyota", "Camry", 350.0);
System.out.println("=== Initial Car Information ===");
car1.displayInfo();
car2.displayInfo();
car3.displayInfo();
System.out.println("\n=== Test Rent/Return ===");
car1.rentCar();
car1.rentCar();
car1.returnCar();
car1.returnCar();
System.out.println("\n=== Calculate Rent ===");
double total = car2.calculateRent(5);
System.out.println("car2 5-day rent: " + total);
System.out.println("\n=== Test Invalid Rent ===");
car3.setDailyRent(-100);
System.out.println("car3 rent: " + car3.getDailyRent());
System.out.println("\n=== Total Cars ===");
System.out.println("Total: " + Car.getTotalCars());
}
}

BIN
W3/实验报告.doc

Binary file not shown.
Loading…
Cancel
Save