Car.java
package RentCar;
public abstract class Car { // 父类
int value, id;
String name;
}
Bus.java
package RentCar;
public class Bus extends Car { // 客车
public int humanNumber;
public Bus(int id, String name, int value, int humanNumber) {
this.id = id;
this.name = name;
this.value = value;
this.humanNumber = humanNumber;
}
public String toString() {
return id + ". " + name + " | " + value + "元/天 | " + "载人:" + humanNumber + "人";
}
}
Truck.java
package RentCar;
public class Truck extends Car { // 货车
public int cargoNumber;
public Truck(int id, String name, int value, int cargoNumber) {
this.id = id;
this.name = name;
this.value = value;
this.cargoNumber = cargoNumber;
}
public String toString() {
return id + ". " + name + " | " + value + "元/天 | " + "载货" + cargoNumber + "吨";
}
}
Pickup.java
package RentCar;
public class Pickup extends Car { // 皮卡
public int humanNumber, cargoNumber;
public Pickup(int id, String name, int value, int humanNumber, int cargoNumber) {
this.id = id;
this.name = name;
this.value = value;
this.humanNumber = humanNumber;
this.cargoNumber = cargoNumber;
}
public String toString() {
return id + ". " + name + " | " + value + "元/天 | " + "载人:" + humanNumber + "人" + " 载货" + cargoNumber + "吨";
}
}
Main.java
package RentCar;
import java.util.Scanner;
public class Main {
public void Solve() {
int humanNumber = 0, cargoNumber = 0, cost = 0;
String rentCar = "总共租了这些车:", human = "可以载客的车有:", cargo = "可以载货的车有:";
Scanner scan = new Scanner(System.in);
Car[] cars = {
new Bus(1, "奥迪A4", 500, 4),
new Bus(2, "马自达6", 400, 4),
new Pickup(3, "皮卡雪6", 450, 4, 2),
new Bus(4, "金龙", 800, 20),
new Truck(5, "松花江", 400, 4),
new Truck(6, "依维柯", 1000, 20)
};
System.out.println("欢迎使用租车系统");
System.out.println("您是否要租车? 1.是 2.不是");
int cas = scan.nextInt();
if(cas == 1) {
System.out.println("您可以租用的车: ");
System.out.println("序号 " + "名字 " + "价格 " + "容量");
for(int i = 0; i < cars.length; i++)
System.out.println(cars[i]);
System.out.println("请输入您要租的车的数量:");
int tol = scan.nextInt();
for(int now = 1; now <= tol; now++) {
System.out.println("请输入第" + now + "辆车的序号");
int cur = scan.nextInt();
if(cur < 1 || cur > cars.length) {
System.out.println("输入的序号不合法, 请重新输入");
now--;
} else {
cur--;
if(cars[cur].getClass() == Bus.class) {
human += cars[cur].name + " ";
humanNumber += ((Bus)cars[cur]).humanNumber;
} else if(cars[cur].getClass() == Truck.class) {
cargo += cars[cur].name + " ";
cargoNumber += ((Truck)cars[cur]).cargoNumber;
} else {
human += cars[cur].name + " ";
humanNumber += ((Pickup)cars[cur]).humanNumber;
cargo += cars[cur].name + " ";
cargoNumber += ((Pickup)cars[cur]).cargoNumber;
}
cost += cars[cur].value;
}
}
System.out.println("请输入租车天数: ");
int days = scan.nextInt();
System.out.println("您的账单:");
System.out.println(rentCar);
System.out.println(human);
System.out.println("共可载" + humanNumber + "人");
System.out.println(cargo);
System.out.println("共可载" + cargoNumber + "吨");
System.out.println("租车总价格:" + days * cost + "元");
System.out.println("祝您使用愉快,欢迎下次光临");
} else if(cas == 2) {
System.out.println("欢迎下次光临");
} else {
System.out.println("输入不合法\n欢迎下次光临");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Main main = new Main();
main.Solve();
}
}
输出
欢迎使用租车系统
您是否要租车? 1.是 2.不是
1
您可以租用的车:
序号 名字 价格 容量
1. 奥迪A4 | 500元/天 | 载人:4人
2. 马自达6 | 400元/天 | 载人:4人
3. 皮卡雪6 | 450元/天 | 载人:4人 载货2吨
4. 金龙 | 800元/天 | 载人:20人
5. 松花江 | 400元/天 | 载货4吨
6. 依维柯 | 1000元/天 | 载货20吨
请输入您要租的车的数量:
4
请输入第1辆车的序号
1
请输入第2辆车的序号
2
请输入第3辆车的序号
3
请输入第4辆车的序号
4
请输入租车天数:
4
您的账单:
总共租了这些车:
可以载客的车有:奥迪A4 马自达6 皮卡雪6 金龙
共可载32人
可以载货的车有:皮卡雪6
共可载2吨
租车总价格:8600元
祝您使用愉快,欢迎下次光临