Trafalgar Law
2015-02-09 15:50
package com.DADAzuche; import java.util.ArrayList; import java.util.Scanner; public class DADArentcar { public static void main(String[] args) { Car[] car ={new Normalcar(1,"奥迪A4",500,4),new Normalcar(2,"马自达6",400,4) ,new Pickup(3,"皮卡",450,4,2),new Normalcar(4,"苏州金龙",800,50) ,new Lorry(5,"跃进",400,4),new Lorry(6,"依维柯",999,20)}; System.out.println("\t****欢迎您使用嗒嗒租车系统****"); System.out.println(); System.out.println("1.进入系统,2.退出系统"); Scanner in = new Scanner(System.in); int str = in.nextInt(); if(str==1) { System.out.println("\t****欢迎您进入嗒嗒租车系统****"); System.out.println(); System.out.println("租车信息列表如下:"); System.out.println("序号\t\t汽车名称\t\t租金\t\t容量:"); for(int i = 0;i<car.length;i++) { car[i].show(); } System.out.println(); System.out.println("请输入你要租的汽车数量:"); int number = in.nextInt(); System.out.println(); System.out.println("请输入你要租车的序号:"); System.out.println(); ArrayList<Car> al = new ArrayList<Car>(); for(int i = 0 ;i<number;i++) { System.out.println("请输入第"+(i+1)+"辆车的序号"); int num = in.nextInt(); for(int x=0;x<car.length;x++) { if(num==car[x].getID()) al.add(car[x]); } } System.out.println("请输入您租车的天数:"); int Rentday = in.nextInt(); System.out.println(); System.out.println("租车清单如下,请认真核对:"); System.out.println(); System.out.println("可载人的车:"); int Allprice = 0; int ALLBusload = 0; int ALLBoatload = 0; for(int i=0;i<al.size();i++) { if(al.get(i)instanceof Normalcar) { System.out.print(al.get(i).getCarname()+" "); Allprice+=al.get(i).getPrice(); ALLBusload+=((Normalcar) al.get(i)).getBusload(); } else if(al.get(i)instanceof Pickup) { System.out.print(al.get(i).getCarname()+" "); Allprice+=al.get(i).getPrice(); ALLBusload+=((Pickup) al.get(i)).getBusload(); } } System.out.println(" 总可载人数:"+ALLBusload+"人"); System.out.println(); System.out.println(); System.out.println("可载货的车:"); for(int i=0;i<al.size();i++) { if(al.get(i)instanceof Pickup) { System.out.print(al.get(i).getCarname()+" "); ALLBoatload+= ((Pickup) al.get(i)).getBoatload(); } else if(al.get(i)instanceof Lorry) { System.out.print(al.get(i).getCarname()+" "); ALLBoatload+= ((Lorry) al.get(i)).getBoatload(); Allprice+=al.get(i).getPrice(); } } System.out.println(" 总可载货物:"+ALLBoatload+"吨"); System.out.println(); System.out.println("一共需付的金额为:"+Allprice*Rentday); } else if(str==0) { System.exit(5); } in.close(); } } package com.DADAzuche; public abstract class Car { protected int ID; protected String Carname; protected int Price; public Car(int iD, String carname, int price) { super(); this.ID = iD; this.Carname = carname; this.Price = price; } public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getCarname() { return Carname; } public void setCarname(String carname) { Carname = carname; } public int getPrice() { return Price; } public void setPrice(int price) { Price = price; } public abstract void show(); } package com.DADAzuche; public class Normalcar extends Car { private int Busload; public Normalcar(int iD, String carname, int price, int busload) { super(iD, carname, price); Busload = busload; } public int getBusload() { return Busload; } public void setBusload(int busload) { Busload = busload; } @Override public void show() { System.out.println(ID+"\t\t"+Carname+"\t\t"+Price+"元/天"+"\t\t"+Busload+"人"); } } package com.DADAzuche; public class Lorry extends Car { private int Boatload; public Lorry(int iD, String carname, int price,int Boatload) { super(iD, carname, price); this.Boatload = Boatload; } public int getBoatload() { return Boatload; } public void setBoatload(int boatload) { Boatload = boatload; } @Override public void show() { System.out.println(ID+"\t\t"+Carname+"\t\t"+Price+"元/天"+"\t\t"+Boatload+"吨"); } } package com.DADAzuche; public class Pickup extends Car { private int Busload; private int Boatload; public Pickup(int iD, String carname, int price, int busload, int boatload) { super(iD, carname, price); this.Busload = busload; this.Boatload = boatload; } public int getBusload() { return Busload; } public void setBusload(int busload) { Busload = busload; } public int getBoatload() { return Boatload; } public void setBoatload(int boatload) { Boatload = boatload; } @Override public void show() { System.out.println(ID+"\t\t"+Carname+"\t\t"+Price+"元/天"+"\t\t"+Busload+"人"+","+Boatload+"吨"); } } /*************************************************************************/ package 嗒嗒租车系统; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; class CAR //小汽车,只可以多载人 { private String Carname; private int Price; private int Busload; public CAR(String carname, int price, int busload) { super(); Carname = carname; Price = price; Busload = busload; } public int getBusload() { return Busload; } public void setBusload(int busload) { Busload = busload; } public String getCarname() { return Carname; } public void setCarname(String carname) { Carname = carname; } public int getPrice() { return Price; } public void setPrice(int price) { Price = price; } } class Lorry //卡车只可以多载货 { private String name; private int Price; private int Boatload; public Lorry(String name, int price, int boatload) { super(); this.name = name; Price = price; Boatload = boatload; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return Price; } public void setPrice(int price) { Price = price; } public int getBoatload() { return Boatload; } public void setBoatload(int boatload) { Boatload = boatload; } } class Pickup //皮卡车,又可以载货又可以多载人 { private String name; private int Price; private int Busload; private int Boatload; public Pickup(String name, int price, int busload, int boatload) { super(); this.name = name; Price = price; Busload = busload; Boatload = boatload; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return Price; } public void setPrice(int price) { Price = price; } public int getBusload() { return Busload; } public void setBusload(int busload) { Busload = busload; } public int getBoatload() { return Boatload; } public void setBoatload(int boatload) { Boatload = boatload; } } public class DADAZUZUCHE { public static void main(String[] args) { System.out.println("欢迎使用租车系统:"); Scanner input = new Scanner(System.in); System.out.println("您是否要租车?1/0(是/不是)"); try { int select = input.nextInt(); if(select==1) { System.out.println("****欢迎进入租车系统****"); System.out.println("下面是您可以租车的类型和价目表"); CarType(); Zuchenumber(); } else System.out.println("欢迎下次光临!正在退出系统...."); System.exit(5); input.close(); } catch(RuntimeException e) { throw new RuntimeException("输入的数据 类型不正确!"); } } public static void Zuchenumber() { System.out.println(); System.out.println("请输入您要租车的数量:"); Scanner input = new Scanner(System.in); int num = input.nextInt(); zucheType(num); System.out.println(); input.close(); } public static void zucheType(int num) { ArrayList<Object> al = new ArrayList<Object>(); Scanner input = new Scanner(System.in); for(int i=0;i<num;i++) { System.out.println("请输入你要租的第"+(i+1)+"车的序号:"); int number = input.nextInt(); al.add(selectCar(number)); } Arrays.asList(al); ZuCheList(al); input.close(); } private static void ZuCheList(ArrayList<Object> al) { System.out.println("请输入要租车的天数"); Scanner input = new Scanner(System.in); int day = input.nextInt(); System.out.println(); System.out.println("租车清单为:"); System.out.println(); System.out.println("\t****可以载人的车有****"); int CARload = 0; int Pickupload = 0; int ALLprice=0; for(int i=0;i<al.size();i++) { if(al.get(i)instanceof CAR) { CAR a = (CAR) al.get(i); System.out.print(a.getCarname()+" "); CARload+=a.getBusload(); ALLprice+=a.getPrice(); } if(al.get(i)instanceof Pickup) { Pickup c = (Pickup) al.get(i); System.out.print(c.getName()+" "); Pickupload+=c.getBusload(); ALLprice+=c.getPrice(); } } System.out.println("总载人数:"+(CARload+Pickupload)+"人"); System.out.println(); System.out.println(); System.out.println("****可以载货的车有****"); int LorryBoatload=0; int PKBoatload=0; for(int i=0;i<al.size();i++) { if(al.get(i)instanceof Lorry) { Lorry b = (Lorry) al.get(i); System.out.print(b.getName()+" "); LorryBoatload+=b.getBoatload(); ALLprice+=b.getPrice(); } if(al.get(i)instanceof Pickup) { Pickup c = (Pickup) al.get(i); System.out.print(c.getName()+" "); PKBoatload+=c.getBoatload(); } } System.out.println("总载货量:"+(LorryBoatload+PKBoatload)+"吨"); System.out.println(); System.out.println("****租车总价格****"+(ALLprice*day)+"元"); input.close(); } public static Object selectCar(int number) { switch(number) { case 1:return new CAR("奥迪A4",500,4); case 2:return new CAR("马自达6",400,4); case 3:return new Pickup("皮卡车",450,4,20); case 4:return new CAR("苏州金龙",800,20); case 5:return new Lorry("跃进",400,4); case 6:return new Lorry("依维柯",1000,20); default:System.out.println("您输入的车号不存在!!"); } return null; } public static void CarType() { System.out.println("序号\t\t汽车名称\t\t租金\t\t容量:"); System.out.println(); System.out.println("1\t\t奥迪A4\t\t500元/天\t\t载人:4人"); System.out.println(); System.out.println("2\t\t马自达6\t\t400元/天\t\t载人:4人"); System.out.println(); System.out.println("3\t\t皮卡车\t\t450元/天\t\t载人:4人,载货:20吨"); System.out.println(); System.out.println("4\t\t苏州金龙\t\t800元/天\t\t载人:20人"); System.out.println(); System.out.println("5\t\t跃进\t\t400元/天\t\t载货:4吨"); System.out.println(); System.out.println("6\t\t依维柯\t\t1000元/天\t载货:20吨"); } }
Car[] car ={new Normalcar(1,"奥迪A4",500,4),new Normalcar(2,"马自达6",400,4)
,new Pickup(3,"皮卡",450,4,2),new Normalcar(4,"苏州金龙",800,50)
,new Lorry(5,"跃进",400,4),new Lorry(6,"依维柯",999,20)};
不明白这句?求解答
在eclipse 里有报错。。
都没讲到过ArrayList的用法啊
Java入门第二季 升级版
530649 学习 · 6091 问题
相似问题