7-4 sdut-oop-7 答答租车系统(类的继承与多态 面向对象综合练习) (10 分)
题目:
点击查看问题
各位面向对象的小伙伴们,在学习了面向对象的核心概念——类的封装、继承、多态之后,答答租车系统开始营运了。
请你充分利用面向对象思想,为公司解决智能租车问题,根据客户选定的车型和租车天数,来计算租车费用,最大载客人数,最大载载重量。
公司现有三种车型(客车、皮卡车、货车),每种车都有名称和租金的属性;其中:客车只能载人,货车只能载货,皮卡车是客货两用车,即可以载人,也可以载货。
下面是答答租车公司的可用车型、容量及价目表:
要求:根据客户输入的所租车型的序号及天数,计算所能乘载的总人数、货物总数量及租车费用总金额。
输入格式:
首行是一个整数:代表要不要租车 1——要租车(程序继续),0——不租车(程序结束);
第二行是一个整数,代表要租车的数量N;
接下来是N行数据,每行2个整数m和n,其中:m表示要租车的编号,n表示租用该车型的天数。
输出格式:
若成功租车,则输出一行数据,数据间有一个空格,含义为: 载客总人数 载货总重量(保留2位小数) 租车金额(整数) 若不租车,则输出: 0 0.00 0(含义同上)
输入样例:
1
2
1 1
2 2
输出样例:
在这里给出相应的输出。例如:
15 0.00 1600
思路:
弄个Car的接口,实现三个类
题解:
import java.util.Scanner; import java.util.*; interface Car{ public static final double number = 0; public static final double pe = 0; public static final int name = 0; public static final int ma = 0; double getNum(); int getM(); int getPe(); } class C1 implements Car{ int name; double number=0; int ma; int pe; public C1(int name,int pe,int day){ this.ma=day; this.pe=pe; this.name=name; } public double getNum() { return 0; } public int getM() { return ma; } public int getPe() { return pe; } } class C2 implements Car{ int name; int pe; double number; int ma; public C2(int name,int pe,double number,int day){ this.ma=day; this.number=number; this.pe=pe; this.name=name; } public double getNum() { return number; } public int getM() { return ma; } public int getPe() { return pe; } } class C3 implements Car{ int name; double number; int ma; int pe=0; public C3(int name,double number,int day){ this.ma=day; this.number=number; this.name=name; } public double getNum() { return number; } public int getM() { return ma; } public int getPe() { return 0; } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Car[] cars = new Car[10]; cars[0]=new C1(1,5,800); cars[1]=new C1(2,5,400); cars[2]=new C1(3,5,800); cars[3]=new C1(4,51,1300); cars[4]=new C1(5,55,1500); cars[5]=new C2(6,5,0.45,500); cars[6]=new C2(7,5,2.0,450); cars[7]=new C3(8,3.0,200); cars[8]=new C3(9,25.0,1500); cars[9]=new C3(10,35.0,2000); int k=sc.nextInt(); if(k==0) { System.out.println("0 0.00 0"); return; } int n=sc.nextInt(); int ans1=0; double ans2=0; int ans3=0; for(int i=0;i<n;i++) { int a=sc.nextInt(); a--; int b=sc.nextInt(); ans1+=cars[a].getPe()*b; ans2+=cars[a].getNum()*b; ans3+=cars[a].getM()*b; } System.out.printf("%d %.2f %d",ans1,ans2,ans3); sc.close(); } }
本文作者:kingwzun
本文链接:https://www.cnblogs.com/kingwz/p/15549707.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
JAVA习题
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步