public abstract class Auto { private String carName; private String carColor; private int carAge; private float carPrice; public Auto() { super(); } public Auto(String carName, String carColor, int carAge, float carPrice) { super(); this.carName = carName; this.carColor = carColor; this.carAge = carAge; this.carPrice = carPrice; } public abstract float moneyCount(int day); public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public String getCarColor() { return carColor; } public void setCarColor(String carColor) { this.carColor = carColor; } public int getCarAge() { return carAge; } public void setCarAge(int carAge) { this.carAge = carAge; } public float getCarPrice() { return carPrice; } public void setCarPrice(float carPrice) { this.carPrice = carPrice; } @Override public String toString() { return carName + "\t" + carColor + "\t" + carAge + "\t" + carPrice ; } }
public class Car extends Auto{ public Car() { super(); // TODO Auto-generated constructor stub } public Car(String carName, String carColor, int carAge, float carPrice) { super(carName, carColor, carAge, carPrice); // TODO Auto-generated constructor stub } @Override public float moneyCount(int day) { // TODO Auto-generated method stub return day * this.getCarPrice(); } }
public class Trcuk extends Auto{ private int t; public Trcuk() { super(); // TODO Auto-generated constructor stub } public Trcuk(String carName, String carColor, int carAge, float carPrice, int t) { super(carName, carColor, carAge, carPrice); this.t = t; } @Override public float moneyCount(int day) { return day * this.getCarPrice()*t; } public int getT() { return t; } public void setT(int t) { this.t = t; } }
java.util.Scanner Import; public class Manager { // store existing car garage Private Auto [] = {INCAR new new Car ( "Benz", "black", 2, 800.f), new new Car ( "BMW" "black", 2, 800.f), new new Car ( "Ferrari", "red", 2, 1800.f), new new Car ( "Porsche", "Bose", 2, 2800.f), new new Car ( "Lamborghini", "yellow", 2, 3800.f), null, null, null, null, null }; // store car has been rented out Private Auto [] = {outCar new new car ( "Wuling Hongguang" "black", 2, 200.f), new new Car ( "BYD", "black", 2, 400.f), new new Car ( "Harvard", "red", 2, 600.f), null, null , null, null, null, null, null, }; The INPUT = new new Scanner Scanner (System.in); public void shouMainMenu(){ System.out.println ( "Welcome Shangxue Tang car rental system"); System.out.println ( "========================== ====== "); System.out.println (". 1, 2 car, the car 3 and car storage 4, exit "); int input.nextInt In Flag = (); Switch (In Flag) { Case. 1 : getCar (); shouMainMenu (); BREAK; Case 2: backCar (); shouMainMenu (); BREAK; Case. 3: newCar (); shouMainMenu (); BREAK; default: System.out.println ( "end of program") ; BREAK; } } / ** * Car * / public void getCar () { showAutoArray (INCAR); System.out.println ( "Please enter the rented vehicle request have number"); int input.nextInt NO = (); // Get the user to ask for a vehicle leased object INCAR = Auto Auto [-NO. 1]; INCAR [-NO. 1] = null; // Add the object is removed from the array to incar outCar array to // the object is removed from the INCAR array for (int i NO. 1-=; I <-inCar.length. 1; I ++) { INCAR [I] INCAR = [I +. 1]; } INCAR [-inCar.length. 1] = null; // add to the object array outCar in int index = 0; for (int I = 0; I <outCar.length; I ++) { IF (outCar [I] == null) { index = I; BREAK; } } outCar [index] = Auto; showAutoArray ( INCAR); } / ** * the car * / public void backCar () { showAutoArray (outCar); System.out.println ( "Please enter further vehicle number"); int index = input.nextInt (); days System.out.println ( "Please enter the rented "); int Day input.nextInt = (); Auto Auto = outCar [-index. 1]; System.out.println (" total rent "+ auto.moneyCount (Day)); // the user further vehicle remove from outCar, and added to the inCar outCar [index] = null; for (int I = index; I <-inCar.length. 1; I ++) { outCar [I] = outCar [I +. 1]; } outCar [ . 1-outCar.length] = null; // the user to change back into the car INCAR array int n-= 0; for (int I = 0; I <inCar.length; I ++) { Auto INCAR AT = [I ]; IF (AT == null) { n-= I; BREAK; } } INCAR [n-] = Auto; // also show Write vehicle list showAutoArray (outCar); } / ** * car storage * / Public void newCar () { System.out.println ( "Please choose the type of vehicle 1, 2 cars, trucks"); int of the type = input.nextInt (); Auto Auto = null; System.out.println ( " Please enter the license plate number "); String = carid input.next (); System.out.println (" Please enter a car type "); String carname = input.next (); System.out.println (" Please enter the vehicle color "); String carColor = input.next (); System.out.println (" Please enter the age of the vehicle "); int carAge = input.nextInt (); System.out.println (" Please enter the daily rent "); float = input.nextFloat carPrice (); IF (type ==. 1) { Auto = Car new new (carname, carColor, carAge, carPrice); } the else { System.out.println ( "Please enter the tonnage truck"); int t = input.nextInt(); auto = new Trcuk(carName, carColor, carAge, carPrice, t); } int index = 0; for (int i = 0; i < inCar.length; i++) { Auto a = inCar[i]; if (a == null) { index = i; break; } } inCar [index] = auto; } public void showAutoArray(Auto [] auto) { System.out.println("序号\t车名\t颜色\t使用年限\t日租金\t卡车载重"); for (int i = 0; i < auto.length; i++) { if (auto[i] == null) { break; } System.out.print(i+1+"\t"); System.out.println(auto[i]); } } }
public class Test { public static void main(String[] args) { Manager manager = new Manager(); manager.shouMainMenu(); } }