先创建一个抽象类,里面定义名称、载客量、载货量、价格的抽象方法,然后给每辆车定义一个子类,返回值为他们各自的属性,最后在main方法中把所有属性加起来
首先是父类
package dada;
public abstract class Total {
public abstract int zaiKe();
public abstract int zaiHuo();
public abstract int zuJin();
public abstract String leiXing1();
public abstract String leiXing2();
}
这些都是拼音,一下就看懂了,俩个类型是要把载客和载货分开,1是载客,2是载货
接下来是留个子类,就是把自己的属性返回,没有的属性就返回0
ackage dada;
public class Aodi extends Total {
@Override
public int zaiKe() {
// TODO Auto-generated method stub
return 4;
}
@Override
public int zaiHuo() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int zuJin() {
// TODO Auto-generated method stub
return 500;
}
@Override
public String leiXing1() {
// TODO Auto-generated method stub
return "奥迪A4 ";
}
@Override
public String leiXing2() {
// TODO Auto-generated method stub
return null;
}
package dada;
public class Jianglong extends Total {
@Override
public int zaiKe() {
// TODO Auto-generated method stub
return 20;
}
@Override
public int zaiHuo() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int zuJin() {
// TODO Auto-generated method stub
return 800;
}
@Override
public String leiXing1() {
// TODO Auto-generated method stub
return "江龙 ";
}
@Override
public String leiXing2() {
// TODO Auto-generated method stub
return null;
}
}
package dada;
public class Mazida extends Total {
@Override
public int zaiKe() {
// TODO Auto-generated method stub
return 4;
}
@Override
public int zaiHuo() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int zuJin() {
// TODO Auto-generated method stub
return 400;
}
@Override
public String leiXing1() {
// TODO Auto-generated method stub
return "马自达6 ";
}
@Override
public String leiXing2() {
// TODO Auto-generated method stub
return null;
}
}
package dada;
public class Pokaxue extends Total {
@Override
public int zaiKe() {
// TODO Auto-generated method stub
return 4;
}
@Override
public int zaiHuo() {
// TODO Auto-generated method stub
return 2;
}
@Override
public int zuJin() {
// TODO Auto-generated method stub
return 450;
}
@Override
public String leiXing1() {
// TODO Auto-generated method stub
return "皮卡雪6 ";
}
@Override
public String leiXing2() {
// TODO Auto-generated method stub
return "皮卡雪6 ";
}
}
package dada;
public class Songhuajiang extends Total {
@Override
public int zaiKe() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int zaiHuo() {
// TODO Auto-generated method stub
return 4;
}
@Override
public int zuJin() {
// TODO Auto-generated method stub
return 400;
}
@Override
public String leiXing1() {
// TODO Auto-generated method stub
return null;
}
@Override
public String leiXing2() {
// TODO Auto-generated method stub
return "松花江 ";
}
}
package dada;
public class Yiweihe extends Total {
@Override
public int zaiKe() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int zaiHuo() {
// TODO Auto-generated method stub
return 20;
}
@Override
public int zuJin() {
// TODO Auto-generated method stub
return 1000;
}
@Override
public String leiXing1() {
// TODO Auto-generated method stub
return null;
}
@Override
public String leiXing2() {
// TODO Auto-generated method stub
return "依维柯 ";
}
}
最后是实现代码
package dada;
import java.util.Scanner;
public class Rental {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用答答租车系统:\n您是否要租车:1是 0否");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
if(a==1){//租车系统开始
System.out.println("您可租车的类型及其价目表");
System.out.println("序号 汽车名称 租金 容量");
System.out.println("1. 奥迪A4 500元/天 载人:4人" );
System.out.println("2. 马自达6 400元/天 载人:4人" );
System.out.println("3. 皮卡雪6 450元/天 载人:4人 载货:2吨" );
System.out.println("4. 金龙 800元/天 载人:20人" );
System.out.println("5. 松花江 400元/天 载人:4吨" );
System.out.println("6. 依维柯 1000元/天 载人:20吨" );
System.out.println("请输入您要租汽车的数量");
int b=input.nextInt();
int i=1;
int price=0;
int person=0;
int cargo=0;
String name1=" ";
String name2=" ";
for(;b>0;b--){
System.out.println("请输入第"+i+"辆车的序号:");
int choose=input.nextInt();
switch(choose){
case 1:{Total aodi=new Aodi();
price=price+aodi.zuJin();
person=person+aodi.zaiKe();
cargo=cargo+aodi.zaiHuo();
name1=name1+aodi.leiXing1();
};break;
case 2:{Total mazida=new Mazida();
price=price+mazida.zuJin();
person=person+mazida.zaiKe();
cargo=cargo+mazida.zaiHuo();
name1=name1+mazida.leiXing1();
};break;
case 3: {Total pikaxue=new Pokaxue();
price=price+pikaxue.zuJin();
person=person+pikaxue.zaiKe();
cargo=cargo+pikaxue.zaiHuo();
name1=name1+pikaxue.leiXing1();
name2=name2+pikaxue.leiXing2();
} ;break;
case 4:{Total jinlong=new Jianglong();
price=price+jinlong.zuJin();
person=person+jinlong.zaiKe();
cargo=cargo+jinlong.zaiHuo();
name1=name1+jinlong.leiXing1();
} ;break;
case 5:{Total songhuajiang=new Songhuajiang();
price=price+songhuajiang.zuJin();
person=person+songhuajiang.zaiKe();
cargo=cargo+songhuajiang.zaiHuo();
name2=name2+songhuajiang.leiXing1();
} ;break;
case 6:{Total yiweike=new Yiweihe();
price=price+yiweike.zuJin();
person=person+yiweike.zaiKe();
cargo=cargo+yiweike.zaiHuo();
name2=name2+yiweike.leiXing1();
} ;break;
}
i++;
}
System.out.println("输入租车天数:");
int c=input.nextInt();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
System.out.println(name1+"共载人:"+person+"人");
System.out.println("***载货的车有:");
System.out.println(name2+"共载货:"+cargo+"吨");
System.out.println("租车总价格:"+price*c);
}else{
System.out.println("欢迎您的下次使用,再见");
}
}
}
看了大神的之后,觉得好多地方都要改,这里先留一个纪念。如果有想评价一下的可以加qq1955873580
热门评论
硬编码啊- -- - -