Java實現「汽車租賃項目」


wKioL1eMTWrwyVF6AAWrWC1-7f4676.png-wh_50

一、建立租車cab父類(抽象)java

package study;

//	建立抽象租車cab父類

public abstract class cab {
	
//	建立cab具備的公共屬性
	
	private String brand; //	車輛的品牌
	
	private String licencePlateNumber;	//	車輛的車牌號碼
	
	private double dayRent;	//	車輛的日租金

//	cab的空構造方法
	
	public cab() {
					
	}

/**cab的有參構造方法
 * @param brand
 * @param licencePlateNumber
 * @param dayRent
 */
	
public cab(String brand, String licencePlateNumber, double dayRent) {
	
	this.brand = brand;
	
	this.licencePlateNumber = licencePlateNumber;
	
	this.dayRent = dayRent;
	
	}

//建立cab的車輛租賃rent方法

public abstract void rent();

//	建立cab的計算租金的calcRent方法

public abstract void calcRent();

//	建立cab的get、set方法

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;
	
	}

}

二、建立汽車car子類繼承cab父類ide

package study;

import java.util.Scanner;

//	建立汽車car子類

public class car extends cab {
	
	Scanner input = new Scanner(System.in);
		
	int days = 0;//	定義客戶租賃的天數	
	
//	car的私有屬性
	
	private String type;
	
//	car的空構造方法
	
	public car() {
	
	}
	
	/** car的有參構造方法
	 * @param brand
	 * @param licencePlateNumber
	 * @param dayRent
	 */
	public car(String brand, String licencePlateNumber, double dayRent,String type) {
		
		super(brand, licencePlateNumber, dayRent);
		
		this.type = type ;
			
}
	
//  重寫cab的車輛租賃rent方法

	public  void rent(){
		
//		System.out.println("===========歡迎光臨汽車租賃系統=============="+"\n");//	用戶提示信息
		
		System.out.println("請輸入您要租賃的汽車品牌:1.寶馬\t2.別克");//	用戶選擇提示信息
		
//		獲取用戶輸入的數字(選項)1或者2
		
		int brand1 = input.nextInt();
		
//		判斷用戶輸入的數字
		
		switch (brand1){
				
			case 1://	第一種狀況爲寶馬
				
				super.setBrand("寶馬") ;
				
				System.out.println("請輸入您要租賃的汽車型號:1.X6\t2.550i");	//	提示客戶輸入信息
				
				int type1 = input.nextInt(); //	獲取用戶輸入信息存儲到type1變量中
			
				if(type1==1){
											
					this.type = "X6";
				
					super.setLicencePlateNumber("京NY28588") ;
				
					super.setDayRent(800);
				
				}else{
				
					type = "550i";
				
					super.setLicencePlateNumber("京CNY3284");
				
					super.setDayRent(600);
				
				}
				
				break;
		
			case 2://	第一種狀況爲別克
			
				super.setBrand("別克") ;
				
				System.out.println("請輸入您要租賃的汽車型號:1.林蔭大道\t2.GL8");	//	提示客戶輸入信息
								
					int type2 = input.nextInt();//	獲取用戶輸入信息存儲到type2變量中
					
					if(type2==1){
				
						this.type = "林蔭大道";
						
						super.setLicencePlateNumber("京NT37465");
															
						super.setDayRent(300);
				
					}else{
				
						this.type = "GL8";
						
						super.setLicencePlateNumber("京NT96968");
				
						super.setDayRent(600);
															
					}
			
					break;
			
			default:
						
	}	
			
}
	
//	重寫租金計算的方法
	
	public  void calcRent(){
		
//		double discount = 1.0;//	定義折扣變量並賦初始值爲1,即不打折扣
		
		System.out.println("請輸入您要租賃的天數:");//	提示用戶輸入租賃的天數
		
		days = input.nextInt();//	將獲取的天數存儲到days變量中
				
		if(days<=7){
			
			System.out.println("您成功的租賃了"+super.getBrand()+"\t"+type+" ;"+"車牌號爲:"+super.getLicencePlateNumber()+" "+";"+"您須要支付的金額爲:"+super.getDayRent()*(days)*1+"元人民幣。");
									
		}else if(7<days&&days<=30){
			
			System.out.println("您成功的租賃了"+super.getBrand()+"\t"+type+" ;"+"車牌號爲:"+super.getLicencePlateNumber()+" "+";"+"您須要支付的金額爲:"+super.getDayRent()*(days)*(0.9)+"元人民幣。");
			
		}else if(30<days&&days<=150){
			
			System.out.println("您成功的租賃了"+super.getBrand()+"\t"+type+" ;"+"車牌號爲:"+super.getLicencePlateNumber()+" "+";"+"您須要支付的金額爲:"+super.getDayRent()*(days)*(0.8)+"元人民幣。");
			
		}else{
			
			System.out.println("您成功的租賃了"+super.getBrand()+"\t"+type+" ;"+"車牌號爲:"+super.getLicencePlateNumber()+" "+";"+"您須要支付的金額爲:"+super.getDayRent()*(days)*(0.7)+"元人民幣。");
			
		}
		
	}
			
//	car的get、set方法
	
	public String getType() {
		
		return type;
			
	}

	public void setType(String type) {
		
		this.type = type;
		
	}

}

三、建立測試類一測試

package study;

//	建立測試類

public class Demo {

	public static void main(String[] args) {

		cab car = new car ();

		car.rent();

		car.calcRent();

	}

}

編譯結果一:this

===========歡迎光臨汽車租賃系統==============

請輸入您要租賃的汽車品牌:1.寶馬	2.別克
2
請輸入您要租賃的汽車類型:1.林蔭大道	2.GL8
1
請輸入您要租賃的天數:
1
您成功的租賃了別克	林蔭大道 ;車牌號爲:京NT37465 ;您須要支付的金額爲:300.0元人民幣。

四、增長一個bus子類spa

package study;

import java.util.Scanner;

//建立客車bus子類

public class bus extends cab {

	Scanner input = new Scanner(System.in);
	
	int days = 0;//	定義客戶租賃的天數	
	
//	bus的私有屬性
	
	private int seat;
	
//	bus的空構造方法
	
	public bus() {
	
	}
	
	/** bus的有參構造方法
	 * @param brand
	 * @param licencePlateNumber
	 * @param dayRent
	 */
	public bus(String brand, String licencePlateNumber, double dayRent,int seat) {
		
		super(brand, licencePlateNumber, dayRent);
		
		this.seat = seat ;
			
}
	
//  重寫bus的車輛租賃rent方法

	public  void rent(){
		
//		System.out.println("===========歡迎光臨汽車租賃系統=============="+"\n");//	用戶提示信息
		
		System.out.println("請輸入您要租賃的客車品牌:1.金盃\t2.金龍");//	用戶選擇提示信息
		
//		獲取用戶輸入的數字(選項)1或者2
		
		int brand1 = input.nextInt();
		
//		判斷用戶輸入的數字
		
		switch (brand1){
				
			case 1://	第一種狀況爲金盃
				
				super.setBrand("金盃") ;
				
				System.out.println("請輸入您要租賃的客車座位數:1.16\t2.34");	//	提示客戶輸入信息
				
				int seat1 = input.nextInt(); //	獲取用戶輸入信息存儲到type1變量中
			
				if(seat1==1){
											
					this.seat = 16;
				
					super.setLicencePlateNumber("京6566754") ;
				
					super.setDayRent(800);
				
				}else{
				
					this.seat = 34;
				
					super.setLicencePlateNumber("京9696996");
				
					super.setDayRent(1500);
				
				}
				
				break;
		
			case 2://	第一種狀況爲金龍
			
				super.setBrand("金龍") ;
				
				System.out.println("請輸入您要租賃的汽車型號:1.16\t2.34");	//	提示客戶輸入信息
								
					int seat2 = input.nextInt();//	獲取用戶輸入信息存儲到type2變量中
					
					if(seat2==1){
				
						this.seat = 16;
						
						super.setLicencePlateNumber("京8696997");
															
						super.setDayRent(300);
				
					}else{
				
						this.seat = 34;
						
						super.setLicencePlateNumber("京8696998");
				
						super.setDayRent(1500);
															
					}
			
					break;
			
			default:
						
	}	
			
}
	
//	重寫租金計算的方法
	
	public  void calcRent(){
		
//		double discount = 1.0;//	定義折扣變量並賦初始值爲1,即不打折扣
		
		System.out.println("請輸入您要租賃的天數:");//	提示用戶輸入租賃的天數
		
		days = input.nextInt();//	將獲取的天數存儲到days變量中
				
		if(days<=7){
			
			System.out.println("您成功的租賃了"+super.getBrand()+"\t"+seat+" ;"+"車牌號爲:"+super.getLicencePlateNumber()+" "+";"+"您須要支付的金額爲:"+super.getDayRent()*(days)*1+"元人民幣。");
									
		}else if(7<days&&days<=30){
			
			System.out.println("您成功的租賃了"+super.getBrand()+"\t"+seat+" ;"+"車牌號爲:"+super.getLicencePlateNumber()+" "+";"+"您須要支付的金額爲:"+super.getDayRent()*(days)*(0.9)+"元人民幣。");
			
		}else if(30<days&&days<=150){
			
			System.out.println("您成功的租賃了"+super.getBrand()+"\t"+seat+" ;"+"車牌號爲:"+super.getLicencePlateNumber()+" "+";"+"您須要支付的金額爲:"+super.getDayRent()*(days)*(0.8)+"元人民幣。");
			
		}else{
			
			System.out.println("您成功的租賃了"+super.getBrand()+"\t"+seat+" ;"+"車牌號爲:"+super.getLicencePlateNumber()+" "+";"+"您須要支付的金額爲:"+super.getDayRent()*(days)*(0.7)+"元人民幣。");
			
		}
		
	}
			
//	car的get、set方法
	
	public int getSeat() {
		
		return seat;
			
	}

	public void setSeat(int seat) {
		
		this.seat = seat;
		
	}

}

修改測試類orm

package study;

import java.util.Scanner;

//	建立測試類

public class Demo {

	public static void main(String[] args) {
		
		System.out.println("===========歡迎光臨汽車租賃系統=============="+"\n");
		
		System.out.println("請輸入您要租賃的車輛類型:1.汽車\t2.客車");
		
		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();
		}
			
	}

}

編譯結果二:繼承

===========歡迎光臨汽車租賃系統==============

請輸入您要租賃的車輛類型:1.汽車	2.客車
1
請輸入您要租賃的汽車品牌:1.寶馬	2.別克
1
請輸入您要租賃的汽車型號:1.X6	2.550i
1
請輸入您要租賃的天數:
23
您成功的租賃了寶馬X6 ;車牌號爲:京NY28588 ;您須要支付的金額爲:16560.0元人民幣。
相關文章
相關標籤/搜索