继承、封装和多态的一个简单应用(答答租车系统)
Car.java
package com.imooc;
//使用面向对象的封装特性
public class Car {
// 描述汽车可能有特征
private String name; // 车的名称
private double cargoCapacity;// 车的载货量
private int busLoad;// 车的载客量
private int dailyRent;// 车的日租金
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public int getBusLoad() {
return busLoad;
}
public void setBusLoad(int busLoad) {
this.busLoad = busLoad;
}
public int getDailyRent() {
return dailyRent;
}
public void setDailyRent(int dailyRent) {
this.dailyRent = dailyRent;
}
}
PassengerCar.java
package com.imooc;
//使用面向对象的继承特性
public class PassengerCar extends Car {
private String name; // 车的名称
private int busLoad;// 车的载客量
private int dailyRent;// 车的日租金
// 客车的构造方法
public PassengerCar(String name, int busLoad, int dailyRent) {
this.name = name;
this.busLoad = busLoad;
this.dailyRent = dailyRent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBusLoad() {
return busLoad;
}
public void setBusLoad(int busLoad) {
this.busLoad = busLoad;
}
public int getDailyRent() {
return dailyRent;
}
public void setDailyRent(int dailyRent) {
this.dailyRent = dailyRent;
}
}
Pickup.java
package com.imooc;
public class Pickup extends Car { // 即可载客又可载人
private String name; // 车的名称
private double cargoCapacity;// 车的载货量
private int busLoad;// 车的载客量
private int dailyRent;// 车的日租金
//构造函数
public Pickup(String name, double cargoCapacity, int busLoad, int dailyRent) {
this.name = name;
this.cargoCapacity = cargoCapacity;
this.busLoad = busLoad;
this.dailyRent = dailyRent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public int getBusLoad() {
return busLoad;
}
public void setBusLoad(int busLoad) {
this.busLoad = busLoad;
}
public int getDailyRent() {
return dailyRent;
}
public void setDailyRent(int dailyRent) {
this.dailyRent = dailyRent;
}
}
Test.java
package com.imooc;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统!");
System.out.println("您是否需要租车:1 是 0否");
Scanner input = new Scanner(System.in);
int is = input.nextInt();
// 创建一个Car类数组来保存各种车,利用引用多态来创建不同的子类对象
Car[] cars = { new PassengerCar("奥迪", 5, 500),
new Truck("小货车", 5, 150), new PassengerCar("奔驰", 5, 700),
new Pickup("皮卡", 1.5, 4, 200), new Truck("大型货车", 10, 600),
new PassengerCar("大客车", 25, 650) };
if (is == 1) {
int totalMoney = 0;
double totalcargoCapacity = 0;
int totalbusLoad = 0;
int rentCarDays = 0;
int totalDailyMoney = 0;
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t" + "汽车名称\t\t" + "日租金\t\t" + "容量\t");
int num = 1;// 定义初始序列号
for (Car car : cars) {
if (car instanceof PassengerCar) {
System.out.println("No." + num + '\t' + car.getName()
+ "\t\t" + car.getDailyRent() + "元/天\t\t" + "载人:"
+ car.getBusLoad() + "人");
num++;
}
if (car instanceof Truck) {
System.out.println("No." + num + '\t' + car.getName()
+ "\t\t" + car.getDailyRent() + "元/天\t\t" + "载货:"
+ car.getCargoCapacity() + "吨");
num++;
}
if (car instanceof Pickup) {
System.out.println("No." + num + '\t' + car.getName()
+ "\t\t" + car.getDailyRent() + "元/天\t\t" + "载人:"
+ car.getBusLoad() + "人" + "载货:"
+ car.getCargoCapacity() + "吨");
num++;
}
}
System.out.println("请输入您的租车数量(最大租车数为6):");
Scanner sc = new Scanner(System.in);
int carNum = sc.nextInt();
if (carNum > 0 && carNum <= 6) {
int[] rentCar = new int[carNum];
for (int i = 0; i < carNum; i++) {
System.out.println("请输入第" + (i + 1) + "辆车的序号:");
Scanner sc2 = new Scanner(System.in);
int rentCarNum = sc2.nextInt();
rentCar[i] = rentCarNum; // 保存输入的租车序号
totalDailyMoney += cars[rentCarNum - 1].getDailyRent();
totalcargoCapacity += cars[rentCarNum - 1]
.getCargoCapacity();
totalbusLoad += cars[rentCarNum - 1].getBusLoad();
}
System.out.println("请输入租车的天数:");
Scanner sc3 = new Scanner(System.in);
rentCarDays = sc3.nextInt();
totalMoney = totalDailyMoney * rentCarDays;
// 载人账单
System.out.println("--------可载人车辆--------:");
for (int i = 0; i < rentCar.length; i++) {
if (cars[rentCar[i] - 1] instanceof PassengerCar
|| cars[rentCar[i] - 1] instanceof Pickup) {
System.out.print(cars[rentCar[i] - 1].getName()+'\t');
}
}
System.out.println();
// 载货账单
System.out.println("--------可载货车辆--------:");
for (int i = 0; i < rentCar.length; i++) {
if (cars[rentCar[i] - 1] instanceof Truck
|| cars[rentCar[i] - 1] instanceof Pickup) {
System.out.print(cars[rentCar[i] - 1].getName()+'\t');
}
}
System.out.println();
System.out.println("共载货:" + totalcargoCapacity+"\t共载人:" + totalbusLoad+"\t总价格:" + totalMoney);
} else {
System.out.println("请修改租车数量!");
}
} else {
System.out.println("感谢您访问答答租车系统!");
}
}
}
运行结果:
欢迎使用答答租车系统!
您是否需要租车:1 是 0否
1
您可租车的类型及其价目表:
序号 汽车名称 日租金 容量
No.1 奥迪 500元/天 载人:5人
No.2 小货车 150元/天 载货:5.0吨
No.3 奔驰 700元/天 载人:5人
No.4 皮卡 200元/天 载人:4人载货:1.5吨
No.5 大型货车 600元/天 载货:10.0吨
No.6 大客车 650元/天 载人:25人
请输入您的租车数量(最大租车数为6):
3
请输入第1辆车的序号:
1
请输入第2辆车的序号:
2
请输入第3辆车的序号:
4
请输入租车的天数:
2
--------可载人车辆--------:
奥迪 皮卡
--------可载货车辆--------:
小货车 皮卡
共载货:6.5 共载人:9 总价格:1700