共享车辆计费程序
分数 20
全屏浏览
切换布局
作者 Ma
单位 山东科技大学
你好车辆共享公司现有两种共享车辆,即共享汽车和共享单车。共享汽车包含车牌号(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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64MB
abstract class HiINC {
abstract double getRent();
static double calculateRent(HiINC[] hi, int days) {
double sum = 0;
for (HiINC h : hi) {
sum += h.getRent();
}
return sum * days;
}
}
class Car extends HiINC {
String number;
double money;
public Car(String number, double money) {
this.number = number;
this.money = money;
}
@Override
double getRent() {
return money * 0.001;
}
}
class Bicycle extends HiINC {
String number;
public Bicycle(String number) {
this.number = number;
}
@Override
double getRent() {
return 12;
}
}