首先先创建一个父类DadaCar package com.imooc;public class DadaCar {public int id;//序号public String name;//汽车名字public int rentMoney;//租金public int passgerCapacity;//载人量public int cargoCapacity;//载货量public int getId() { return id;}public void setId(int id) { this.id = id;}public String getName() { return name;}public void setName(String name) { this.name = name;}public int getRentMoney() { return rentMoney;}public void setRentMoney(int rentMoney) { this.rentMoney = rentMoney;}public int getPassgerCapacity() { return passgerCapacity;}public void setPassgerCapacity(int passgerCapacity) { this.passgerCapacity = passgerCapacity;}public int getCargoCapacity() { return cargoCapacity;}public void setCargoCapacity(int cargoCapacity) { this.cargoCapacity = cargoCapacity;}}
接着创建第一个子类MannedCars继承DadaCar,这个类代表载人汽车
package com.imooc;public class MannedCars extends DadaCar{ //初始化类,防止子类重新定义构造方法时因找不到父类相应的构造方法而出现错误。 public MannedCars(){} public MannedCars(int id ,String name,int rentMoney,int passgerCapacity) { this.id=id; this.name=name; this.rentMoney=rentMoney; this.passgerCapacity=passgerCapacity; } public String toString()
{
return this.id+".车名:"+this.name+"\t"+"租金为:"+this.rentMoney+"元/天"+ "\t"+" 载客量为:"+this.passgerCapacity+"人";
}
}
第二个子类TruckCars继承DadaCar,这个类代表载货汽车
package com.imooc;
public class TruckCars extends DadaCar {
//初始化类,防止子类重新定义构造方法时因找不到父类相应的构造方法而出现错误。
public TruckCars(int id ,String name,int rentMoney,int cargoCapacity)
{
this.id=id;
this.name=name;
this.rentMoney=rentMoney;
this.cargoCapacity=cargoCapacity;
}
public String toString()
{
return this.id+".车名:"+this.name+"\t"+"租金为:"+this.rentMoney+"元/天"+ "\t"+" 载货量为:"+this.cargoCapacity+"吨";
}
}
第三个子类PickupCars继承父类DadaCar,代表皮卡车(技能既能载货也能载人)
package com.imooc;
public class PickupCars extends MannedCars {
//初始化类,防止子类重新定义构造方法时因找不到父类相应的构造方法而出现错误。
public PickupCars(int id ,String name,int rentMoney,int passgerCapacity,int cargoCapacity)
{
this.id=id;
this.name=name;
this.rentMoney=rentMoney;
this.passgerCapacity=passgerCapacity;
this.cargoCapacity=cargoCapacity;
}
public String toString()
{
return this.id+".车名:"+this.name+"\t"+"租金为:"+this.rentMoney+"元/天"+ "\t"+" 载客量为:"+this.passgerCapacity+"人"+"\t"+"载货量为:"+this.cargoCapacity+"吨";
}
}
接着写测试类
package com.imooc;
import java.util.Scanner;
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("欢迎使用达达租车系统:\n您是否要租车: 1 是 2 否");
int choice=input.nextInt();//此处choice表示是否进入达达租车系统
if(choice==1)
{
//通过构造函数输入各车型数据 Cars =new Cars(1,"奥迪A4",500,4);//通过构造函数进行赋值操作。不要使用 Cars =new Cars(); audi.Cars(1,"奥迪A4",500,4,0);
//的方式进行赋值,因为那样调用的是无参构造方法。
MannedCars aodi=new MannedCars(1,"奥迪A4L",500,5);
MannedCars mazda=new MannedCars(2,"马自达阿特兹",300,5);
TruckCars huoche1=new TruckCars(3,"欧马可",200,100);
TruckCars huoche2=new TruckCars(4,"五菱",250,120);
PickupCars pika1=new PickupCars(5,"日产皮卡",350,4,60);
PickupCars pika2=new PickupCars(6,"美国皮卡",400,5,80);
//输出汽车信息表
System.out.println(aodi);
System.out.println(mazda);
System.out.println(huoche1);
System.out.println(huoche2);
System.out.println(pika1);
System.out.println(pika2);
System.out.println("请输入您要租车的数量");
int rentNum=input.nextInt();
String[]storeRentCar={"","","","","",""};
int[]storeRentNum={0,0,0,0,0,0};
int[]storeRentDay={0,0,0,0,0,0};
for(int i=0;i<rentNum;i++)
{
System.out.println("请输入第"+(i+1)+"辆车的序号");
storeRentNum[i]=input.nextInt();
System.out.println("请输入想租车的天数");
storeRentDay[i]=input.nextInt();
}
//System.out.println("请输入租车天数");
//int rentDay=input.nextInt();
String displayCars="";//列出被选中的车辆的信息
String displayPassger="";//输入载客车辆
String displayCargo="";//输入载货车辆
int sumRentDay=0;//租车的天数
int sumPassger=0;
int sumCargo=0;
int sumRent=0;
for(int i=0;i<rentNum;i++)
{
switch(storeRentNum[i])
{
case 1:storeRentCar[i]=aodi.getName();
displayPassger+=aodi.getName()+"\t";
sumRentDay=storeRentDay[i];
sumPassger+=aodi.getPassgerCapacity();
sumRent+=(storeRentDay[i]*aodi.getRentMoney());
break;
case 2:
storeRentCar[i]=mazda.getName();
displayPassger+=mazda.getName()+"\t";
sumRentDay=storeRentDay[i];
sumPassger+=mazda.getPassgerCapacity();
sumRent+=(storeRentDay[i]*mazda.getRentMoney());
break;
case 3:
storeRentCar[i]=huoche1.getName();
displayCargo+=huoche1.getName()+"\t";
sumRentDay=storeRentDay[i];
sumCargo+=huoche1.getCargoCapacity();
sumRent+=(storeRentDay[i]*huoche1.getRentMoney());
break;
case 4:
storeRentCar[i]=huoche2.getName();
displayCargo+=huoche2.getName()+"\t";
sumRentDay=storeRentDay[i];
sumCargo+=huoche2.getCargoCapacity();
sumRent+=(storeRentDay[i]*huoche1.getRentMoney());
break;
case 5 :
storeRentCar[i]=pika1.getName();
displayPassger+=pika1.getName()+"\t";
sumRentDay=storeRentDay[i];
displayCargo+=pika1.getName()+"\t";
displayCargo+=pika1.getCargoCapacity();
sumPassger+=pika1.getPassgerCapacity();
sumCargo+=pika1.getCargoCapacity();
sumRent+=(storeRentDay[i]*pika1.getRentMoney());
break;
case 6:
storeRentCar[i]=pika2.getName();
displayPassger+=pika2.getName()+"\t";
displayPassger+=pika2.getName()+"\t";
sumRentDay=storeRentDay[i];
displayCargo+=pika2.getCargoCapacity();
sumPassger+=pika2.getPassgerCapacity();
sumCargo+=pika2.getCargoCapacity();
sumRent+=(storeRentDay[i]*pika2.getRentMoney());
break;
default:System.out.println("本公司暂不支持该车型,请重新输入");
break;
}
}
System.out.println("您的账单:");
for(int i=0;i<rentNum;i++)
{
System.out.println("您租的车有:"+storeRentCar[i]+"\t"+"分别租了"+"\t"+storeRentDay[i]+"天");
}
System.out.println("可载人的车有");
System.out.println(displayPassger+"共载人"+sumPassger+"人");
System.out.println("可载货的车有");
System.out.println(displayCargo+"共载货"+sumCargo+"吨");
System.out.println("租车总价格为:"+sumRent+"元");
}
else
{
System.out.println("欢迎下次使用");
}
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章