Car.java
Package COM.IMOOC;
Using object-oriented encapsulation features
public class Car {
Description car may have characteristics
private String name; The name of the car
Private double cargocapacity;//car's cargo capacity
private int busload;//capacity of car
private int dailyrent;//car's daily rent
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public double getcargocapacity () {
return cargocapacity;
}
public void setcargocapacity (double cargocapacity) {
this.cargocapacity = cargocapacity;
}
public int getbusload () {
return busload;
}
public void setbusload (int busload) {
This.busload = busload;
}
public int getdailyrent () {
return dailyrent;
}
public void setdailyrent (int dailyrent) {
This.dailyrent = dailyrent;
}
}
Passengercar.java
Package COM.IMOOC;
Using object-oriented inheritance attributes
public class Passengercar extends Car {
private String name; The name of the car
private int busload;//capacity of car
private int dailyrent;//car's daily rent
Construction method of passenger car
Public Passengercar (String name, int busload, int dailyrent) {
THIS.name = name;
This.busload = busload;
This.dailyrent = dailyrent;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getbusload () {
return busload;
}
public void setbusload (int busload) {
This.busload = busload;
}
public int getdailyrent () {
return dailyrent;
}
public void setdailyrent (int dailyrent) {
This.dailyrent = dailyrent;
}
}
Pickup.java
Package COM.IMOOC;
public class Pickup extends Car {//can carry passengers and manned
private String name; The name of the car
Private double cargocapacity;//car's cargo capacity
private int busload;//capacity of car
private int dailyrent;//car's daily rent
constructor function
Public Pickup (String name, double cargocapacity, int busload, int dailyrent) {
THIS.name = name;
this.cargocapacity = cargocapacity;
This.busload = busload;
This.dailyrent = dailyrent;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public double getcargocapacity () {
return cargocapacity;
}
public void setcargocapacity (double cargocapacity) {
this.cargocapacity = cargocapacity;
}
public int getbusload () {
return busload;
}
public void setbusload (int busload) {
This.busload = busload;
}
public int getdailyrent () {
return dailyrent;
}
public void setdailyrent (int dailyrent) {
This.dailyrent = dailyrent;
}
}
Test.java
Package COM.IMOOC;
Import Java.util.Scanner;
public class Test {
public static void Main (string[] args) {
System.out.println ("Welcome to use the car rental system!") ");
System.out.println ("Do you need a car rental: 1 is 0 no");
Scanner input = new Scanner (system.in);
int is = Input.nextint ();
Create an array of car classes to hold a variety of cars, using reference polymorphism to create different sub-class objects
Car[] Cars = {New Passengercar ("Audi", 5, 500),
New Truck ("Minivan", 5, Max), new Passengercar ("Mercedes", 5, 700),
New Pickup ("pickup", 1.5, 4, $), new Truck ("Big Truck", 10, 600),
New Passengercar ("Bus", 25, 650)};
if (is = = 1) {
int Totalmoney = 0;
Double totalcargocapacity = 0;
int totalbusload = 0;
int rentcardays = 0;
int Totaldailymoney = 0;
System.out.println ("The type of car rental you can and its price list:");
SYSTEM.OUT.PRINTLN ("Serial number \ T" + "car name \t\t" + "Day rent \t\t" + "capacity \ T");
int num = 1;//defines the initial sequence number
for (Car car:cars) {
if (car instanceof Passengercar) {
System.out.println ("No." + num + ' t ' + car.getname ()
+ "\t\t" + car.getdailyrent () + "Yuan/day \t\t" + "Manned:"
+ car.getbusload () + "person");
num++;
}
if (car instanceof Truck) {
System.out.println ("No." + num + ' t ' + car.getname ()
+ "\t\t" + car.getdailyrent () + "Yuan/day \t\t" + "laden:"
+ car.getcargocapacity () + "ton");
num++;
}
if (car instanceof Pickup) {
System.out.println ("No." + num + ' t ' + car.getname ()
+ "\t\t" + car.getdailyrent () + "Yuan/day \t\t" + "Manned:"
+ car.getbusload () + "people" + "laden:"
+ car.getcargocapacity () + "ton");
num++;
}
}
System.out.println ("Please enter the number of your rental car (maximum number of cars is 6):");
Scanner sc = new Scanner (system.in);
int carnum = Sc.nextint ();
if (Carnum > 0 && carnum <= 6) {
int[] Rentcar = new Int[carnum];
for (int i = 0; i < Carnum; i++) {
System.out.println ("Please enter" + (i + 1) + "Vehicle serial number:");
Scanner SC2 = new Scanner (system.in);
int rentcarnum = Sc2.nextint ();
Rentcar[i] = Rentcarnum; Save the entered rental number
Totaldailymoney + = Cars[rentcarnum-1].getdailyrent ();
Totalcargocapacity + = Cars[rentcarnum-1]
. getcargocapacity ();
Totalbusload + = Cars[rentcarnum-1].getbusload ();
}
System.out.println ("Please enter the number of days to rent a car:");
Scanner sc3 = new Scanner (system.in);
Rentcardays = Sc3.nextint ();
Totalmoney = Totaldailymoney * rentcardays;
Manned bill
SYSTEM.OUT.PRINTLN ("--------Manned vehicle--------:");
for (int i = 0; i < rentcar.length; i++) {
if (Cars[rentcar[i]-1] instanceof Passengercar
|| Cars[rentcar[i]-1] instanceof Pickup) {
System.out.print (Cars[rentcar[i]-1].getname () + ' \ t ');
}
}
System.out.println ();
Cargo bill
System.out.println ("--------laden vehicle--------:");
for (int i = 0; i < rentcar.length; i++) {
if (Cars[rentcar[i]-1] instanceof Truck
|| Cars[rentcar[i]-1] instanceof Pickup) {
System.out.print (Cars[rentcar[i]-1].getname () + ' \ t ');
}
}
System.out.println ();
SYSTEM.OUT.PRINTLN ("Total laden:" + totalcargocapacity+ "\ t total manned:" + totalbusload+ "\ t Total Price:" + Totalmoney);
} else {
System.out.println ("Please revise the number of rental cars!") ");
}
} else {
SYSTEM.OUT.PRINTLN ("Thank you for your visit to answer the car rental system!") ");
}
}
}
Operation Result:
Welcome to use the car rental system!
Do you need a car rental: 1 Yes 0 no
1
The type of rental car you can hire and its price list:
Number of car name day rental capacity
Audi 500 Yuan/day manned: 5 people
No.2 pickup truck 150 yuan/day cargo: 5.0 tons
No.3 Benz 700 yuan/day manned: 5 people
No.4 pickup 200 yuan/day manned: 4 people laden: 1.5 tons
No.5 Large Goods vehicle 600 yuan/day cargo: 10.0 tons
No.6 bus 650 yuan/day manned: 25 people
Please enter your rental number (maximum number of cars is 6):
3
Please enter the number of the 1th car:
1
Please enter the number of the 2nd car:
2
Please enter the number of the 3rd car:
4
Please enter the number of days to rent:
2
--------Manned vehicle--------:
Audi Pickup
----------------of a cargo vehicle:
Pickup truck
Total Cargo: 6.5 Total manned: 9 Total Price: 1700
A simple application of inheritance, encapsulation, and polymorphism (answer the car rental system)