For the convenience of understanding, drew a UML diagram, you can refer to for the convenience of understanding, drew a UML diagram, you can refer to
Define car Type: manned + laden
public class Car {int id; String name;int seat;double load;double price;public Car (int id,string name,int seat,double load,double price) {this.id = i D;this.name = Name;this.seat = Seat;this.load = Load;this.price = Price;} Public String toString () {return id+ "\ t" +name+ "\ T" +seat+ "block \ T" +load+ "ton \ T" +price+ "Yuan";}}
Define Loadcar inherit car, override construction method (laden, then seat =0)
public class Loadcar extends Car {public Loadcar (int. id,string name,double load,double Price) {Super (Id,name,0,load,price );} Public String toString () {return id+ "\ t" +name+ "\ T" +seat+ "\ T" +load+ "\ T" +price;}
' Define Seatcar inherit car, override construction method (manned, then laden =0)
public class Seatcar extends Car {public Seatcar (int. id,string name,int seat,double Price) {super (id,name,seat,0,price);} Public String toString () {return id+ "\ t" +name+ "\ T" +seat+ "\ T" +load+ "\ T" +price;}
Implementation (primarily business processes):
import Java.util.Scanner;; public class Initail {public static void main (string[] args) {//TODO auto-generated method StubSystem.out.println ("Do you want to rent a car ? 1, yes; 2, no "); Scanner in = new Scanner (system.in), if (In.nextint () ==1) {car[] cars = new Car[5];cars[0]=new Seatcar (1, "Audi A8", 4,400); Cars[1]=new Seatcar (2, "Tesla", 4,500); Cars[2]=new Car (3, "Pikachu", 4,2,200); cars[3]=new Loadcar (4, "Songhua River", 10,200); cars[4]= New Loadcar (5, "Heilongjiang", 20,300); System.out.println ("You can choose from 5 models available:"); SYSTEM.OUT.PRINTLN ("Serial number" + "T" + "model" + "T" + "seat" + "T" + "load (ton)" + "\ T" + "Rent/day"); for (car c:cars) {System.out.println (c);} int totalseat = 0;double Totalload = 0;double totalprice = 0; String loadcarlist = ""; String seatcarlist = ""; System.out.println ("Please enter the quantity you want to rent"); int rentnum = In.nextint (); for (int i=0;i<rentnum;i++) {System.out.println (" You want to select the number of "+ (i+1) +" cars), int rentid = In.nextint (), if (rentid<5) {totalseat = totalseat + cars[rentid-1].seat; Totalload = totalload + Cars[rentid-1].load;totalprice = totalprice + cars[rentid-1].price;if (cars[rentId-1]. seat!=0) {seatcarlist = Seatcarlist + cars[rentid-1] + "\ n";} else if (cars[rentid-1].load!=0) {loadcarlist = Loadcarlist + cars[rentid-1] + "\ n";}} Else{system.out.println ("Sorry, please select an existing vehicle (Serial No. 1-5)");}} System.out.println ("Please enter the number of days you want to rent a Car"); int rentdays = In.nextint (); System.out.println ("/********************** Below is your rental Bill **********************/"), if (seatcarlist== "") { SYSTEM.OUT.PRINTLN ("No manned vehicle"); ELSE{SYSTEM.OUT.PRINTLN ("Manned vehicles have \ n" +seatcarlist); SYSTEM.OUT.PRINTLN ("Total number of seats" +totalseat);} if (loadcarlist== "") {System.out.println ("No truck to be loaded");} ELSE{SYSTEM.OUT.PRINTLN ("Manned vehicles have \ n" +seatcarlist); SYSTEM.OUT.PRINTLN ("Total cargo capacity" +totalload);} SYSTEM.OUT.PRINTLN ("Total Price:" +totalprice*rentdays+ "Yuan");} ELSE{SYSTEM.OUT.PRINTLN ("Thank you for your use");}}}
Mu class net Java introductory second season 6.1 car rental system, use to inherit, rewrite, polymorphic, tostring function