import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("~~~~~~~欢迎使用哒哒租车~~~~~~~~ ");
System.out.println("您是否要租车:1.是 >> 点击其他键退出");
Test test = new Test();
while (test.test1()) {
System.out.println(">>>您可选择车的类型及其价目表:");
System.out.println("-------------------------------------------");
Car[] cars = {new PassengerCar(1, "奥迪A8L", 600, 4),
new PassengerCar(2, "奔驰G", 800, 4),
new Pickup(3, "福特猛禽", 1300, 4, 2),
new PassengerCar(4, "宝马M5", 700, 20),
new Truck(5, "斯柯达", 500, 4),
new Truck(6, "依维柯", 1000, 20)
};
System.out.println("序号" + " " + "\t" + "车型" + " " + "\t" + "租金" + " " + "\t" + "容量");
for (Car car : cars) {
//判断左边的类是否是右边的实例类
if (car.getClass().equals(PassengerCar.class)) {
//如果是客车的实例类
System.out.println(car.getId() + "\t" + "\t" + car.getName() + "\t" + "\t" + car.getPrice() + "/天" + "\t"
+ "\t" + "载客" + ((PassengerCar) car).getCapacity() + "/人");
}
if (car.getClass().equals(Truck.class)) {
System.out.println(car.getId() + "\t" + "\t" + car.getName() + "\t" + "\t" + car.getPrice() + "/天" + "\t"
+ "\t" + "载货" + ((Truck) car).getCargoVolume() + "/吨");
}
if (car.getClass().equals(Pickup.class)) {
System.out.println(car.getId() + "\t" + "\t" + car.getName() + "\t" + "\t" + car.getPrice() + "/天" + "\t"
+ "\t" + "载客" + ((Pickup) car).getCapacity() + "/人" + " " + "载货" + ((Pickup) car).getCargoVolume() + "/吨");
}
}
System.out.println("-------------------------------------------");
System.out.println("-->请输入你要租的汽车的数量:");
Scanner z = new Scanner(System.in);
int carNum = z.nextInt();//用于接收租车的数量
List<Car> carList = new ArrayList<Car>();
int add = 0;
List<Car> carListPassenger = new ArrayList<>(); //用来储存可载人的车辆
List<Car> carListTrunk = new ArrayList<>(); //用来储存可载货的车辆
int passengerCar = 0; //载人数
int carCargo = 0; //载货量
int carPrice = 0; //总资金
for (int i = 0; i < carNum; i++) {
System.out.println(">>请输入第" + (i + 1) + "辆车的序号:");
int num = z.nextInt(); //用于接收输入的汽车的序号
carList.add(cars[num - 1]);
System.out.println("----成功添加:" + carList.get(add).getName());
if (cars[num - 1].getClass().equals(PassengerCar.class)) {
passengerCar += ((PassengerCar) carList.get(add)).getCapacity();
carPrice += ((PassengerCar) carList.get(add)).getPrice();
carListPassenger.add(carList.get(add));
}
if (cars[num - 1].getClass().equals(Pickup.class)) {
passengerCar += ((Pickup) carList.get(add)).getCapacity();
carCargo += ((Pickup) carList.get(add)).getCargoVolume();
carPrice += ((Pickup) carList.get(add)).getPrice();
carListPassenger.add(carList.get(add));
carListTrunk.add(carList.get(add));
}
if (cars[num - 1].getClass().equals(Truck.class)) {
carCargo += ((Truck) carList.get(add)).getCargoVolume();
carPrice += ((Truck) carList.get(add)).getPrice();
carListTrunk.add(carList.get(add));
}
add++;
}
System.out.println(">>请输入租车的天数:");
Scanner g = new Scanner(System.in);
int carDay = g.nextInt();
carPrice = carPrice * carDay;
System.out.println("------------租车选车完成------------" + "\n" + "下面开始统计数据..........");
//租车完成,开始统计数据并输出
System.out.print("您租的车共有" + carNum + "辆:" + " ");
for (Car car : carList) {
System.out.print(car.getName() + " ");
}
System.out.println();
System.out.println("共租用:" + carDay + " 天");
System.out.println("可载人:" + passengerCar + " 人");
System.out.println("可载货:" + carCargo + " 吨");
System.out.println("需要付款:" + carPrice + " 元");
System.out.println("->请输入付款金额:");
System.out.println("------------");
Scanner x = new Scanner(System.in);
while (carPrice != x.nextInt())
System.out.println("------------" + "\n" + "输入错误,请重新输入金额!");
System.out.println("------------");
System.out.println("->请输入付款密码:");
Scanner y = new Scanner(System.in);
while (y.nextInt() != 123456)
System.out.println("------------" + "\n" + "密码错误,请重新输入!");
System.out.println("------------");
System.out.println(" 交易成功!");
System.out.println();
System.out.println("------------感谢您的使用--------------");
System.out.println("………………继续租车请按1,退出请按其他键………………");
}
System.out.println("***欢迎您再次使用,再见!***");
}
// 捕获输入参数不正确异常
public boolean test1() {
try {
Scanner z = new Scanner(System.in);
if (z.nextInt() == 1) {
return true;
} else {
return false;
}
} catch (Exception e1) {
return false;
}
}
}