//Car package com.Car; public class Car { String name; double rent; public void Car(String name,double rent){}; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getRent() { return rent; } public void setRent(double rent) { this.rent = rent; } } //PassengerCar package com.Car; public class PassengerCar extends Car { private double peopleC; public PassengerCar(String name,double rent,double peopleC){ this.name = name; this.rent = rent; this.peopleC = peopleC; } public double getPeopleC() { return peopleC; } public void setPeopleC(double peopleC) { this.peopleC = peopleC; } } //Pickup package com.Car; public class Pickup extends Car{ private double peopleC; private double cargoC; public Pickup(String name,double rent,double cargoC,double peopleC) { this.name = name; this.rent = rent; this.cargoC = cargoC; this.peopleC = peopleC; } public double getCargoC() { return cargoC; } public void setCargoC(double cargoC) { this.cargoC = cargoC; } public double getPeopleC() { return peopleC; } public void setPeopleC(double peopleC) { this.peopleC = peopleC; } } //Truck package com.Car; public class Truck extends Car { private double cargoC; public Truck(String name,double rent,double cargoC) { this.name = name; this.rent = rent; this.cargoC = cargoC; } public double getCargoC() { return cargoC; } public void setCargoC(double cargoC) { this.cargoC = cargoC; } } package com.Car; import java.util.Scanner; public class Test { public static void main(String[] args) { Car[] cars = { new PassengerCar("金龙大巴", 1000, 40), new PassengerCar("奥迪A6", 500, 4), new Pickup("小皮卡", 300, 5, 4), new Truck("大卡车", 800, 25) }; Scanner Type = new Scanner(System.in); System.out.println("欢迎使用答答租车系统"); System.out.println("您是否要租车:1是 0否"); String input = Type.next(); if (input.equals("1")) { System.out.println("您可租车的类型及价目表:"); System.out.println("序号\t汽车名称\t租金\t\t容量"); int i = 1;// 车辆序号 for (Car Allcars : cars) { if (Allcars instanceof PassengerCar) { System.out.println(i + "\t" + Allcars.getName() + "\t" + Allcars.getRent() + "/天\t载人:" + ((PassengerCar) Allcars).getPeopleC() + "/人"); i += 1; } if (Allcars instanceof Pickup) { System.out.println(i + "\t" + Allcars.getName() + "\t" + Allcars.getRent() + "/天\t载人:" + ((Pickup) Allcars).getPeopleC() + "/人 载货:" + ((Pickup) Allcars).getCargoC() + "吨"); i += 1; } if (Allcars instanceof Truck) { System.out.println(i + "\t" + Allcars.getName() + "\t" + Allcars.getRent() + "/天\t载货:" + ((Truck) Allcars).getCargoC() + "吨"); i += 1; } } System.out.println("请输入您要租车的数量:"); int num = Type.nextInt(); double rentsum = 0;// 租车总金额 double Peosum = 0;// 载人总数 double CargoCsum = 0;// 载货总数 String[] PassengerName = new String[4];// 存放可载人车辆的name的数组 String[] CargoCars = new String[4];// 存放可载货车辆的name的数组 int p = 0; int c = 0; for (int n = 1; n <= num; n++) { System.out.println("输入第" + n + "辆车的序号:"); int x = Type.nextInt(); rentsum += cars[x - 1].rent;// 计算租车金额 switch (x - 1) { case 0: if (cars[x - 1] instanceof PassengerCar) { PassengerName[p] = cars[x - 1].name; Peosum += ((PassengerCar) cars[x - 1]).getPeopleC(); p += 1; } if (cars[x - 1] instanceof Pickup) { PassengerName[p] = cars[x - 1].name; CargoCars[c] = cars[x - 1].name; Peosum += ((Pickup) cars[x - 1]).getPeopleC(); CargoCsum += ((Pickup) cars[x - 1]).getCargoC(); p += 1; c += 1; } if (cars[x - 1] instanceof Truck) { CargoCars[c] = cars[x - 1].name; CargoCsum += ((Truck) cars[x - 1]).getCargoC(); c += 1; } break; case 1: if (cars[x - 1] instanceof PassengerCar) { PassengerName[p] = cars[x - 1].name; Peosum += ((PassengerCar) cars[x - 1]).getPeopleC(); p += 1; } if (cars[x - 1] instanceof Pickup) { PassengerName[p] = cars[x - 1].name; CargoCars[c] = cars[x - 1].name; Peosum += ((Pickup) cars[x - 1]).getPeopleC(); CargoCsum += ((Pickup) cars[x - 1]).getCargoC(); p += 1; c += 1; } if (cars[x - 1] instanceof Truck) { CargoCars[c] = cars[x - 1].name; CargoCsum += ((Truck) cars[x - 1]).getCargoC(); c += 1; } break; case 2: if (cars[x - 1] instanceof PassengerCar) { PassengerName[p] = cars[x - 1].name; Peosum += ((PassengerCar) cars[x - 1]).getPeopleC(); p += 1; } if (cars[x - 1] instanceof Pickup) { PassengerName[p] = cars[x - 1].name; CargoCars[c] = cars[x - 1].name; Peosum += ((Pickup) cars[x - 1]).getPeopleC(); CargoCsum += ((Pickup) cars[x - 1]).getCargoC(); p += 1; c += 1; } if (cars[x - 1] instanceof Truck) { CargoCars[c] = cars[x - 1].name; CargoCsum += ((Truck) cars[x - 1]).getCargoC(); c += 1; } break; case 3: if (cars[x - 1] instanceof PassengerCar) { PassengerName[p] = cars[x - 1].name; Peosum += ((PassengerCar) cars[x - 1]).getPeopleC(); p += 1; } if (cars[x - 1] instanceof Pickup) { PassengerName[p] = cars[x - 1].name; CargoCars[c] = cars[x - 1].name; Peosum += ((Pickup) cars[x - 1]).getPeopleC(); CargoCsum += ((Pickup) cars[x - 1]).getCargoC(); p += 1; c += 1; } if (cars[x - 1] instanceof Truck) { CargoCars[c] = cars[x - 1].name; CargoCsum += ((Truck) cars[x - 1]).getCargoC(); c += 1; } break; default: break; } } System.out.println("请输入租车天数:"); int y = Type.nextInt(); System.out.println("您的账单:"); System.out.println("***可载人的车有:"); for (String pn : PassengerName) { System.out.print(pn + "\t"); } System.out.println("载人总数:" + Peosum + "人"); System.out.println("***可载货的车有:"); for (String cc : CargoCars) { System.out.print(cc + "\t"); } System.out.println("载货总数:" + CargoCsum + "吨"); rentsum = rentsum * y; System.out.println("***租车总金额:" + rentsum); } else if (input.equals("2")) { System.out.println("欢迎您访问答答租车系统,再见。"); } else { System.out.println("输入错误!"); main(null);// 再次调用主函数 } } }
yanrun
cafebabe0x
相关分类