练习3
客车类
public final class Bus extends MotoVehicle {
private int seatCount;
public Bus(String no, String brand, int seatCount) {
super(no, brand);
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public int calRent(int days) {
if (seatCount <= 16) {
return days * 800;
} else {
return days * 1500;
}
}
}
轿车类
public final class Car extends MotoVehicle {
private String type;
public Car(String no, String brand, String type) {
super(no, brand);
this.type = type;
}
public String getType() {
return type;
}
public int calRent(int days) {
if ("550i".equals(type)) {
return days * 500;
} else if ("商务舱GL8".equals(type)) {
return 600 * days;
} else {
return 300 * days;
}
}
}
汽车类(父类)
public abstract class MotoVehicle {
private String no;
private String brand;
public MotoVehicle(String no, String brand) {
this.no = no;
this.brand = brand;
}
public String getNo() {
return no;
}
public String getBrand() {
return brand;
}
public abstract int calRent(int days);
}
顾客类:
public class Customer {
String id;
String name;
public Customer(String id, String name) {
this.id=id;
this.name=name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
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;
}
}
测试类:
public class TestRent {
public static void main(String[] args) {
int days;
int totalRent;
MotoVehicle motos[] = new MotoVehicle[4];
motos[0] = new Car("冀D28588", "宝马", "550i");
motos[1] = new Car("冀DN3284", "宝马", "550i");
motos[2] = new Car("冀D43765", "别克", "商务舱GL8");
motos[3] = new Bus("冀D43765", "金龙", 34);
days = 5;
Customer customer= new Customer("s070537", "张三丰");
totalRent = customer.calcTotalRent(motos, days);
System.out.println("车牌号\t\t品牌");
for(int i=0;i<motos.length;i++){
System.out.println(motos[i].getNo()+"\t\t"+motos[i].getBrand());
}
System.out.println("\n客户名:" + customer.getName()+",租赁天数:"+days
+ ",租赁总费用:" + totalRent+"。");
}
}
练习4:
客车类:
public final class Bus extends MotoVehicle {
private int seatCount;
public Bus(String no, String brand, int seatCount) {
super(no, brand);
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public int calRent(int days) {
if (seatCount <= 16) {
return days * 800;
} else {
return days * 1500;
}
}
}
轿车类:
public final class Car extends MotoVehicle {
private String type;
public Car(String no, String brand, String type) {
super(no, brand);
this.type = type;
}
public String getType() {
return type;
}
public int calRent(int days) {
if ("550i".equals(type)) {
return days * 500;
} else if ("商务舱GL8".equals(type)) {
return 600 * days;
} else {
return 300 * days;
}
}
}
汽车类(父类):
public abstract class MotoVehicle {
private String no;
private String brand;
public MotoVehicle(String no, String brand) {
this.no = no;
this.brand = brand;
}
public String getNo() {
return no;
}
public String getBrand() {
return brand;
}
public abstract int calRent(int days);
}
卡车类:
public class Truck extends MotoVehicle {
int tonnage;// 吨位
public Truck(String no, String brand, int tonnage) {
super(no, brand);
this.tonnage = tonnage;
}
public int getTonnage() {
return tonnage;
}
/**
* 计算卡车租赁价
*/
public int calRent(int days) {
return tonnage * 50 * days;
}
}
顾客类:
public class Customer {
String id;
String name;
public Customer(String id, String name) {
this.id=id;
this.name=name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
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;
}
}
测试类:
public class TestRent {
public static void main(String[] args) {
int days;
int totalRent;
MotoVehicle motos[] = new MotoVehicle[5];
motos[0] = new Car("冀D28588", "宝马", "550i");
motos[1] = new Car("冀DN3284", "宝马", "550i");
motos[2] = new Car("冀D43765", "别克", "商务舱GL8");
motos[3] = new Bus("冀D43765", "金龙", 34);
motos[4] = new Truck("冀D56577", "解放", 30);
days = 5;
Customer customer= new Customer("s070537", "张三丰");
totalRent = customer.calcTotalRent(motos, days);
System.out.println("车牌号\t\t品牌");
for(int i=0;i<motos.length;i++){
System.out.println(motos[i].getNo()+"\t\t"+motos[i].getBrand());
}
System.out.println("\n客户名:" + customer.getName()+",租赁天数:"+days
+ ",租赁总费用:" + totalRent+"。");
}
}