一个主类,3个子类分别继承主类,测试类;
one:AllCar类
package zCar;
public class AllCar {//车类
public String name;//车名
public int manned;//载人
public int goods;//载货
public int prices;//租金
}
two:MannedCar类
package zCar;
public class MannedCar extends AllCar {//只载人的车
public MannedCar(String name,int manned,int prices){
this.name=name;
this.manned=manned;
this.prices=prices;
}
}
three:GoodsCar类
package zCar;
public class GoodsCar extends AllCar {//只载货的车
public GoodsCar(String name,int goods,int prices){
this.name=name;
this.goods=goods;
this.prices=prices;
}
}
four:DoubleUseCar 类
package zCar;
public class DoubleUseCar extends AllCar {//载人和载货都可以的车
public DoubleUseCar(String name,int manned,int goods,int prices){
this.name=name;
this.manned=manned;
this.goods=goods;
this.prices=prices;
}
}
five:DadaRentCarSystem类
package zCar;
import java.util.;
public class DadaRentCarSystem {
public static int bool=1;//静态全局变量,用来接收输入的数
public static void main(String[] args) {
MannedCar car1=new MannedCar("奥迪A4 ",4,500);
MannedCar car2=new MannedCar("马自达6 ",4,400);
DoubleUseCar car3=new DoubleUseCar("皮卡雪6 ",4,2,450);
MannedCar car4=new MannedCar("金龙 ",20,800);
GoodsCar car5=new GoodsCar("松花江 ",4,400);
GoodsCar car6=new GoodsCar("依维柯 ",20,1000);
System.out.println("欢迎您使用答答租车系统!");
System.out.println("你是否要租车:1是,0否");
Scanner in=new Scanner(System.in);
while(true){//检验输入的合法性
try{
int a=in.nextInt();//临时输入的数
bool=a;
break;
}
catch(InputMismatchException e){
System.out.println("输入有误:请输入1或0!!");
in.nextLine();
}
}
if(bool==1){
System.out.println("序号\t汽车名称\t租金\t容量");
System.out.println("1\t"+car1.name+"\t"+car1.prices+"元/天 "+car1.manned+"人");
System.out.println("2\t"+car2.name+"\t"+car2.prices+"元/天 "+car2.manned+"人");
System.out.println("3\t"+car3.name+"\t"+car3.prices+"元/天 "+car3.manned+"人"+","+car3.goods+"吨");
System.out.println("4\t"+car4.name+"\t"+car4.prices+"元/天 "+car4.manned+"人");
System.out.println("5\t"+car5.name+"\t"+car5.prices+"元/天 "+car5.goods+"吨");
System.out.println("6\t"+car6.name+"\t"+car6.prices+"元/天 "+car6.goods+"吨");
System.out.println("请输入要租车的辆数:");
int numCar=in.nextInt();//租车的辆数
int manned=0,goods=0,prices=0;
String name1="";//存储载人车名
String name2="";//存储载货车名
for(int i=1;i<=numCar;i++){
System.out.println("请输入第"+i+"辆车的序号");
int num = in.nextInt();//租车的序号
switch(num){
case 1:
name1+=car1.name;
prices += car1.prices;
manned += car1.manned;
break;
case 2:
name1+=car2.name;
prices+=car2.prices;
manned+=car2.manned;
break;
case 3:
name1+=car3.name;
name2+=car3.name;
prices+=car3.prices;
manned+=car3.manned;
goods+=car3.goods;
break;
case 4:
name1+=car4.name;
prices+=car4.prices;
goods+=car4.goods;
break;
case 5:
name2+=car5.name;
prices+=car5.prices;
goods+=car5.goods;
break;
case 6:
name2+=car6.name;
prices+=car6.prices;
goods+=car6.goods;
break;
}
}
System.out.println("请输入租车天数");
int day = in.nextInt();//租车天数
System.out.println("下面是您的账单:");
System.out.println("您租的载人车有:");
System.out.println(name1+"总载人数:"+manned+"人");
System.out.println("您租的载货车有:");
System.out.println(name2+"总载货量:"+goods+"吨");
System.out.println("您租了:"+day+"天");
System.out.println("租车总价格:"+dayprices+"元");
}
else if(bool==0){
System.out.println("欢迎下次光临!!");
}else{
System.out.println("您输入有误,请再次尝试");
}
}
}
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123