获取租用车名称及总容量那块写的有点问题,看了下大家都用的数组.先放上来,稍后再完善
汽车抽象类
public abstract class Car {
public int carNumber;
public String carName;
public int carMoney;
public int carCapacity1man;
public int carCapacity2goods;
public abstract void getCar();
}
载人子类
public class MannedCar extends Car {
public void getCar() {
System.out.println(carNumber+carName+carMoney+"元/天"+"载人:"+carCapacity1man+"人");
}
}
货车子类
public class Van extends Car {
public void getCar() {
System.out.println(carNumber+carName+carMoney+"元/天"+"载货:"+carCapacity2goods+"吨");
}
}
皮卡子类
public class PickupTruck extends Car{
public void getCar() {
System.out.println(carNumber+carName+carMoney+"元/天"+"载人:"+carCapacity1man+"人"+"载货:"+carCapacity2goods+"吨");
}
}
主函数
public class main {
public static void main(String[] args) {
MannedCar car1 = new MannedCar();
car1.carNumber = 1;
car1.carName = "奥迪A6";
car1.carMoney = 500;
car1.carCapacity1man = 4;
MannedCar car2 = new MannedCar();
car2.carNumber = 2;
car2.carName = "马自达6";
car2.carMoney = 400;
car2.carCapacity1man = 4;
PickupTruck car3 = new PickupTruck();
car3.carNumber = 3;
car3.carName = "皮卡雪6";
car3.carMoney = 450;
car3.carCapacity1man = 4;
car3.carCapacity2goods = 2;
MannedCar car4 = new MannedCar();
car4.carNumber = 4;
car4.carName = "金龙";
car4.carMoney = 800;
car4.carCapacity1man = 20;
Van car5 = new Van();
car5.carNumber = 5;
car5.carName = "松花江";
car5.carMoney = 400;
car5.carCapacity2goods = 4;
Van car6 = new Van();
car6.carNumber = 6;
car6.carName = "依维柯";
car6.carMoney = 1000;
car6.carCapacity2goods = 20;
Scanner sc = new Scanner(System.in);
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
int yes = sc.nextInt();
//展示车辆信息
if (yes == 1) {
System.out.println("您可租车的类型及价格:");
System.out.println("序号 汽车名称 租金 容量");
car1.getCar();
car2.getCar();
car3.getCar();
car4.getCar();
car5.getCar();
car6.getCar();
} else {
System.out.println("欢迎再次使用答答租车系统!");
}
//租车数量
System.out.println("请输入您要租汽车的数量:");
int sum = sc.nextInt();
int[] num = new int[sum];
int summon = 0;
//租车车型
for (int i = 0; i < sum; i++) {
System.out.println("请输入第" + (i + 1) + "辆车的序号:");
num[i] = sc.nextInt();
}
//租车时间
System.out.println("请输入租车天数:");
int days = sc.nextInt();
//计算租车费用
String[] carname = new String[num.length];
for (int i = 0; i < num.length; i++) {
switch (num[i]) {
case 1:
summon = summon + car1.carMoney;
carname[i]=car1.carName;
break;
case 2:
summon = summon + car2.carMoney;
carname[i]=car2.carName;
break;
case 3:
summon = summon + car3.carMoney;
carname[i]=car3.carName;
break;
case 4:
summon = summon + car4.carMoney;
carname[i]=car4.carName;
break;
case 5:
summon = summon + car2.carMoney;
carname[i]=car5.carName;
break;
case 6:
summon = summon + car6.carMoney;
carname[i]=car6.carName;
break;
}
}
summon = summon * days;
//输出账单详情
System.out.println("您的账单:");
System.out.println("***可供载人的车有:");
for(int i=0;i<carname.length;i++)
{
System.out.println(carname[i]);
}
// System.out.println("可载货的车有:");
// for(int i=0;i<carname.length;i++)
// {
// System.out.println(carname[i]);
// }
System.out.println("租车总价格:" + summon +"元");
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章