1, the parent class
package Rent; //交通工具类 public class Vehicle { private int id; private String name; private int rentPrice; public Vehicle(int id,String name,int rentPrice) { this.id=id; this.name=name; this.rentPrice=rentPrice; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getRentPrice() { return rentPrice; } public void setRentPrice(int rentPrice) { this.rentPrice = rentPrice; } }
2, Passenger Van class
package Rent; //载客车 public class Bus extends Vehicle{ private int peopleNum; public Bus(int id,String name,int rentPrice,int peopleNum){ super(id,name,rentPrice); this.peopleNum=peopleNum; } public void setPeopleNum(int peopleNum) { this.peopleNum = peopleNum; } public int getPeopleNum() { return peopleNum; } public String toString () { // Returns a string, the string will be returned by this targeting System.out.println (Bus) when printing objects instantiated class; return Super .getId () + "\ T" + Super .getName () + "\ T" + Super .getRentPrice () + "/ day \ t" + peopleNum + "people" ; } }
3, trucks category
package rents; // 载货车 public class trunk extends Vehicle { private int cargoNum; public Trunk ( int id, String name, int rentPrice, int cargoNum) { super (ie, name rentPrice); this .cargoNum = cargoNum; } Public int getCargoNum () { return cargoNum; } Public void setCargoNum ( int cargoNum) { this .cargoNum = cargoNum; } public String toString() { return super.getId()+"\t"+super.getName()+"\t"+super.getRentPrice()+"元/天\t"+cargoNum+"吨"; } }
4, manned truck class
package Rent; //载人载货车 public class Bustrunk extends Vehicle{ private int peopleNum; private int cargoNum; public Bustrunk(int id,String name,int rentPrice,int peopleNum,int cargoNum){ super(id,name,rentPrice); this.peopleNum=peopleNum; this.cargoNum=cargoNum; } public void setPeopleNum(int peopleNum) { this.peopleNum = peopleNum; } public int getPeopleNum() { return peopleNum; } public void setCargoNum(int cargoNum) { this.cargoNum = cargoNum; } public int getCargoNum() { return cargoNum; } public String toString() { return super.getId()+"\t"+super.getName()+"\t"+super.getRentPrice()+"元/天\t"+peopleNum+"人"+cargoNum+"吨"; } }
5, the test class
Package Rent; Import Classes in java.util *. ; public class the Test { public static void main (String [] args) { Bus Bus = new new Bus (. 1, "Audi", 500,4 ); Trunk Trunk = new new Trunk (2, "east", 300,2 ); Bustrunk btrunk = new new Bustrunk (. 3, "Rover", 600,5,1 ); Vehicle Vehicle [] = {Bus, Trunk, btrunk}; Scanner SC = new new Scanner (the System.in ); System.out.println ( "Welcome to the Car rental system" ); System.out.println ( "no. 1 Car 2 Car" ); int n-= sc.nextInt (); Switch (n-) { Case . 1 : { System.out.println ( "ID \ t" + "car names \ t "+" price \ T "+" capacity " ); for ( int I = 0; I <vehicle.length; I ++ ) { System.out.println (Vehicle [I]); } System.out.println ( " Please enter the number of rental car: " ); int Total = sc.nextInt (); int TotalPrice = 0 ; int total people = 0; int totalcargo=0; int p=0; int c=0; Vehicle people[]=new Vehicle[total]; Vehicle cargo[]=new Vehicle[total]; for (int i = 1; i <=total; i++) { int busPeople=0; int trunkCargo=0; int btrunkPeople=0; int btrunkCargo=0; int= 0. price ; System.out.println ( "Please enter" + i + "car number" ); int ID = ; sc.nextInt () System.out.println ( "Please enter the number of days leased:" ); int Day = sc.nextInt (); . price = Vehicle [-ID. 1] .getRentPrice () * Day; TotalPrice + = . price; IF (Vehicle [-ID. 1] the instanceof Bus) { busPeople = bus.getPeopleNum (); people [P ++] = Vehicle [-ID. 1]; } else if(vehicle[id-1] instanceof Trunk){ trunkCargo=trunk.getCargoNum(); cargo[c++]=vehicle[id-1]; } else{ btrunkCargo=btrunk.getCargoNum(); btrunkPeople=btrunk.getPeopleNum(); people[p++]=vehicle[id-1]; cargo[c++]=vehicle[id-1]; } totalpeople+=busPeople+btrunkPeople; totalcargo+=trunkCargo+btrunkCargo; } System.out.println(""); System.out.println("***************您的账单*******************"); System.out.print("载人的车:"); for (int i = 0; i < p; i++) { System.out.print(people[i].getName()+" "); } System.out.println(""); System.out.print("载货的车:"); breakfor( Int I = 0; I <C; I ++ ) { of System.out.print (Cargo [I] .getName () + "" ); } System.out.println ( "" ); System.out.println ( " the total number of people can carry is: "+ totalpeople +" person \ t "+" total cargo: "+ totalcargo +" t " ); System.out.println ( " total amount: "+ totalprice +" yuan " ); BREAK ; } Case 2 : System.out.println ( "Thank you for using the system" ); System.exit ( 0 ); :; Default System.out.println ( "input error" ); System.exit ( 0 ); BREAK ; } } }
6, operating results