0
点赞
收藏
分享

微信扫一扫

使用多态完善汽车租赁系统计价功能 ----一次租借多辆车


一、计算一次租赁多辆汽车的总租金

(一) 需求说明
在前一章(继承)汽车租赁系统的基础上,即已经实现了汽车租赁系统的简单计价功能,客户可以租赁一辆某种型号的汽车若干天。现在要增加需求:

客户可以一次租赁多辆不同品牌的不同型号的汽车若干天,要求计算出总租赁价。

假设有一客户要租赁:

2辆宝马

1辆别克商务舱

1辆金龙(34)座

租5天共多少租金?

MotoVehicle 类

//抽象类
public abstract class MotoVehicle {
//属性
private String no;
private String brand;
private String type;
/**
* set、get
* @return
*/
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
* 构造方法
*/
public MotoVehicle() {
super();
}
public MotoVehicle(String no) {
this.no = no;
}
public MotoVehicle(String no, String brand) {
this.no = no;
this.brand = brand;
}
/**
* 抽象方法
* @return
*/
public abstract int calRent(int days);
}

Car类:

public final class Car extends MotoVehicle {

private String type;
/**
* 构造方法
*/
public Car() {
super();
}
public Car(String no,String type) {
super(no);
this.type=type;
}
/**
* set\get
*/
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
* 租金计算
*/
public int calRent(int days) {
int sum = 0;
if ("宝马550i".equals(type)) {
sum = 500 * days;
} else if ("别克商务舱GLB3".equals(type)) {
sum = 600 * days;
} else if ("别克林荫大道".equals(type)) {
sum = 300 * days;
}
return sum;
}
}

Bus类:

public final class Bus extends MotoVehicle {

public int seatCount;
/**
* 构造方法
*/
public Bus() {
super();
}
public Bus(String no, String brand) {
super(no, brand);
}
public Bus(String no, String brand,int seatCount) {
super(no, brand);
this.seatCount = seatCount;
}
/**
* 租金计算
*/
public int calRent(int days) {
int sum=0;
if (seatCount <= 16) {
sum = 800 * days;
} else if (seatCount > 16) {
sum =1500 * days;
}
return sum;
}
}

Customer类:

public class Customer {
String name;
/**
* 构造方法
* @param name
*/
public Customer(String name){
this.name = name;
}
/**
* 总租金计算
* @param motos
* @param days
* @return
*/
public int calcTotalRent(MotoVehicle motos[],int days){
int sum=0;
for(int i=0;i<motos.length;i++){
sum+=motos[i].calRent(days);
}
return sum;
}
}

TestRent类:

public class TestRent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int days=5; //租赁天数
int totalRent;// 总租赁费用
//客户租用的多辆汽车信息及租用天数
MotoVehicle[] motos = new MotoVehicle[4];
motos[0] = new Car("京NY28588","宝马550i");
motos[1] = new Car("京NNN3284","宝马550i");
motos[2] = new Car("京NT43765","别克林荫大道");
motos[3] = new Bus("京5643765","金龙",34);
//2、计算总租赁费用
Customer customer = new Customer("沈伟");
totalRent = customer.calcTotalRent(motos, days);
// 输出客户姓名和总租赁费用
System.out.println("汽车牌号"+"\t\t"+"汽车品牌");
for(int i=0;i<motos.length;i++){
if(i<3){
System.out.println(motos[i].getNo()+"\t"+motos[i].getType());
}else{
System.out.println(motos[i].getNo()+"\t"+motos[i].getBrand());
}
}

System.out.println("客户名:"+customer.name+",租赁天数:"+
days+"天,租赁费用:"+totalRent+"元。");
}
}

使用多态完善汽车租赁系统计价功能 ----一次租借多辆车_构造方法


添加Truck类:

public class Truck extends MotoVehicle {
public int tonnage;
/**
* 构造方法
*/
public Truck() {
super();
}
public Truck(String no, String brand,int tonnage) {
super(no, brand);
this.tonnage = tonnage;
}
/**
* 租金计算
*/
public int calRent(int days) {
int sum = 0;
sum = tonnage * 50 * days;
return sum;
}
}

测试:
public class TestRent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int days=5; //租赁天数
int totalRent;// 总租赁费用
//客户租用的多辆汽车信息及租用天数
MotoVehicle[] motos = new MotoVehicle[5];
motos[0] = new Car("京NY28588","宝马550i");
motos[1] = new Car("京NNN3284","宝马550i");
motos[2] = new Car("京NT43765","别克林荫大道");
motos[3] = new Bus("京5643765","金龙",34);
motos[4] = new Truck("京GD56577","解放",30);
//2、计算总租赁费用
Customer customer = new Customer("沈伟");
totalRent = customer.calcTotalRent(motos, days);
// 输出客户姓名和总租赁费用
System.out.println("汽车牌号"+"\t\t"+"汽车品牌");
for(int i=0;i<motos.length;i++){
if(i<3){
System.out.println(motos[i].getNo()+"\t"+motos[i].getType());
}else if(i==3){
System.out.println(motos[i].getNo()+"\t"+motos[i].getBrand());
}else if(i==4){
System.out.println(motos[i].getNo()+"\t"+motos[i].getBrand());
}
}

System.out.println("客户名:"+customer.name+",租赁天数:"+
days+"天,租赁费用:"+totalRent+"元。");
}
}

使用多态完善汽车租赁系统计价功能 ----一次租借多辆车_构造方法_02


在教材上我按照前一个汽车租赁的价钱算了一下,应该是14000元,可能有些教材数据有些偏差~

在写这道题时,我遇到了两个难点:
一、在哪些地方需要转型(利用多态)

MotoVehicle[] motos = new MotoVehicle[5];
motos[0] = new Car("京NY28588","宝马550i");
motos[1] = new Car("京NNN3284","宝马550i");
motos[2] = new Car("京NT43765","别克林荫大道");
motos[3] = new Bus("京5643765","金龙",34);
motos[4] = new Truck("京GD56577","解放",30);

这里转型是最容易想到的,因为要存储的信息会对应到具体的子类实例化。
另外就是在最后输出信息时,需不需要进行转型。因为,最开始时在父类中只设置了brand属性​no属性​,而​type属性​是单独放置在Car类中的,我就在思考需不需要进行转型,去调用Car类中的type,完成信息输出。
但是有个知识点忽略了:​向上转型只能调用父类定义过的且子类重写了的方法​​
所以结合Bus类,可以看到type属性也需要在父类中定义一旦这样理解,那么最后在输出信息时,也就不需要进行转型了。

二、在最后输出信息时,怎么去调出type

使用多态完善汽车租赁系统计价功能 ----一次租借多辆车_i++_03

for(int i=0;i<motos.length;i++){
if(i<3){
System.out.println(motos[i].getNo()+"\t"+motos[i].getType());
}else if(i==3){
System.out.println(motos[i].getNo()+"\t"+motos[i].getBrand());
}else if(i==4){
System.out.println(motos[i].getNo()+"\t"+motos[i].getBrand());
}
}

类似brand的调用方法,当在父类中定义了type属性时,其实就和brand采取一样的方法了。


举报

相关推荐

0 条评论