创建Car类
public class Car {
public String carName; //车名
public int ID, personLoad; //序号、载客量
public double carRental, meterLoad; //租金、载货量
@Override
public String toString() {
return "ID:" + ID + ", 名称:" + carName
+ ", 载客量:" + personLoad + ", 载货量:"
+ meterLoad + ", 租金:" + carRental;
}
}
创建Bus类,继承Car类
public class Bus extends Car {
public Bus() {
super();
// TODO Auto-generated constructor stub
}
public Bus(int ID, String carName, int personLoad, double meterLoad, double carRental){
this.ID = ID;
this.carName = carName;
this.personLoad = personLoad;
this.meterLoad = meterLoad;
this.carRental = carRental;
}
}
创建皮卡类,继承Car类
public class PickUp extends Car {
public PickUp() {
super();
// TODO Auto-generated constructor stub
}
public PickUp(int ID, String carName, int personLoad, double meterLoad, double carRental){
this.ID = ID;
this.carName = carName;
this.personLoad = personLoad;
this.meterLoad = meterLoad;
this.carRental = carRental;
}
}
创建卡车类,继承Car类
public class Truck extends Car {
public Truck() {
super();
// TODO Auto-generated constructor stub
}
public Truck(int ID, String carName, int personLoad, double meterLoad, double carRental){
this.ID = ID;
this.carName = carName;
this.personLoad = personLoad;
this.meterLoad = meterLoad;
this.carRental = carRental;
}
}
创建展示类,答答用车系统界面
import java.util.Scanner;
public class Show {
public static void main (String[] args){
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车呢?1是;0否");
Scanner sn = new Scanner(System.in);
int isGet = sn.nextInt(); //判断是否需要租车
if (isGet == 1){
//显示车的信息
System.out.println("在库车的信息如下:序号、名称、载客数、载货量、租金"); //显示车的信息
Car[] car = new Car[]{new Bus(1, "奥迪A4", 500, 0, 4),
new Bus(2, "马自达6", 400, 0, 4),
new PickUp(3, "皮卡雪", 450, 4, 2),
new Bus(4, "金龙", 800, 0, 20),
new Truck(5, "松花江", 0, 400, 4),
new Truck(6, "依维柯", 0, 1000, 20)};
for(int i = 0; i <= car.length - 1; i ++){
System.out.println(car[i]);
}
//需要使用几辆车
System.out.println("您需要使用几辆车呢");
int useCarNum = sn.nextInt(); //获取想要使用的车辆
//当需要用车的时间大于0时
if (useCarNum > 0){
//创建数组,用于存储员工输入的车序号
int[] CarNum = new int[useCarNum];
//将序号写入到数组中中
for (int i = 1; i <= useCarNum; i++){
System.out.println("请输入第" + i + "辆车的序号");
int Order = sn.nextInt();
CarNum[i - 1] = Order; //将车的序号写入数组中
}
//获取租车天数
System.out.println("请输入租车的天数");
int CarDay = sn.nextInt();
//当数组里面有数据的时候
if (CarNum.length > 0){
double Totalmoney = 0;
//打印用车信息
System.out.println("您使用的车如下:");
for(int i = 0; i < CarNum.length; i++){
System.out.println(car[CarNum[i] - 1]);
Totalmoney = Totalmoney + CarDay * car[CarNum[i] - 1].carRental;
}
System.out.println("租期为:" + CarDay + "天,总租金为:" + Totalmoney + "元");
}
}else{
System.out.println("输入有误");
}
}else{
System.out.println("欢迎下次使用,再见");
}
sn.close();
}
}
显示结果