慕圣8516364
2020-08-13 21:45
父类Car(属性name,price)
子类Trunk(独有属性:载货量)
子类Bus(独有属性:载客量)
子类Pickup(独有属性:载货量,载客量)
CarRentTest(测试类)
package CarRent; public class Car { private String carName; private int carPrice; public Car(String name, int price) { carName = name; carPrice = price; } public String getCarName() { return carName; } public int getCarPrice() { return carPrice; } public void setCarName(String carName) { this.carName = carName; } public void setCarPrice(int carPrice) { this.carPrice = carPrice; } }
package CarRent; /* 货车,只能载货 */ public class Truck extends Car { private int boatload; // 载货量 public Truck(String name, int price, int boatload) { super(name, price); this.boatload = boatload; } public int getBoatload() { return boatload; } public void setBoatload(int boatload) { this.boatload = boatload; } }
package CarRent; /* 客车,只能载人 */ public class Bus extends Car { private int busload; // 载客量 public Bus(String name, int price, int busload) { super(name, price); this.busload = busload; } public int getBusload() { return busload; } public void setBusload(int busload) { this.busload = busload; } }
package CarRent; /* 皮卡,既可以载货也可以载人 */ public class Pickup extends Car { private int boatload; // 载货量 private int busload; // 载客量 public Pickup(String name, int price, int boatload, int busload) { super(name, price); this.boatload = boatload; this.busload = busload; } public int getBoatload() { return boatload; } public int getBusload() { return busload; } public void setBoatload(int boatload) { this.boatload = boatload; } public void setBusload(int busload) { this.busload = busload; } }
package CarRent; import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class CarRentTest { public static void main(String[] args) { Car availableCars[] = new Car[] { new Bus("奥迪A4", 500, 4), new Bus("马自达6", 400, 4), new Pickup("皮卡雪6", 450, 2, 4), new Bus("金龙", 800, 20), new Truck("松花江", 400, 4), new Truck("依维柯", 1000, 20) }; Scanner input = new Scanner(System.in); System.out.println("欢迎使用答答租车系统:"); System.out.print("您是否要租车:1 是;0 否 >>> "); int rentOrNot = input.nextInt(); while (rentOrNot != 0 && rentOrNot != 1) { System.out.print("无效输入。请重新输入:1 是;0 否 >>> "); rentOrNot = input.nextInt(); if (rentOrNot == 0) { System.out.println("再见!欢迎下次使用!"); System.exit(0); } else if (rentOrNot == 1) { break; } else { continue; } } System.out.println("您可租车的类型及其价目表:"); System.out.println("序号\t汽车名称\t租金\t\t容量"); for (int i = 0; i < availableCars.length; i++) { Car car = availableCars[i]; System.out.print((i+1)+".\t"); System.out.print(car.getCarName()+"\t\t"); System.out.print(car.getCarPrice()+"元/天\t"); if (availableCars[i] instanceof Truck) { System.out.print("载货:"+((Truck)car).getBoatload()+"吨\n"); } else if (availableCars[i] instanceof Bus) { System.out.print("载人:"+((Bus)car).getBusload()+"人\n"); } else if (availableCars[i] instanceof Pickup) { System.out.print("载人:"+((Pickup)car).getBusload()+"人 载货:"+((Pickup)car).getBoatload()+"吨\n"); } } System.out.print("请输入您要租汽车的数量(0 ~ "+availableCars.length+"):>>> "); int rentNumber = input.nextInt(); while (rentNumber < 0 || rentNumber > availableCars.length) { System.out.print("无效输入。请重新输入(0 ~ "+availableCars.length+"):>>> "); rentNumber = input.nextInt(); } List<Integer> carsAlreadyPicked = new ArrayList<>(); for (int i = 1; i <= rentNumber; i++) { System.out.print("请输入第"+i+"辆车的序号:>>> "); int carID = input.nextInt(); while (carsAlreadyPicked.contains(carID) || carID < 1 || carID > availableCars.length) { if (carID < 1 || carID > availableCars.length) { System.out.print("无效输入。请重新输入:>>> "); } else { System.out.print("该车已选择。请重新选择:>>> "); } carID = input.nextInt(); } carsAlreadyPicked.add(carID); } System.out.print("请输入租车天数(> 0):>>> "); int rentDay = input.nextInt(); while (rentDay <= 0) { System.out.print("无效输入。请重新输入:>>> "); rentDay = input.nextInt(); } int totalExpense = 0, totalBusLoad = 0, totalBoatLoad = 0; for (int id : carsAlreadyPicked) { totalExpense += availableCars[id-1].getCarPrice(); } totalExpense *= rentDay; System.out.println("您的账单:"); System.out.println("***可载人的车有:"); for (int id : carsAlreadyPicked) { Car car = availableCars[id-1]; if (availableCars[id-1] instanceof Bus || availableCars[id-1] instanceof Pickup) { System.out.print(car.getCarName()+"\t"); totalBusLoad += (availableCars[id-1] instanceof Bus) ? ((Bus)car).getBusload() : ((Pickup)car).getBusload(); } } System.out.print("共载人:"+totalBusLoad+"人\n"); System.out.println("***载货的车有:"); for (int id : carsAlreadyPicked) { Car car = availableCars[id-1]; if (availableCars[id-1] instanceof Truck || availableCars[id-1] instanceof Pickup) { System.out.print(car.getCarName()+"\t"); totalBoatLoad += (availableCars[id-1] instanceof Truck) ? ((Truck)car).getBoatload() : ((Pickup)car).getBoatload(); } } System.out.print("共载货:"+(double)totalBoatLoad+"吨\n"); System.out.print("***租车总价格:"); System.out.print(totalExpense+"元\n"); input.close(); System.out.println("再见!欢迎下次使用!"); } }
List<Integer> carsAlreadyPicked =
new
ArrayList<>(); 有木有人告诉我这个语句的作用
参考了你的作业,写得很好,对象数组、instanceof判断对象类型、查重(能看懂,不会用)、遍历都用得很好
写得很好
不错,比我的少?
Java入门第二季 升级版
530559 学习 · 6091 问题
相似问题