星光不问赶路人,时光不负追梦人
1、汽车抽象类
package vehicle;
//汽车类
public abstract class Automobile {
// 车牌号 品牌 日租金
private String licensePlateNumber;
private String brand;
private int dailyRent;
public String getLicensePlateNumber() {
return licensePlateNumber;
}
public void setLicensePlateNumber(String licensePlateNumber) {
this.licensePlateNumber = licensePlateNumber;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getDailyRent() {
return dailyRent;
}
public void setDailyRent(int dailyRent) {
this.dailyRent = dailyRent;
}
public Automobile() {
super();
}
public Automobile(String licensePlateNumber, String brand, int dailyRent) {
super();
this.licensePlateNumber = licensePlateNumber;
this.brand = brand;
this.dailyRent = dailyRent;
}
// 计算租金(抽象方法)
public abstract float calculation(int days);
2.继承汽车抽象类的轿车类
package vehicle;
//轿车类
public class Car extends Automobile {
// 型号
private String model;
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public Car() {
super();
}
public Car(String licensePlateNumber, String brand, int dailyRent,
String model) {
super(licensePlateNumber, brand, dailyRent);
this.model = model;
}
@Override
public float calculation(int days) {
float price = this.getDailyRent() * days;
if (days > 7 && days <= 30) {
price *= 0.9f;
}
if (days > 30 && days <= 150) {
price *= 0.8f;
}
if (days > 150) {
price *= 0.7f;
}
return price;
}
}
3.继承汽车抽象类的客车类
package vehicle;
//客车类
public class Bus extends Automobile {
// 座位数
private int seatingCapacity;
public int getSeatingCapacity() {
return seatingCapacity;
}
public void setSeatingCapacity(int seatingCapacity) {
this.seatingCapacity = seatingCapacity;
}
public Bus() {
super();
}
public Bus(String licensePlateNumber, String brand, int dailyRent,
int seatingCapacity) {
super(licensePlateNumber, brand, dailyRent);
this.seatingCapacity = seatingCapacity;
}
@Override
public float calculation(int days) {
float price = this.getDailyRent() * days;
if (days >= 3 && days < 7) {
price *= 0.9f;
}
if (days >= 7 && days < 30) {
price *= 0.8f;
}
if (days >= 30 && days < 150) {
price *= 0.7f;
}
if (days >= 150) {
price *= 0.6f;
}
return price;
}
}
4、创建汽车业务类
package system;
import vehicle.Automobile;
import vehicle.Bus;
import vehicle.Car;
//汽车业务类
public class Business {
// 汽车类型的数组,将该数组声明为父类类型
public Automobile[] motos = new Automobile[8];
// 初始化汽车信息
public void init() {
motos[0] = new Car("京NY28588", "宝马", 800, "X6");
motos[1] = new Car("京CNY3284", "宝马", 600, "550i");
motos[2] = new Car("京NT37465", "别克", 300, "林荫大道");
motos[3] = new Car("京NT96968", "别克", 600, "GLB");
motos[4] = new Bus("京6566754", "金杯", 800, 16);
motos[5] = new Bus("京8696997", "金龙", 800, 16);
motos[6] = new Bus("京9696996", "金杯", 1500, 34);
motos[7] = new Bus("京8696998", "金龙", 1500, 34);
}
// 租车:根据用户提供的条件去汽车数组中查找相应车辆并返回
public Automobile carRental(String brand, String model, int seatingCapacity) {
Automobile moto = null;
for (Automobile m : motos) {
if (m instanceof Car) {
Car car = (Car) m;
if (car.getBrand().equals(brand)
&& car.getModel().equals(model)) {
moto = car;
break;
}
} else {
Bus bus = (Bus) m;
if (bus.getBrand().equals(brand)
&& bus.getSeatingCapacity() == seatingCapacity) {
moto = bus;
break;
}
}
}
return moto;
}
}
5、创建测试类
package system;
import java.util.Scanner;
import vehicle.Automobile;
//汽车租赁管理类
public class LeaseManagement {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Business motoMgr = new Business();
// 租赁公司首先需要购置汽车
motoMgr.init();
System.out
.println("-----------------------欢迎光临租赁公司----------------------");
System.out.println("1 轿车\t2 客车");
System.out.print("请选择你要租赁的汽车类型:");
int num = input.nextInt();
String brand = "";// 品牌
String model = "";// 型号
int seatingCapacity = 0;// 座位数
if (num == 1) {
// 租赁轿车
System.out.println("请选择您要租赁的桥车品牌:1、别克 2、宝马 ");
int choose = input.nextInt();
if (choose == 1) {
brand = "别克";
System.out.println("请选择您要租赁的汽车型号:1、林荫大道 2、GLB");
model = (input.nextInt() == 1) ? "林荫大道" : "GLB";
} else if (choose == 2) {
brand = "宝马";
System.out.println("请选择您要租赁的汽车型号:1、X6 2、550i");
model = (input.nextInt() == 1) ? "X6" : "550i";
}
} else if (num == 2) {
// 租赁客车
model = "";
System.out.println("请选择您要租赁的桥车品牌:1、金杯 2、金龙 ");
int choose = input.nextInt();
if (choose == 1) {
brand = "金杯";
System.out.println("请选择您要租赁的汽车座位数:1、16座 2、34座");
seatingCapacity = (input.nextInt() == 1) ? 16 : 34;
} else if (choose == 2) {
brand = "金龙";
System.out.println("请选择您要租赁的汽车座位数:1、16座 2、34座");
seatingCapacity = (input.nextInt() == 1) ? 16 : 34;
}
}
// 租车
Automobile moto = motoMgr.carRental(brand, model, seatingCapacity);
System.out.println("请输入您的租赁天数:");
int days = input.nextInt();
double money = moto.calculation(days);
System.out.println("租车成功:请按照如下车牌去提车:" + moto.getLicensePlateNumber());
System.out.println("您需要支付:" + money + "元");
}
}
6、实现效果