答答租车系统(面向对象综合练习)
Problem Description
各位面向对象的小伙伴们,在学习了面向对象的核心概念——类的封装、继承、多态之后,答答租车系统开始营运了。
请你充分利用面向对象思想,为公司解决智能租车问题,根据客户选定的车型和租车天数,来计算租车费用,最大载客人数,最大载载重量。
公司现有三种车型(客车、皮卡车、货车),每种车都有名称和租金的属性;其中:客车只能载人,货车只能载货,皮卡车是客货两用车,即可以载人,也可以载货。
下面是答答租车公司的可用车型、容量及价目表:
序号 名称 载客量 载货量 租金
(人) (吨) (元/天)
1 A 5 800
2 B 5 400
3 C 5 800
4 D 51 1300
5 E 55 1500
6 F 5 0.45 500
7 G 5 2.0 450
8 H 3 200
9 I 25 1500
10 J 35 2000
要求:根据客户输入的所租车型的序号及天数,计算所能乘载的总人数、货物总数量及租车费用总金额。
Input
第二行是一个整数,代表要租车的数量N;
接下来是N行数据,每行2个整数m和n,其中:m表示要租车的编号,n表示租用该车型的天数。
Output
载客总人数 载货总重量(保留2位小数) 租车金额(整数)
若不租车,则输出:
0 0.00 0(含义同上)
Sample Input
1 2 1 1 2 2
Sample Output
15 0.00 1600
Hint
Source
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t;
int i, d, n;
while(input.hasNext()) {
t = input.nextInt();
if(t == 0) {
System.out.println("0 0.00 0");
break;
}
else if(t == 1){
customus cu= new customus();
customus sum = new customus();
n = input.nextInt();
while(n != 0) {
n--;
i = input.nextInt();
d = input.nextInt();
Dada da = new Dada();
cu = da.th(i, d);
sum = customus.cal(cu, sum);
}
customus.show(sum);
}
}
}
}
class Car {
int id;
int people;
double thing;
int rent;
Car(int id, int people, double thing, int rent){
this.id = id;
this.people = people;
this.thing = thing;
this.rent = rent;
}
}
class customus {
double things;
int people;
int rents;
customus(){
things = 0;
people = 0;
rents = 0;
}
static customus cal(customus cu, customus sum) {
sum.things += cu.things;
sum.people += cu.people;
sum.rents += cu.rents;
return sum;
}
static void show(customus sum) {
System.out.printf("%d %.2f %d", sum.people, sum.things, sum.rents);
}
}
class Dada {
Car A = new Car(1, 5, 0, 800);
Car B = new Car(2, 5, 0, 400);
Car C = new Car(3, 5, 0, 800);
Car D = new Car(4, 51, 0, 1300);
Car E = new Car(5, 55, 0, 1500);
Car F = new Car(6, 5, 0.45, 500);
Car G = new Car(7, 5, 2.0, 450);
Car H = new Car(8, 0, 3, 200);
Car I = new Car(9, 0, 25, 1500);
Car J = new Car(10, 0, 35, 2000);
customus th(int x, int y){
customus su = new customus();
switch(x) {
case 1: su.things = A.thing * y; su.people = A.people * y; su.rents = A.rent*y;return su;
case 2: su.things = B.thing * y; su.people = B.people * y; su.rents = B.rent*y;return su;
case 3: su.things = C.thing * y; su.people = C.people * y; su.rents = C.rent*y;return su;
case 4: su.things = D.thing * y; su.people = D.people * y; su.rents = D.rent*y;return su;
case 5: su.things = E.thing * y; su.people = E.people * y; su.rents = E.rent*y;return su;
case 6: su.things = F.thing * y; su.people = F.people * y; su.rents = F.rent*y;return su;
case 7: su.things = G.thing * y; su.people = G.people * y; su.rents = G.rent*y;return su;
case 8: su.things = H.thing * y; su.people = H.people * y; su.rents = H.rent*y;return su;
case 9: su.things = I.thing * y; su.people = I.people * y; su.rents = I.rent*y;return su;
case 10: su.things = J.thing * y; su.people = J.people * y; su.rents =J.rent*y;return su;
}
return su;
}
}