package homeworks.www;
/**
*
* 汽车类 (父类)
* 描述: 客户一次租赁多辆不同品牌的不同型号的汽车若干天,计算总租金
* 轿车
* 别克商务仓GL8(日租费600) 宝马550i(日租费500) 别克林荫大道 (日租费300)
* 客车
* 16座以下(日租费800) 16座以上(日租费1500)
*
*/
public abstract class MotoVehicle {
private String no;//车牌号
private String brand;//品牌(别克 宝马 金杯 金龙)
/**
*
* getter ,setter方法
*/
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 MotoVehicle(){
}
public MotoVehicle(String no,String brand){
this.brand=brand;
this.no=no;
}
/**
*
* @param days 租赁的天数
* @return 返回租赁的费用
* 定义抽象方法,子类重写
*/
public abstract int calRent(int days);
}
package homeworks.www;
/**
*
* 轿车类继承汽车类
*
*/
public final class Car extends MotoVehicle {
private String type;//型号属性有商务舱GL8,550i, 林荫大道
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Car() {
super();
}
public Car(String no, String brand, String type) {
super(no, brand);
this.type = type;
}
/**
* 重写父类抽象方法(轿车类计算租金)
*/
public int calRent(int days) {
int money=0;
if (super.getBrand().equals("宝马")) {
money=days*500;
}else if (super.getBrand().equals("别克")&&type.equals("商务舱GL8")) {
money=days*600;
}else {
money=days*300;
}
return money;
}
}
package homeworks.www;
/**
*
* 客车类继承汽车类
*
*/
public final class Bus extends MotoVehicle {
private int seataCount;//座位数
public int getSeataCount() {
return seataCount;
}
public void setSeataCount(int seataCount) {
this.seataCount = seataCount;
}
public Bus(){
}
public Bus(String no, String brand, int seataCount) {
super(no, brand);
this.seataCount = seataCount;
}
/**
* 重写父类抽象方法(汽车类计算租金)
*/
public int calRent(int days) {
int money=0;
if (seataCount<=16) {
money=days*800;
}else {
money=days*1500;
}
return money;
}
}
package homeworks.www;
/**
*
* 顾客类(计算总租金)
*
*/
public class Customer {
public int calTotalRent(MotoVehicle motos[],int days){
int sum=0;
for (int i = 0; i < motos.length; i++) {
sum+=motos[i].calRent(days);
}
System.out.println("总租金为:"+sum);
return sum;
}
}
package homeworks.www;
public class TestM {
public static void main(String[] args) {
int days=5;
MotoVehicle[] motos=new MotoVehicle[4];
Customer customer=new Customer();
motos[0]=new Car("京NY28588", "宝马", "550i");
motos[1]=new Car("京NNN3284", "宝马", "550i");
motos[2]=new Car("京NT4375", "别克", "林荫大道");
motos[3]=new Bus("京5643765", "金龙", 34);
customer.calTotalRent(motos, days);
}
}