你好车辆共享公司现有两种共享车辆,即共享汽车和共享单车。共享汽车包含车牌号(String类型)和售价(double类型),共享单车包含车牌号(String类型)。不同车辆的租金计算方式不同,具体计算方式如下:
1、共享汽车,租金天计算,每天租金为售价的0.1%;
2、共享单车,租金天计算,每天租金为12元。
现需要您构造上述共享车辆的继承体系,他们均继承自你好车辆类(HiINC),且提供方法getRent()返回租金。在main函数中构造了HiINC数组,包含共享汽车和共享单车对象,调用HiINC类中的静态方法calculateRent得到并输出租金,保留小数点两位。
函数接口定义:
double getRent(); static double calculateRent(HiINC[] hi, int days);
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); HiINC[] hi = new HiINC[n]; for (int i = 0; i < n; i++) { String type = sc.next(); if (type.equals("bicycle")) { hi[i] = new Bicycle(sc.next()); } else { hi[i] = new Car(sc.next(), sc.nextDouble()); } } double rent = HiINC.calculateRent(hi, sc.nextInt()); System.out.printf("%.2f", rent); } } /* 请在这里填写答案 */
输入样例:
5
car C001 253000
car C002 163000
bicycle B001
bicycle B002
car C003 161000
20
样例说明:
5 //表示需要计算5辆车的租金
car C001 253000 //表示车辆类型,车牌,售价
car C002 163000
bicycle B001
bicycle B002
car C003 161000
20 //表示租赁天数
输出样例:
在这里给出相应的输出。例如:
12020.00
答案:
class HiINC {
public static double calculateRent(HiINC[] hi, int days) {
double totalRent = 0;
for (HiINC vehicle : hi) {
totalRent += vehicle.getRent() * days;
}
return totalRent;
}
// This method needs to be implemented by subclasses
public double getRent() {
return 0; // Default implementation, to be overridden by subclasses
}
}
class Car extends HiINC {
private String plateNumber;
private double sellingPrice;
public Car(String plateNumber, double sellingPrice) {
this.plateNumber = plateNumber;
this.sellingPrice = sellingPrice;
}
@Override
public double getRent() {
return sellingPrice * 0.001; // 0.1% of selling price per day
}
}
class Bicycle extends HiINC {
private String plateNumber;
public Bicycle(String plateNumber) {
this.plateNumber = plateNumber;
}
@Override
public double getRent() {
return 12.0; // $12 per day for bicycles
}
}