在Java中,综合:各位面向对象的小伙伴们,在学习了面向对象的校心概念——类的封装、 承、多态之后,答答租车系统开始营运了。请你充分利用面向对象思想,为公司解决智能租车问题,根据客户选定的车型和租车天数,来计算租车费用,最大载客人数,最大载载更量 公司现有三种车型(客车、皮卡车、货车),每种车都有名称和租金的属性;其中:客车只能载人,货车只能载货,皮卡车是客货两用车,即可以载人,也可以载货。 下面是答答租车公司的可用车型、容量及价目表: 序号 1 2 3 4 5 6 7 车型名称 A 载客量(人) 租金(元/天) B C D E F 5 5 51 55 5 8 G 9 H 5 10 800 400 800 1300 1500 500 450 200 1500 2000 J 0.45 2.0 3 25 35 要求:根据客户输入的所租车型的序号及天数,计算所能乘载的总人数
时间: 2023-06-18 17:07:12 浏览: 116
首先,我们需要设计一个车辆类(Vehicle),包括车辆名称(name)、租金(rent)等属性,以及能够计算载人量(getPassengerCapacity)和载货量(getCargoCapacity)的方法。
```java
public abstract class Vehicle {
private String name;
private int rent;
public Vehicle(String name, int rent) {
this.name = name;
this.rent = rent;
}
public String getName() {
return name;
}
public int getRent() {
return rent;
}
public abstract int getPassengerCapacity();
public abstract double getCargoCapacity();
}
```
接着,我们设计三个具体的车辆类,分别为客车类(Bus)、皮卡车类(PickupTruck)和货车类(Truck),它们继承自车辆类(Vehicle),并实现父类中的抽象方法。
```java
public class Bus extends Vehicle {
private int passengerCapacity;
public Bus(String name, int rent, int passengerCapacity) {
super(name, rent);
this.passengerCapacity = passengerCapacity;
}
@Override
public int getPassengerCapacity() {
return passengerCapacity;
}
@Override
public double getCargoCapacity() {
return 0;
}
}
public class PickupTruck extends Vehicle {
private int passengerCapacity;
private double cargoCapacity;
public PickupTruck(String name, int rent, int passengerCapacity, double cargoCapacity) {
super(name, rent);
this.passengerCapacity = passengerCapacity;
this.cargoCapacity = cargoCapacity;
}
@Override
public int getPassengerCapacity() {
return passengerCapacity;
}
@Override
public double getCargoCapacity() {
return cargoCapacity;
}
}
public class Truck extends Vehicle {
private double cargoCapacity;
public Truck(String name, int rent, double cargoCapacity) {
super(name, rent);
this.cargoCapacity = cargoCapacity;
}
@Override
public int getPassengerCapacity() {
return 0;
}
@Override
public double getCargoCapacity() {
return cargoCapacity;
}
}
```
接下来,我们设计租车系统类(CarRentalSystem),它包含一个车辆列表(vehicles),以及能够根据客户选定的车型和租车天数,来计算租车费用(calculateRent)和最大载客人数(calculateMaxPassengerCapacity)的方法。
```java
import java.util.ArrayList;
import java.util.List;
public class CarRentalSystem {
private List<Vehicle> vehicles;
public CarRentalSystem() {
vehicles = new ArrayList<>();
vehicles.add(new Bus("A", 51, 5));
vehicles.add(new Bus("B", 55, 5));
vehicles.add(new PickupTruck("C", 800, 5, 0.45));
vehicles.add(new PickupTruck("D", 400, 2, 2.0));
vehicles.add(new PickupTruck("E", 800, 3, 3.0));
vehicles.add(new Truck("F", 1300, 25));
vehicles.add(new Truck("G", 1500, 35));
vehicles.add(new Truck("H", 500, 5));
vehicles.add(new Truck("J", 450, 10));
vehicles.add(new Truck("K", 200, 0));
vehicles.add(new Truck("L", 1500, 0));
}
public int calculateRent(int vehicleIndex, int days) {
Vehicle vehicle = vehicles.get(vehicleIndex - 1);
return vehicle.getRent() * days;
}
public int calculateMaxPassengerCapacity(int vehicleIndex) {
Vehicle vehicle = vehicles.get(vehicleIndex - 1);
return vehicle.getPassengerCapacity();
}
}
```
最后,我们可以编写一个测试类(Test),调用租车系统类中的方法来计算租车费用和最大载客人数。
```java
public class Test {
public static void main(String[] args) {
CarRentalSystem carRentalSystem = new CarRentalSystem();
int vehicleIndex = 1; // 客车 A
int days = 3;
int rent = carRentalSystem.calculateRent(vehicleIndex, days);
int maxPassengerCapacity = carRentalSystem.calculateMaxPassengerCapacity(vehicleIndex);
System.out.println("租 " + carRentalSystem.getVehicleName(vehicleIndex) + " " + days + " 天,租金为 " + rent + " 元,最大载客人数为 " + maxPassengerCapacity + " 人。");
}
}
```
输出结果为:
```
租 A 3 天,租金为 153 元,最大载客人数为 5 人。
```
阅读全文