哈哈~终于写出来答答打车系统啦,但会重复输出载客车和载货车,代码如下,希望交流!
不过遗憾的是无法将重复的载客车或载货车不重复输出,请问有大神可以解决吗,代码如下:
package Car; public class Car { private String name; private int money; private int passagerCapacity; private int cargoCapacity; public Car(String name,int money,int passagerCapacity,int cargoCapacity){ this.name=name; this.money=money; this.passagerCapacity=passagerCapacity; this.cargoCapacity=cargoCapacity; } public String getName(){ return name; } public int getMoney(){ return money; } public int getPassagerCapacity(){ return passagerCapacity; } public int getCargoCapacity(){ return cargoCapacity; } }
package Car; public class PickupCar extends Car{ public PickupCar(String name,int money,int passagerCapacity,int cargoCapacity){ super(name,money,passagerCapacity,cargoCapacity); } }
package Car; public class PickUpSnow6 extends Car{ public PickUpSnow6(String name,int money,int passagerCapacity,int cargoCapacity){ super(name,money,passagerCapacity,cargoCapacity); } }
package Car; import java.util.ArrayList; import java.util.Scanner; public class User { public void play() { System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车:1是0否"); Scanner input = new Scanner(System.in); int yesOrNo = input.nextInt(); if(yesOrNo == 1){ Car [] carList ={new PickupCar("AudiA4\t",500,4,0),new PickupCar("Mazda6\t",400,4,0),new PickUpSnow6("PickUpSnow6",450,4,2),new PickupCar("GoldenDragonBus",800,20,0),new Truck("SongHuaRiver",400,0,4),new Truck("Iveco\t",1000,0,20)}; System.out.println("您可租车的类型及其价目表:"); System.out.println("序号"+"\t"+"汽车名称"+ "\t\t"+ "租金(元/天)"+"\t"+"容量"); for(int i = 0;i<carList.length;i++){ System.out.println((i+1)+".\t"+carList[i].getName()+"\t"+carList[i].getMoney()+"\t"+"载人:"+carList[i].getPassagerCapacity()+"人,载货:"+carList[i].getCargoCapacity()+"吨"); } System.out.println("请输入您要的汽车的数量:"); int num = input.nextInt(),i = 0; ArrayList<Car> userList= new ArrayList<Car>(); int cost1=0,cost2=0; int cap1=0,cap2=0; String pickupCarName = ""; String truckName = ""; while (i < num) { System.out.println("请输入第" + (i + 1) + "车的序号:"); int choose = input.nextInt()-1; if(choose<0||choose>5){ System.out.println("输入错误,请再次输入序号:"); }else{ Car currentCar = carList[choose]; userList.add(currentCar); if(currentCar instanceof PickupCar){ pickupCarName += currentCar.getName()+" "; cost1 += currentCar.getMoney(); cap1 += currentCar.getPassagerCapacity(); i++; }else if(currentCar instanceof PickUpSnow6){ pickupCarName += currentCar.getName()+" "; truckName += currentCar.getName()+" "; cost1 += currentCar.getMoney(); cap1 += currentCar.getPassagerCapacity(); cap2 += currentCar.getCargoCapacity(); i++; }else{ truckName += currentCar.getName()+" "; cost2 += currentCar.getMoney(); cap2 += currentCar.getCargoCapacity(); i++; } } } System.out.println("请输入租车天数:"); int day = input.nextInt(); if(day <= 0){ System.out.println("输入有误,请输入租车天数:"); day = input.nextInt(); } System.out.println("您的账单:"); System.out.println("***可载人的车有:" + pickupCarName + "共载人:" + cap1 +"人"); System.out.println("***可载货的车有:" + truckName + "共载货:" + cap2 +"吨"); System.out.println("***租车总价格:" + day*(cost1 + cost2) +"元"); }else{ System.out.println("好的,已退出该系统"); } input.close(); } }
package Car; public class UserTest { public static void main(String[] args) { User u1 = new User(); u1.play(); } }