package com.info;
public class Car {
String name; // 车名
double price; // 每小时价格
}
package com.info;
public class PiKa extends Bus { // 皮卡
int cap_goods; // 载货量
public PiKa(String name, int price, int cap_people, int cap_goods) {
super(name, price, cap_people);
this.cap_goods = cap_goods;
}
@Override
public String toString() {
return this.name + "\t\t" + this.price + "元/每天\t\t载人:" + this.cap_people + "人 载货:" + this.cap_goods + "吨";
}
}
package com.info;
public class Bus extends Car { // 客车
int cap_people; // 载人量
public Bus(String name, int price, int cap_people) {
this.name = name;
this.price = price;
this.cap_people = cap_people;
}
@Override
public String toString() {
return this.name + "\t\t" + this.price + "元/每天\t\t载人:" + this.cap_people + "人";
}
}
package com.info;
public class Truck extends Car { // 货车
int cap_goods; // 载货量
public Truck(String name, int price, int cap_goods) {
this.name = name;
this.price = price;
this.cap_goods = cap_goods;
}
@Override
public String toString() {
return this.name + "\t\t" + this.price + "元/每天\t\t载货:" + this.cap_goods + "吨";
}
}
package com.info;
import java.util.Scanner;
public class Shop { // 商店
private Bus aodi = new Bus("奥迪A4", 500, 4);
private Bus mazida = new Bus("马自达6", 400, 4);
private PiKa pikaxue = new PiKa("皮卡雪6", 450, 4, 2);
private Bus jinlong = new Bus("金龙", 800, 20);
private Truck songhuajiang = new Truck("松花江", 400, 4);
private Truck yiweike = new Truck("依维柯", 1000, 20);
private Car[] car_list = new Car[]{aodi, mazida, pikaxue, jinlong, songhuajiang, yiweike};
public void sell() {
System.out.println("欢迎使用答答出租车系统:");
System.out.println("您是否要租车:1 是 0 否");
Scanner input = new Scanner(System.in);
if (input.nextInt() == 0) {
System.out.println("欢迎下次光临!");
return;
}
display(); // 展示车的信息
int[] car_order = car_order(); // 车的订单序号
compute(car_order); // 计算以及打印账单
}
private void compute(int[] car_order) { // 计算以及打印账单
System.out.println("请输入租车天数:");
int days = new Scanner(System.in).nextInt(); // 租车天数
int num_people = 0; // 总载人量
int num_goods = 0; // 总载货量
int price = 0;
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
for (int i : car_order) {
if (car_list[i] instanceof Bus) {
System.out.print(car_list[i].name + "\t");
num_people += ((Bus) car_list[i]).cap_people;
price += car_list[i].price;
}
}
System.out.println("共载人:" + num_people + "人");
System.out.println("***可载货的车有:");
for (int i : car_order) {
if (car_list[i] instanceof Truck) {
System.out.print(car_list[i].name + "\t");
num_goods += ((Truck) car_list[i]).cap_goods;
price += car_list[i].price;
}
if (car_list[i] instanceof PiKa) {
System.out.print(car_list[i].name + "\t");
num_goods += ((PiKa) car_list[i]).cap_goods;
}
}
System.out.println("共载货:" + num_goods + "吨");
System.out.println("***租车总价格:" + price * days + "元");
}
private int[] car_order() { // 车的订单序号
System.out.println("请输入您要租汽车的数量:");
Scanner sc = new Scanner(System.in);
int car_num = sc.nextInt(); // 车的数量
int[] num = new int[car_num];
int i = 0;
while (car_num > 0 && i < car_num) {
System.out.println("请输入第" + (i + 1) + "车的序号:");
num[i] = sc.nextInt() - 1;
i++;
}
return num;
}
private void display() { // 展示车的信息
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t\t汽车名称\t\t租金\t\t容量");
int i = 1;
for (Car car : car_list) {
System.out.println(i + ".\t\t" + car);
i++;
}
}
}
package com.info;
public class Client {
public static void main(String[] args) {
new Shop().sell();
}
}