1.父类 Car.java
package com.car;
public abstract class Car {
String name;
double price;
int member;
int rl;
}
2.子类1 Car1.java
package com.car;
public class Car1 extends Car {
//只载人的车型
public Car1(String name,double price,int member) {
this.name=name;
this.price=price;
this.member=member;
}
public String toString() {
return this.name+"\t"+this.price+"元/天\t"+"载人:"+this.member+"人";
}
}
3.子类2 Car2.java
package com.car;
public class Car2 extends Car {
//可载货+载人的车型
public Car2(String name,double price,int member,int rl) {
this.name=name;
this.price=price;
this.member=member;
this.rl=rl;
}
public String toString() {
return this.name+"\t"+this.price+"元/天\t"+"载人:"+this.member+"人"+" 载货:"+this.rl+"吨";
}
}
4.子类3 Car3.java
package com.car;
public class Car3 extends Car {
//只载货的车型
public Car3(String name,double price,int rl) {
this.name=name;
this.price=price;
this.rl=rl;
}
public String toString() {
return this.name+"\t"+this.price+"元/天\t"+"载货:"+this.rl+"吨";
}
}
5.主函数调试代码Main.java
package com.car;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Car[] cars= {new Car1("奥迪A4",500,4),new Car2("雷诺",500,4,3),new Car3("AE86",600,1)};
double totalprice=0;
int totalnumber=0;
int totalrl=0;
String zairen = "" , zaihuo = "";
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是 0否");
Scanner input = new Scanner(System.in);
int yes=input.nextInt();
if(yes==1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t\t容量");
for(int i=0;i<cars.length;i++) {
System.out.println((i+1)+".\t"+cars[i]);
}
System.out.println("请输入您要租汽车的数量");
int num=input.nextInt();
for(int j=0;j<num;j++) {
System.out.println("请输入第"+(j+1)+"车的序号:");
int c=input.nextInt();
totalprice+=cars[c-1].price;
totalnumber+=cars[c-1].member;
totalrl+=cars[c-1].rl;
if(cars[c-1].member!=0) {
zairen+=cars[c-1].name+" ";
}
if(cars[c-1].rl!=0) {
zaihuo+=cars[c-1].name+" ";
}
}
System.out.println("请输入租车天数");
int days=input.nextInt();
totalprice*=days;
System.out.println("您的账单:");
System.out.println("***可载人的车有");
System.out.println(zairen+" 共载人:"+totalnumber+"人");
System.out.println("***载货的车有");
System.out.println(zaihuo+" 共载货:"+totalrl+"吨");
System.out.println("***租车总价格:"+totalprice);
}else {
System.out.println("感谢您的访问");
}
}
}
6.运行效果如下图
点击查看更多内容
27人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦