- 項目:汽車租賃系統
- 項目簡介:開發一個汽車租賃系統,根據客戶要求的汽車型號、乘客人數、用途等需求匹配合適的車輛並計算租賃價位等功能
- 開發工具:eclipse
- 語言:Java
- 知識點:類的繼承、構造函數繼承、抽象方法、重寫方法、多態
背景介紹
“跑得快”汽車租賃公司出租多種車輛,車型及租金情況如下:
需求
編寫程序實現租車流程並計算租賃價格
面向對象設計步驟
類的關係圖
文件劃分說明:兩個包,六個.java文件
- RentMgrSys.java:程序入口->>main()函數
- MotoOperation.java:汽車業務類
- MotoVehicle.java:汽車抽象類
- Car.java:轎車類
- Bus.java:客車類
- Truck.java:卡車類
程序執行效果圖
以下只給出源碼框架,需要查看完整代碼的可於微信搜索公衆號“鑰道不止”或“suoyue_zhan”或下方直接掃碼關注並在後臺回覆“524056”即可獲取完整代碼
源碼框架
RentMgrSys.java
package com.demo;
import java.util.Scanner;
import com.cars.MotoVehicle; //導入另一個包的文件
public class RentMgrSys {
private static MotoVehicle motor; //汽車
private static double rentCost; //租賃費用
public static MotoVehicle getMotor() {
return motor;
}
public static void setMotor(MotoVehicle motor) {
RentMgrSys.motor = motor;
}
public static double getRentCost() {
return rentCost;
}
public static void setRentCost(double rentCost) {
RentMgrSys.rentCost = rentCost;
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
MotoVehicle moto=null; //汽車
System.out.println("***********歡迎光臨跑得快汽車租賃公司***********");
System.out.println("1.轎車\t2.客車\t3.卡車");
System.out.print("請選擇你要租賃的汽車類型:");
int choose=input.nextInt();
System.out.print("請輸入您要租賃的天數:");
int days=input.nextInt();
rentManage(choose, days, 100, 100, 100, 100.0); // vehicleType, brandId, seatCount,ton設置默認爲100
moto = RentMgrSys.getMotor(); // 方便測試修改重新取得moto對象
double money = RentMgrSys.getRentCost(); // 方便測試取得租車費用信息money
System.out.println("\n=====================================");
System.out.println("分配給您的汽車牌號是:"+moto.getVehicleId());
System.out.println("您需要支付的租賃費用是:"+money+"元。");
input.close();
}
/**
* 租車管理
* @param type
* @param days
* @param vehicleType
* @param brandId
* @param seatCount
* @param ton
*/
public static void rentManage(int type, int days, int vehicleType, int brandId, int seatCount, double ton) {
}
}
MotoOperation.java
package com.demo;
import com.cars.MotoVehicle;
import com.cars.Bus;
import com.cars.Car;
import com.cars.Truck;
/**
* 汽車業務
*/
public class MotoOperation {
/**
* 租賃汽車
* @param motoType 汽車類型
* @return 汽車
*/
public MotoVehicle motoLeaseOut(String motoType){
return motorRentCommon(motoType, 0, 0, 0, 0);
}
/**
* 租賃汽車
* @param motoType 汽車類型
* @param brandId 品牌
* @param typeId 型號
* @param seatCount 座位數
* @param ton 噸數
* @return 汽車
*/
public MotoVehicle motorRentCommon(String motoType, int brandId, int typeId, int seatCount, double ton){
MotoVehicle moto=null;
return moto; //返回一個汽車對象
}
}
MotoVehicle.java
package com.cars;
/**
*汽車的抽象類
*/
public abstract class MotoVehicle {
private String vehicleId; //車牌號
private String brand; //品牌
private int perRent; //日租金
//定義兩個抽象方法
public abstract float calRent(int days); //計算租金
public abstract void leaseOutFlow(); //租車流程
/**
* 構造函數
*/
public MotoVehicle() {
}
public MotoVehicle(String vehicleId,String brand,int perRent) {
this.vehicleId = vehicleId;
this.brand = brand;
this.perRent=perRent;
}
public String getVehicleId() {
return vehicleId;
}
public void setVehicleId(String vehicleId) {
this.vehicleId = vehicleId;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPerRent() {
return perRent;
}
public void setPerRent(int perRent) {
this.perRent = perRent;
}
}
Car.java
package com.cars;
import java.util.Scanner;
/**
* 轎車
*/
public class Car extends MotoVehicle{
private String type; // 型號
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Car() {
}
public Car(String vehicleId, String brand, String type, int perRent) {
super(vehicleId, brand, perRent);
this.type = type;
}
/**
* 重寫計算租金
*/
public float calRent(int days) {
return this.getPerRent() * days;
}
/**
* 重寫租車流程
*/
public void leaseOutFlow() {
}
public void rentVehicleFlow(int brandId, int typeId) {
}
}
Bus.java
package com.cars;
import java.util.Scanner;
/**
* 客車
*/
public class Bus extends MotoVehicle{
private int seatCount; // 座位數
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public Bus() {
}
public Bus(String vehicleId, String brand, int perRent, int seatCount) {
super(vehicleId, brand, perRent);
this.seatCount = seatCount;
}
/**
* 重寫計算租金
*/
public float calRent(int days) {
return this.getPerRent() * days;
}
/**
* 重寫租車流程
*/
public void leaseOutFlow() {
}
public void rentBusFlow(int busBrandId, int seatCount) {
}
}
Truck.java
package com.cars;
import java.util.Scanner;
/**
* 卡車
*/
public class Truck extends MotoVehicle{
private double ton; //噸
//設置和取得車輛載重信息即噸的信息
public double getTon() {
return ton;
}
public void setTon(double ton) {
this.ton = ton;
}
// TODO: 重載構造方法,利用構造方法的參數,給成員變量賦值
public Truck() {
}
public Truck(String vehicleId, String brand, double ton, int perRent) {
super(vehicleId, brand, perRent);
this.ton = ton;
}
// TODO: 完成構造方法
/**
* 重寫計算租金
*
*/
public float calRent(int days) {
return this.getPerRent() * days * (float)this.getTon();
}
/**
* 重寫租車流程
*/
public void leaseOutFlow() {
Scanner input=new Scanner(System.in);
System.out.println("1.解放\t2.東風");
System.out.print("請選擇你要租賃的卡車品牌:");
int truckBrandId = input.nextInt(); // TODO 輸入品牌:truckBrandId
System.out.print("請輸入卡車的噸數:");
ton = input.nextDouble();// TODO 輸入噸數
// 爲了測試用例使用,調整實現邏輯
rentVehicleFlow(truckBrandId, ton);
input.close();
}
/**
* 重寫租車流程
* @param brandId 品牌
* @param ton 噸數
*/
public void rentVehicleFlow(int brandId, double ton) {
//根據品牌:brandId,噸數:ton設置車輛的信息
if (brandId == 1) {
this.setBrand("解放");
this.setTon(ton); // 設置卡車噸數
this.setPerRent(800);
this.setVehicleId("京GD56577"); // 設置車牌號
} else if (brandId == 2) {
this.setBrand("東風");
this.setTon(ton); // 設置卡車噸數
this.setPerRent(700);
this.setVehicleId("京GD53456"); // 設置車牌號
} else
System.out.println("對不起,暫時沒有此品牌的客車"); // 客車
}
}