From 6518f04c8df35ab033efaef026db6e869cf202f9 Mon Sep 17 00:00:00 2001 From: zhangsiyuan <3837703520@qq.com> Date: Tue, 24 Mar 2026 12:23:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=A0=E6=80=9D=E6=B8=8A-202401070104-w3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w3/Car.java | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 w3/Car.java diff --git a/w3/Car.java b/w3/Car.java new file mode 100644 index 0000000..d84051c --- /dev/null +++ b/w3/Car.java @@ -0,0 +1,83 @@ +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 = brand; + 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 String getModel(){ + return model; + } + public double getDailyRent(){ + return this.dailyRent; + } + public boolean isRented(){ + return isRented; + } + public void setBrand(String brand){ + this.brand=brand; + } + public void setModel(String model){ + this.model=model; + } + public void setDailyRent(double dailyRent){ + if (dailyRent>0){ + this.dailyRent=dailyRent; + }else{ + System.out.println("日租金必须大于0,设置失败,保持原值。"); + } + } + //业务方法 + public void rentCar() { + if (this.isRented) { + System.out.println("车辆已租出,无法再次租用。"); + } else { + this.isRented = true; + System.out.println("车辆" + this.licensePlate + "租用成功。"); + } + } + public void returnCar() { + if (!this.isRented) { + System.out.println("车辆未被租用,无需归还。"); + } else { + this.isRented = false; + System.out.println("车辆" + this.licensePlate + "归还成功。"); + } + } + public double calculateRent(int days) { + return this.dailyRent * days; + } + //静态方法 + public static int getTotalCars() { + return totalCars; + } + //displayInfo方法 + public void displayInfo() { + String status = this.isRented ? "已租出" : "可租"; + System.out.println("车牌: " + this.licensePlate + ", 品牌: " + this.brand + + ", 型号: " + this.model + ", 日租金: " + this.dailyRent + + "元/天, 状态: " + status); + } +} \ No newline at end of file