From eec4e2a351a7f4e178e6389727520f3b18893b50 Mon Sep 17 00:00:00 2001 From: dengxitong <2452879460@qq.com> Date: Sun, 31 May 2026 12:35:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'w3/car.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w3/car.java | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 w3/car.java diff --git a/w3/car.java b/w3/car.java new file mode 100644 index 0000000..f584514 --- /dev/null +++ b/w3/car.java @@ -0,0 +1,88 @@ +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); + } + + 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) { + isRented = true; + System.out.println(licensePlate + " 租车成功!"); + } else { + System.out.println(licensePlate + " 车辆已租出,无法再次租用"); + } + } + + public void returnCar() { + if (isRented) { + isRented = false; + System.out.println(licensePlate + " 还车成功!"); + } else { + System.out.println(licensePlate + " 车辆未被租用,无需归还"); + } + } + public double calculateRent(int days) { + return dailyRent * days; + } + public static int getTotalCars() { + return totalCars; + } + 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("=================\n"); + } +} +