//总类
public class Car { private String name; private int prices; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrices() { return prices; } public void setPrices(int prices) { this.prices = prices; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
载货接口:
public interface CargoNum { void setCargoNum(int num); }
载人接口:
public interface PersonNum { void setPersonNum(int num); }
载人车类:
public class Bus extends Car implements PersonNum{ private int personNum; public Bus(String name, int prices, int personNum) { super.setName(name); super.setPrices(prices); this.personNum = personNum; } public int getPersonNum() { return personNum; } public void setPersonNum(int personNum) { this.personNum = personNum; } @Override public String toString() { return getName()+"\t\t"+ getPrices()+"/天\t"+ "载人:"+getPersonNum()+"人"; } }
载货车类:
public class CargoCar extends Car implements CargoNum{ private int cargoNum; public CargoCar(String name, int prices, int cargoNum) { this.setName(name); this.setPrices(prices); this.cargoNum = cargoNum; } public int getCargoNum() { return cargoNum; } public void setCargoNum(int cargoNum) { this.cargoNum = cargoNum; } @Override public String toString() { return getName()+"\t\t"+ getPrices()+"/天\t"+ "载货:"+getCargoNum()+"吨"; } }
混合车类:
public class MixCar extends Car implements PersonNum, CargoNum { private int cargoNum; private int personNum; public MixCar(String name, int prices, int personNum, int cargoNum) { super.setName(name); super.setPrices(prices); this.personNum = personNum; this.cargoNum = cargoNum; } @Override public void setCargoNum(int num) { cargoNum = num; } @Override public void setPersonNum(int num) { personNum = num; } public int getCargoNum() { return cargoNum; } public int getPersonNum() { return personNum; } @Override public String toString() { return getName() + "\t\t" + getPrices() + "/天\t" + "载人:" + getPersonNum() + "人 "+ "载货:" + getPersonNum() + "吨"; } }
工具处理类:
public class CarUtil { private static int id = 0; private List<Car> carUtilList; public CarUtil() { id = 0; carUtilList = new ArrayList<>(); } public void add(Car car) { id++; if (car instanceof Bus) { Bus bus = (Bus) car; System.out.println(id+".\t\t"+ bus.toString()); } else if (car instanceof CargoCar) { CargoCar cargoCar = (CargoCar) car; System.out.println(id+".\t\t"+cargoCar.toString()); }else if (car instanceof MixCar) { MixCar mixCar = (MixCar) car; System.out.println(id+".\t\t"+mixCar.toString()); } carUtilList.add(car); } public Car getCar(int index) { return carUtilList.get(index); } }
主函数:
public class Main { private static CarUtil carUtil; public static void main(String[] args) { System.out.println("欢迎使用大大租车系统:\n" + "您是否要租车:1是 0否"); int isUse = getInput(); while (isUse != 1 && isUse != 0) { System.out.println("输入错误。"); System.out.println("欢迎使用大大租车系统:\n" + "您是否要租车:1是 0否"); isUse = getInput(); } if (isUse == 0) { System.out.println("系统已退出"); return; } else { System.out.println("您可租车的类型及价目表:"); System.out.println("序号\t汽车名称\t 租金\t 容量"); initData(); System.out.println("请输入您要租车的数量"); int num = getInput(); int[] index = new int[num]; for (int i = 0; i < num; i++) { System.out.println("请输入第" + (i + 1) + "辆车的序号:"); index[i] = getInput(); } System.out.println("请输入租车天数:"); int days = getInput(); output(index, days); } } private static void output(int[] indexes, int days) { int numPeople = 0; int numCargo = 0; StringBuilder cargoCarName = new StringBuffer(); StringBuilder pCarName = new StringBuffer(); int price = 0; for (int index : indexes) { System.out.println(index + ""); Car car = carUtil.getCar(index-1); price += car.getPrices(); if (car instanceof Bus) { Bus bus = (Bus) car; pCarName.append(car.getName()).append(" "); numPeople = bus.getPersonNum() + numPeople; } else if (car instanceof MixCar) { MixCar mixCar = (MixCar) car; pCarName.append(car.getName()).append(" "); cargoCarName.append(car.getName()).append(" "); numPeople += mixCar.getPersonNum(); numCargo += mixCar.getCargoNum(); } else if (car instanceof CargoCar) { CargoCar cargoCar = (CargoCar) car; cargoCarName.append(car.getName()).append(" "); numCargo += cargoCar.getCargoNum(); } } System.out.println("***可载人的车有:"); System.out.println(pCarName + "共载人:" + numPeople + "人"); System.out.println("***可载货的车有:"); System.out.println(cargoCarName + "共载货:" + numCargo + "吨"); System.out.println("***出租车总价格:" + price*days); } public static int getInput() { Scanner scanner = new Scanner(System.in); return scanner.nextInt(); } public static void initData() { carUtil = new CarUtil(); carUtil.add(new Bus("奥迪A4", 500, 4)); carUtil.add(new Bus("马自达6", 400, 4)); carUtil.add(new MixCar("皮雪卡6", 450, 4, 2)); carUtil.add(new Bus("金龙", 800, 20)); carUtil.add(new CargoCar("松花江", 400, 4)); carUtil.add(new CargoCar("奔驰", 1000, 20)); } }
基本用上了第二季所有学的知识,静态方法,静态变量,接口,继承,多态,封装,toString, 除了抽象类,(其实接口应该也算调用了抽象类),仅供参考。
ziom
相关分类