答答租车系统(面向对象综合练习)
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(含义同上)
Example Input
1 2 1 1 2 2
Example Output
15 0.00 1600
import java.util.Scanner; abstract class car //抽象类 { String name; int rentPrice; abstract int getPerson(); //抽象成员函数在继承的子类中必须被重写 abstract double getPickup(); abstract int getPrice(); } class KCar extends car { int person; KCar(String name,int person,int rentPrice) { this.name=name; this.person=person; this.rentPrice=rentPrice; } String getName() { return this.name; } int getPerson() { return this.person; } int getPrice() { return this.rentPrice; } @Override double getPickup() { // TODO Auto-generated method stub return 0; } } class PCar extends car { int person; double pickup; PCar(String name,int person,double pickup,int rentPrice) { this.name=name; this.person=person; this.pickup=pickup; this.rentPrice=rentPrice; } String getName() { return this.name; } int getPerson() { return this.person; } double getPickup() { return this.pickup; } int getPrice() { return this.rentPrice; } } class HCar extends car { double pickup; HCar(String name,double pickup,int rentPrice) { this.name=name; this.pickup=pickup; this.rentPrice=rentPrice; } String getName() { return this.name; } double getPickup() { return this.pickup; } int getPrice() { return this.rentPrice; } @Override int getPerson() { // TODO Auto-generated method stub return 0; } } public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); car[]rentCar={ //对象的上转型对象的定义 new KCar("A",5,800), new KCar("B",5,400), new KCar("C",5,800), new KCar("D",51,1300), new KCar("E",55,1500), new PCar("F",5,0.45,500), new PCar("G",5,2.0,450), new HCar("H",3,200), new HCar("I",25,1500), new HCar("J",35,2000) }; int f=in.nextInt(); int sumPerson=0; double sumPickup=0; int sumPrice=0; int n; int N,m; if(f==1) { N=in.nextInt(); while(N>0) { N--; m=in.nextInt(); n=in.nextInt(); sumPerson+=rentCar[m-1].getPerson()*n; sumPickup+=rentCar[m-1].getPickup()*n; sumPrice+=rentCar[m-1].getPrice()*n; } } System.out.printf("%d %.2f %d",sumPerson,sumPickup,sumPrice); } }