package com.imooc.main;
import java.util.Scanner;
public class CarSystem {
public static Scanner scanner;
// 程序入口
public static void main(String[] args) {
isNeed();
}
// 判断是否需要进入系统
public static void isNeed() {
System.out.println("欢迎使用达达租车系统:");
System.out.println("您是否要租车:1是 0否");
scanner = new Scanner(System.in);
String input = scanner.next();
if (input.equals("1")) {
list();
} else if (input.equals("0")) {
System.out.println("退出系统,欢迎下次再来!");
} else {
System.out.println("输入错误!请输入1或0");
isNeed();
}
}
// 生成车辆列表
public static void list() {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
Vehicle audi = new Car(1, "奥迪A4", 500, 4);
audi.say();
Vehicle mazda = new Car(2, "马自达6", 400, 4);
mazda.say();
Vehicle pickup6 = new Pickup(3, "皮卡雪6", 450, 4, 2);
pickup6.say();
Vehicle jinlong = new Car(4, "金龙", 800, 20);
jinlong.say();
Vehicle songhuajiang = new Truck(5, "松花江", 400, 4);
songhuajiang.say();
Vehicle yiweike = new Truck(6, "依维柯", 1000, 20);
yiweike.say();
Vehicle[] carlist = { audi, mazda, pickup6, jinlong, songhuajiang,
yiweike };
rent(carlist);
}
// 选择车辆,同时计算租金和载人/载货数量
public static void rent(Vehicle[] carlist) {
int fee = 0; // 总金额
int totalcontain_p = 0; // 总载人数
double totalcontain_g = 0; // 总载货量
System.out.println("请输入您要租车的数量:");
int num = scanner.nextInt();
int[] choose = new int[num];
if (num > 0 && num <= carlist.length) {
for (int i = 1; i <= num; i++) {
System.out.println("请输入第" + i + "辆车的序号:");
int inputserial = scanner.nextInt();
if (inputserial > 0 && inputserial <= carlist.length) {
choose[i - 1] = inputserial;
fee += carlist[inputserial - 1].getPrice();
totalcontain_p += carlist[inputserial - 1].getContain_p();
totalcontain_g += carlist[inputserial - 1].getContain_g();
} else {
System.out.println("输入错误,请输入1到" + carlist.length + "的数字!");
rent(carlist);
}
}
} else {
System.out.println("输入错误,请输入1到" + carlist.length + "的数字!");
rent(carlist);
}
// 计算总金额
System.out.println("请输入租车天数:");
int day = scanner.nextInt();
if (day <= 0) {
System.out.println("输入天数错误!");
rent(carlist);
} else {
fee = fee * day;
}
// 汇总账单信息
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
for (int i = 0; i < choose.length; i++) {
if (carlist[choose[i] - 1].getContain_p() != 0) {
System.out.print(carlist[choose[i] - 1].getCarname() + " ");
}
}
if (totalcontain_p == 0) {
System.out.println("没有可载人的车");
} else {
System.out.println("共载人:" + totalcontain_p + "人");
}
System.out.println("***可载货的车有:");
for (int i = 0; i < choose.length; i++) {
if (carlist[choose[i] - 1].getContain_g() != 0) {
System.out.print(carlist[choose[i] - 1].getCarname() + " ");
}
}
if (totalcontain_g == 0) {
System.out.println("没有可载货的车");
} else {
System.out.println("共载货:" + totalcontain_g + "吨");
}
System.out.println("***租车总价格:" + fee + "元");
}
}