汽车类
public class Car {
int id;
String name;
double money;
int load;
int set;
public Car(int id,String name,double money,int load,int set) {
this.id = id;
this.name = name;
this.money = money;
this.load = load;
this.set = set;
}
}
新类继承汽车类
public class TwoCar extends Car{
public TwoCar(int id, String name, double money, int load, int set) {
super(id, name, money, load, set);
}
public String toString() {
return id+"\t" +name+"\t" + money+"\t" +load+"\t" + set;
}
}
测试类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int sumSet=0;
int sumLoad = 0;
int sumMoney = 0;
int pay=0;
String canSet = "";
String canLoad = "";
Car [] cars = new Car[5];
cars[0] = new TwoCar(1, "奇瑞", 200, 0, 4);
cars[1] = new TwoCar(2, "大奔", 500, 0, 4);
cars[2] = new TwoCar(3, "皮卡", 300, 2, 4);
cars[3] = new TwoCar(4, "江淮", 300, 5, 0);
cars[4] = new TwoCar(5, "解放", 500, 10, 0);
System.out.println("欢迎使用租车系统"+"\n"+"输入任意数字继续,输入0退出。请输入");
Scanner in = new Scanner(System.in);
if(in.nextInt()==0) {
System.out.println("感谢您的使用");
}else {
System.out.println("目前可以选择的车型有:");
System.out.println("序号"+"\t"+"车型"+"\t"+"金额/天"+"\t"+"载重"+"\t"+"座位");
for(Car c:cars) {
System.out.println(c);
}
System.out.println("请选择您需要租借车辆的数量(请输入0~5之间的整数)");
int sum = in.nextInt();
for(int i=0;i<sum;i++) {
System.out.println("请输入第"+(i+1)+"辆车的序号");
int id = in.nextInt();
System.out.println("请输入第"+(i+1)+"辆车的租用时间");
int day = in.nextInt();
sumMoney = (int) (day*cars[id-1].money);
sumSet += cars[id-1].set;
sumLoad += cars[id-1].load;
pay += sumMoney;
if(cars[id-1].set!=0) {
canSet+=cars[id-1].name+"";
}
if(cars[id-1].load!=0) {
canLoad+=cars[id-1].name+"";
}
}
System.out.println("可载客车型有:"+canSet);
System.out.println("您选择的车型载客量为:"+sumSet);
System.out.println("可载货车型有:"+canLoad);
System.out.println("您选择的车型载货量为:"+sumLoad);
System.out.println("您共需要支付的费用为"+pay+"元");
}
}
}
点击查看更多内容
6人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦