整体思路
- 创建名为Car的抽象类,声明共有属性(名字,载客量busLoad,载货量trunkLoad,租金);
- 分别创建卡车类Trunk,小轿车类Bus,皮卡类PickUp,均继承于Car类;
- 在主方法中分别创建各种车的对象,并用一个数组存放这些对象,最后对用户输入的序号对应的租车价格计算;
/**
* 抽象类
* Created by haivo on 2016/3/24.
*/
public abstract class Car {
public String name = "";
public int trunkLoad = 0;
public int busLoad = 0;
public int price = 0;
}
/**
* 卡车(东风,解放)
* Created by haivo on 2016/3/24.
*/
public class Trunk extends Car{
public Trunk(String name,int trunkLoad,int price){
this.name = name;
this.trunkLoad = trunkLoad;
this.price = price;
}
@Override
public String toString() {
return String.format("%s\t\t%s\t\t%d%s\t\t%d%s", name,"-",trunkLoad,"吨",price,"元/天");
}
}
/**
* 小轿车(奔驰,雪佛兰)
* Created by haivo on 2016/3/24.
*/
public class Bus extends Car {
public Bus(String name,int busLoad,int price){
this.name = name;
this.busLoad = busLoad;
this.price = price;
}
@Override
public String toString() {
return String.format("%3s\t%d%s\t\t%s\t\t%d%s", name,busLoad,"人","-",price,"元/天");
}
}
/**
* 皮卡(风骏,拓路者)
* Created by haivo on 2016/3/24.
*/
public class PickUp extends Car {
public PickUp(String name,int busLoad,int trunkLoad,int price){
this.name = name;
this.busLoad = busLoad;
this.trunkLoad = trunkLoad;
this.price = price;
}
@Override
public String toString() {
return String.format("%3s\t%d%s\t\t%d%s\t\t%d%s", name,busLoad,"人",trunkLoad,"吨",price,"元/天");
}
}
/**
* 程序入口
* Created by haivo on 2016/3/24.
*/
public class SystemSurface {
public static void main(String[] args) {
Trunk donfFeng = new Trunk("东风", 10, 400);
Trunk jieFang = new Trunk("解放", 15, 500);
Bus xueFuLan = new Bus("雪佛兰", 6, 200);
Bus benz = new Bus("奔驰", 4, 300);
PickUp fengJun = new PickUp("风骏", 4, 3, 200);
PickUp tuoLuZhe = new PickUp("拓路者", 4, 5, 260);
Car car[] = new Car[]{donfFeng,jieFang,xueFuLan,benz,fengJun,tuoLuZhe};
String[] carInfo = new String[]{
donfFeng.toString(),
jieFang.toString(),
xueFuLan.toString(),
benz.toString(),
fengJun.toString(),
tuoLuZhe.toString()
};
System.out.println("欢迎使用达达租车系统.");
System.out.println("以下是价目表:\n");
System.out.println("序号\t\t汽车名称\t载客量\t载货量\t租金\t");
for(int num =0;num<=carInfo.length-1;num++) {
System.out.println(" "+(num+1)+"\t\t"+carInfo[num]);
}
System.out.println("您要租几辆车?");
Scanner scanner = new Scanner(System.in);
int carNumber = scanner.nextInt();//租车数量carNum
int chooseNo[] = new int[20]; //要租的车的序号chooseNo数组保存
for(int i=1;i<=carNumber;i++){
System.out.print("请输入第"+i+"辆车的序号:");
chooseNo[i] = scanner.nextInt();
}
int subPrice = 0;
for(int i=1;i<=carNumber;i++){
subPrice = subPrice + car[(chooseNo[i])-1].price;
}
System.out.println("您需要支付的金额是:"+subPrice);
}
}
点击查看更多内容
9人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦