650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/84/4C/wKioL1eMTWrwyVF6AAWrWC1-7f4676.png-wh_500x0-wm_3 -wmp_4-s_994674232.png "title=" Car Rental Project "alt=" Wkiol1emtwrwyvf6aawrwc1-7f4676.png-wh_50 "/>
1. Create a car rental cab parent class (abstract)
package study;//Create abstract car Rental cab Parent class public abstract class cab {//Create a public property with a cab private string brand; //vehicle's brand private string licenceplatenumber;//vehicle's license plate number private double dayrent;//of the day rent of vehicles//cab method of Public cab () {}/**cab method of construction * @param brand * @ param licenceplatenumber * @param dayrent */public cab (String brand, String licenceplatenumber, double dayrent) {this.brand = brand; This.licenceplatenumber = licenceplatenumber;this.dayrent = dayrent;} Create Cab Car Rental rent method public abstract void rent ();//Create Cab's Calcrent method for calculating rents public abstract Void calcrent ();//Create a Cab get, set method Public string getbrand () {return brand;} Public void setbrand (String brand) {this.brand = brand;} Public string getlicenceplatenumber () {return licenceplatenumber;} PubliC void setlicenceplatenumber (String licenceplatenumber) {this.licenceplatenumber = licenceplatenumber;} Public double getdayrent () {return dayrent;} Public void setdayrent (double dayrent) {this.dayrent = dayrent;}}
2, create auto car subclass inherit cab parent class
package study;import java.util.scanner;//Create auto Car sub-class Public class car extends cab {scanner input = new scanner (system.in); int days = 0;//defines the number of days the customer leases// The private property of car Private string type;//car method of Public car () {}/** car the method of constructing the * @ param brand * @param licencePlateNumber * @param dayrent */public car (String brand, string licenceplatenumber, double dayrent,string type) {super (brand, licenceplatenumber, dayrent); this.type = type ;} rewrite cab car rental rent Method Public void rent () {//system.out.println ("=========== Welcome to Car rental system = = ============ "+" \ n ");//user prompt information System.out.println (" Please enter the car brand you want to rent: 1. BMW \t2 Buick ");//user selects the prompt message//Gets the user input number (option) 1 or 2int brand1 = input.nextint ();//Determine the number of user input switch (brand1) {case 1://The first case is BMW Super.setbrand ( "BMW") ; System.out.println ("Please enter the type of car you want to rent: 1. X6\t2.550i ");//Prompts the customer to enter information Int type1 = input.nextint (); //Gets the user input information stored in the TYPE1 variable if (type1==1) { this.type = "X6"; Super.setlicenceplatenumber ("Jing NY28588") ;super.setdayrent (800);} else{type = "550i"; Super.setlicenceplatenumber ("Jing CNY3284"); super.setdayrent (600);} break;case 2://first case for Buick Super.setbrand ("Buick") ; System.out.println ("Please enter the car model you want to rent: 1. Boulevard \t2. GL8 ");//prompt customer input information int type2 = input.nextint ();//Get user input information stored in type2 variable if (type2==1) {this.type = "Boulevard"; Super.setlicenceplatenumber ("Jing NT37465"); super.setdayrent (300);} else{this.type = "GL8"; Super.setlicenceplatenumber ("Jing NT96968"); super.setdayrent (600);} break;default:}}//rewriting Rent calculation Method Public void calcrent () {//double discount = 1.0;// Define the discount variable and assign an initial value of 1, i.e. without compromising System.out.println ("Please enter the number of days you want to lease:");//Prompt user to enter the number of days to lease Days = input.nextint ();/ Store the acquired days into the day variable if (days<=7) {System.out.println ("You have successfully leased" +super.getbrand () + "\ T" +type+ " ;" + "license plate number:" + Super.getliceNceplatenumber () + " " + ";" + "The amount you need to pay is:" +super.getdayrent () * (days) *1+ "Yuan. ");} Else if (7<days&&days<=30) {System.out.println ("You have successfully leased" +super.getbrand () + "\ T" +type+ " ;" + "License plate number:" +super.getlicenceplatenumber () + " " + ";" + "You need to pay the amount for:" +super.getdayrent () * (days) * (0.9) + "Yuan Yuan. ");} Else if (30<days&&days<=150) {System.out.println ("You have successfully leased" +super.getbrand () + "\ T" +type+ " The "+" license plate number is: "+super.getlicenceplatenumber () +" "+"; "+" You need to pay the amount for: "+super.getdayrent () * (days) * (0.8) +" Yuan Yuan. ");} ELSE{SYSTEM.OUT.PRINTLN ("You have successfully leased" +super.getbrand () + "\ T" +type+ " ;" + "license plate number:" +super.getlicenceplatenumber () + " "+"; "+" The amount you need to pay is: "+super.getdayrent () * (days) * (0.7) +" RMB. ");}} Car's get, set Method Public string gettype () {return type;} Public void settype (String type) {this.type = type;}}
3. Create test class One
Package study;//Create test class public class Demo {public static void main (string[] args) {Cab car = new car (); Car.rent (); car.calcre NT ();}}
Compile result one:
=========== Welcome to Car Rental System ============== Please enter the car brand you want to rent: 1. BMW 2. Buick 2 Please enter the type of car you want to rent: 1. Boulevard 2.gl81 Please enter the number of days you want to rent: 1 You successfully rented the Buick Boulevard The license plate number is: Beijing NT37465; The amount you need to pay is: 300.0 yuan.
4, add a bus sub-class
package study;import java.util.scanner;//creating bus bus sub-class Public class bus extends cab {scanner input = new scanner (system.in); int days = 0;//defines the number of days the customer leases// The empty construction method of bus's private property Private int seat;//bus Public bus () {}/** bus method * @param brand * @param licencePlateNumber * @param dayrent */public bus ( String brand, string licenceplatenumber, double dayrent,int seat) {super ( brand, licenceplatenumber, dayrent); this.seat = seat ;} rewrite bus car rental rent Method Public void rent () {//system.out.println ("=========== Welcome to Car rental system = = ============ "+" \ n ");//user prompt information System.out.println (" Please enter the passenger car brand you want to rent: 1. Golden cup \t2 ");//user selects the prompt message//Gets the user input number (option) 1 or 2int brand1 = input.nextint ();//Determine the number of user input switch (brand1) {case 1://The first case is the Golden Cup Super.setbrand ( "Golden Cup") ; System.out.println ("Please enter the number of seats you want to rent: 1.16\t2.34 ");//prompt customer input information int seat1 = input.nextint (); //Gets the user input information stored in the TYPE1 variable if (seat1==1) {this.seat = 16;super.setlicenceplatenumber ("Jing 6566754") ;super.setdayrent (800);} Else{this.seat = 34;super.setlicenceplatenumber ("Jing 9696996"); super.setdayrent (1500);} break;case 2://first case for Jinlong super.setbrand ("Jinlong") ; System.out.println ("Please enter the car model you want to rent: 1.16\t2.34");//prompt customer input information int seat2 = input.nextint ();// Gets the user input information stored in the type2 variable if (seat2==1) {this.seat = 16;super.setlicenceplatenumber ("Jing 8696997"); Super.setdayrent (300);} Else{this.seat = 34;super.setlicenceplatenumber ("Jing 8696998"); super.setdayrent (1500);} break;default:}}//rewriting Rent calculation Method Public void calcrent () {//double discount = 1.0;// Define the discount variable and assign an initial value of 1, i.e. without compromising System.out.println ("Please enter the number of days you want to lease:");//Prompt user to enter the number of days to lease Days = input.nextint ();/ Store the acquired days into the day variable if (days<=7) {System.out.println ("You have successfully leased" +super.getbrand () + "\ T" +seat+ " ;" + "license plate number:" + Super.getlicenceplatenumber ()+ " " + ";" + "The amount you need to pay is:" +super.getdayrent () * (days) *1+ "Yuan. ");} Else if (7<days&&days<=30) {System.out.println ("You have successfully leased" +super.getbrand () + "\ T" +seat+ " ;" + "License plate number:" +super.getlicenceplatenumber () + " " + ";" + "You need to pay the amount for:" +super.getdayrent () * (days) * (0.9) + "Yuan Yuan. ");} Else if (30<days&&days<=150) {System.out.println ("You have successfully leased" +super.getbrand () + "\ T" +seat+ " The "+" license plate number is: "+super.getlicenceplatenumber () +" "+"; "+" You need to pay the amount for: "+super.getdayrent () * (days) * (0.8) +" Yuan Yuan. ");} ELSE{SYSTEM.OUT.PRINTLN ("You have successfully leased" +super.getbrand () + "\ T" +seat+ " ;" + "license plate number:" +super.getlicenceplatenumber () + " "+"; "+" The amount you need to pay is: "+super.getdayrent () * (days) * (0.7) +" RMB. ");}} Car's get, set Method Public int getseat () {return seat;} Public void setseat (Int seat) {this.seat = seat;}}
Modifying a test class
Package Study;import java.util.scanner;//Create test class public class Demo {public static void main (string[] args) { SYSTEM.OUT.PRINTLN ("=========== Welcome to the car rental system ==============" + "\ n"); System.out.println ("Please enter the type of vehicle you wish to rent: 1. Car \t2. Bus"); Scanner input = new Scanner (system.in); int type = Input.nextint (); if (type==1) {cab car = new car (); Car.rent (); car.calcrent ();} Else{cab bus = new bus (); bus.rent (); Bus.calcrent ();}}
Compilation result Two:
=========== Welcome to Car rental system ============== Please enter the type of vehicle you wish to rent: 1. Car 2. Bus 1 Please enter the car brand you want to rent: 1. BMW 2. Buick 1 Please enter the number of cars you want to rent: 1.x62.550i1 Please enter the days you want to rent: 23 You successfully leased the BMW X6; license plate number: Beijing NY28588 The amount you need to pay is: 16560.0 yuan.
This article is from the "Advanced Technology" blog, so be sure to keep this source http://vipnoon.blog.51cto.com/7589908/1827335
Java implementation of "Car Rental Project"