diff --git a/W3/Car.class b/W3/Car.class new file mode 100644 index 0000000..bc990ff Binary files /dev/null and b/W3/Car.class differ diff --git a/W3/Car.java b/W3/Car.java new file mode 100644 index 0000000..86c960f --- /dev/null +++ b/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")); + } +} \ No newline at end of file diff --git a/W3/TestCar.class b/W3/TestCar.class new file mode 100644 index 0000000..54492ef Binary files /dev/null and b/W3/TestCar.class differ diff --git a/W3/TestCar.java b/W3/TestCar.java new file mode 100644 index 0000000..553249c --- /dev/null +++ b/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()); + } +} \ No newline at end of file diff --git a/W3/实验报告.doc b/W3/实验报告.doc new file mode 100644 index 0000000..77a41ed Binary files /dev/null and b/W3/实验报告.doc differ