1.题目内容及要求
2. 代码实现
(1)main方法
package com.lut.rent;
import java.util.Scanner;
public class TestRent {
public static void main(String[] args) {
String no, brand, mytype, type, seatCount;
int x, days, rent;
int money = 0;
Car car;
Bus bus;
Scanner input = new Scanner(System.in);
System.out.println("欢迎来到汽车租赁公司!");
System.out.println("请输入要租赁的汽车种类:");
x = input.nextInt();
for (int i = 1; i <= x; i++) {
System.out.println("请输入第" + i + "种车要租赁的天数:");
days = input.nextInt();
System.out.println("请输入要租赁的汽车类型(1:轿车 2:客车)");
mytype = input.next();
if ("1".equals(mytype)) {
System.out.println("请输入要租的汽车品牌(1:宝马 2:别克)");
brand = input.next();
System.out.println("请输入汽车的型号");
if ("1".equals(brand)) {
System.out.println("宝马车(1:550i)");
} else {
System.out.println("别克车 (2:商务舱GL8 3:林荫大道)");
}
type = input.next();
car = new Car(brand, type);
rent = car.calRent(days);
} else {
System.out.println("请输入要租的客车品牌(1:金杯 2:金龙)");
brand = input.next();
System.out.println("请输入客车座位数的类型:(1:<=16座 2: >16座)");
seatCount = input.next();
bus = new Bus(brand, seatCount);
rent = bus.calRent(days);
}
System.out.println("顾客您好,您需要支付的第" + i + "种车费用是" + rent + "。");
money = money + rent;
}
System.out.println("顾客您好,您需要支付的车辆的总费用是" + money + "元。");
}
}
(2) 父类
package com.lut.rent;
public abstract class MotoVehicle {
private String brand
public MotoVehicle() {
}
public MotoVehicle(String brand) {
this.brand = brand;
}
public abstract int calRent(int days);
}
(3)Car子类
package com.lut.rent;
public final class Car extends MotoVehicle {
private String type;
public Car() {
}
public Car(String brand, String type) {
super(brand);
this.type = type;
}
public String getType() {
return type;
}
public int calRent(int days) {
if ("1".equals(type)) {
return days * 500;
} else if ("2".equals(type)) {
return days * 600;
} else {
return days * 300;
}
}
}
(4)Bus子类
package com.lut.rent;
public final class Bus extends MotoVehicle {
private String seatCount;
public Bus() {
}
public Bus(String brand, String seatCount) {
super(brand);
this.seatCount = seatCount;
}
public String getType() {
return seatCount;
}
public int calRent(int days) {
if ("1".equals(seatCount)) {
return days * 800;
} else {
return days * 1500;
}
}
}
3.输出实例
欢迎来到XXX汽车租赁公司!
请输入要租赁的汽车种类:
2
请输入第1种车要租赁的天数:
10
请输入要租赁的汽车类型(1:轿车 2:客车)
1
请输入要租的汽车品牌(1:宝马 2:别克)
1
请输入汽车的型号
宝马车(1:550i)
1
顾客您好,您需要支付的第1种车费用是5000。
请输入第2种车要租赁的天数:
5
请输入要租赁的汽车类型(1:轿车 2:客车)
2
请输入要租的客车品牌(1:金杯 2:金龙)
2
请输入客车座位数的类型:(1:<=16座 2: >16座)
2
顾客您好,您需要支付的第2种车费用是7500。
顾客您好,您需要支付的车辆的总费用是12500元。