package com.yuyong;
/**
* Created by yuyong on 2017/2/8.
*/
// 新建一个Car抽象类
public abstract class Car {
private String type;
private int price;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return id + "\t" + type + "\t" + price;
}
}
package com.yuyong;
/**
* Created by yuyong on 2017/2/8.
*/
// 客车类
public class Bus extends Car implements IBusLoad {
private int busLoad;
@Override
public int getBusLoad() {
return busLoad;
}
@Override
public void setBusLoad(int busLoad) {
this.busLoad = busLoad;
}
public Bus(int id, String type, int price, int busLoad) {
super();
super.setId(id);
super.setType(type);
super.setPrice(price);
this.setBusLoad(busLoad);
}
@Override
public String toString() {
return super.toString() + "\t" + "载人:" + this.getBusLoad() + "人";
}
}
package com.yuyong;
/**
* Created by yuyong on 2017/2/8.
*/
// 货车类
public class Trunk extends Car implements ITrunkLoad {
private int trunkLoad;
@Override
public int getTrunkLoad() {
return trunkLoad;
}
@Override
public void setTrunkLoad(int trunkLoad) {
this.trunkLoad = trunkLoad;
}
public Trunk(int id, String type, int price, int trunkLoad) {
super();
super.setId(id);
super.setType(type);
super.setPrice(price);
this.setTrunkLoad(trunkLoad);
}
@Override
public String toString() {
return super.toString() + "\t" + "载货:" + this.getTrunkLoad() + "吨";
}
}
package com.yuyong;
/**
* Created by yuyong on 2017/2/8.
*/
// 皮卡类
public class Pickup extends Car implements IBusLoad, ITrunkLoad {
private int busLoad;
private int trunkLoad;
public Pickup(int id, String type, int price, int busLoad, int trunkLoad) {
super();
super.setId(id);
super.setType(type);
super.setPrice(price);
this.setBusLoad(busLoad);
this.setTrunkLoad(trunkLoad);
}
@Override
public int getBusLoad() {
return busLoad;
}
@Override
public void setBusLoad(int busLoad) {
this.busLoad = busLoad;
}
@Override
public int getTrunkLoad() {
return trunkLoad;
}
@Override
public void setTrunkLoad(int trunkLoad) {
this.trunkLoad = trunkLoad;
}
@Override
public String toString() {
return super.toString() + "\t" + "载人:" + this.getBusLoad() + "人" + "\t" + "载货:" + this.getTrunkLoad() + "吨";
}
}
package com.yuyong;
/**
* Created by yuyong on 2017/2/8.
*/
// 载客量
public interface IBusLoad {
public int getBusLoad();
public void setBusLoad(int busLoad);
}
package com.yuyong;
/**
* Created by yuyong on 2017/2/8.
*/
// 载货量
public interface ITrunkLoad {
public int getTrunkLoad();
public void setTrunkLoad(int trunkLoad);
}
package com.yuyong;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("欢迎使用DaDa租车系统!");
while (true) {
System.out.println("您是否要租车?:1 是 ,0 否");
Scanner in = new Scanner(System.in);
try {
int choice;
choice = in.nextInt();
if (choice == 0) {
in.close();
break;
} else if (choice == 1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
List<Car> carList = initCarList();
int[][] carNumber = selectCar(carList, in);
printOrder(carList, carNumber);
System.out.println();
in.close();
break;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("指令有误! 请输入正确的指令!");
}
}
System.out.println("感谢使用DaDa租车系统!");
}
private static void printOrder(List<Car> carList, int[][] carNumber) {
List<Car> selectedCars = new ArrayList<>();
int carTypeNumber = carList.size();
int[] rent = new int[carTypeNumber];
int rentSum = 0;
System.out.println();
System.out.println("--------------------------------");
System.out.println("您的订单:");
System.out.println("汽车名称\t数量\t天数\t每日租金\t总租金");
for (int i = 0; i < carTypeNumber; i++) {
if (carNumber[i][1] != 0) {
for (int j = 0; j < carNumber[i][1]; j++) {
selectedCars.add(carList.get(i));
}
rent[i] = carNumber[i][2] * carList.get(i).getPrice() * carNumber[i][1];
System.out.println(carList.get(i).getType() + "\t" + carNumber[i][1] + "\t" + carNumber[i][2] + "\t" +
carList.get(i).getPrice() + "\t" + rent[i]);
rentSum = rentSum + rent[i];
}
}
System.out.println("--------------------------------");
System.out.println("\t\t\t订单总金额(¥): " + rentSum);
}
private static int[][] selectCar(List<Car> carList, Scanner in) {
int carTypeNumber = carList.size();
int[][] carNumber = new int[carTypeNumber][3];
for (int i = 0; i < carTypeNumber; i++) {
carNumber[i][0] = i + 1;
System.out.println("请输入车型(" + carList.get(i).getType() + ")的数量,没有请输入0:");
// 每款车的数量
carNumber[i][1] = in.nextInt();
if (carNumber[i][1] == 0) {
continue;
} else {
// 同一款车每辆车的租期
carNumber[i][2] = 0;
int carRentDay;
for (int j = 1; j <= carNumber[i][1]; j++) {
System.out.println("请输入第" + j + "辆车(" + carList.get(i).getType() + ")的租用天数:");
carRentDay = in.nextInt();
if (carRentDay <= 0) {
j--;
System.out.println("请输入一个大于等于1的数字。");
continue;
} else {
carNumber[i][2] = carNumber[i][2] + carRentDay;
}
}
}
}
return carNumber;
}
private static List<Car> initCarList() {
List<Car> carList = new ArrayList<>();
carList.add(new Bus(1, "奥迪A6", 500, 4));
carList.add(new Bus(2, "马自达6", 400, 4));
carList.add(new Pickup(3, "皮卡雪6", 450, 4, 2));
carList.add(new Bus(4, "金龙", 800, 20));
carList.add(new Trunk(5, "松花江", 300, 3));
carList.add(new Trunk(6, "依维柯", 900, 10));
for (Car i : carList) {
System.out.println(i.toString());
}
return carList;
}
}
欢迎使用DaDa租车系统!
您是否要租车?:1 是 ,0 否
1
您可租车的类型及其价目表:
序号 汽车名称 租金 容量
1 奥迪A6 500 载人:4人
2 马自达6 400 载人:4人
3 皮卡雪6 450 载人:4人 载货:2吨
4 金龙 800 载人:20人
5 松花江 300 载货:3吨
6 依维柯 900 载货:10吨
请输入车型(奥迪A6)的数量,没有请输入0:
2
请输入第1辆车(奥迪A6)的租用天数:
1
请输入第2辆车(奥迪A6)的租用天数:
2
请输入车型(马自达6)的数量,没有请输入0:
3
请输入第1辆车(马自达6)的租用天数:
1
请输入第2辆车(马自达6)的租用天数:
2
请输入第3辆车(马自达6)的租用天数:
3
请输入车型(皮卡雪6)的数量,没有请输入0:
0
请输入车型(金龙)的数量,没有请输入0:
0
请输入车型(松花江)的数量,没有请输入0:
1
请输入第1辆车(松花江)的租用天数:
1
请输入车型(依维柯)的数量,没有请输入0:
0
您的订单:
汽车名称 数量 天数 每日租金 总租金
奥迪A6 2 3 500 3000
马自达6 3 6 400 7200
松花江 1 1 300 300
订单总金额(¥): 10500
感谢使用DaDa租车系统!
点击查看更多内容
25人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦