汽車租賃管理系統及所涉及面向物件的一般步驟
阿新 • • 發佈:2019-02-15
一.面向物件設計的步驟
1.分析需求
2.找名詞(類和屬性)
父類:
汽車:品牌、日租金、車牌號
子類:
轎車:型號
客車:座位數
汽車業務類
汽車租賃管理類
3.找動詞(方法)
汽車(計算租金)
汽車業務類(初始化汽車,提供租賃服務)
汽車租賃管理類(入口和系統介面)
4.優化設計
父子類關係:汽車(父)-->轎車、客車(子類)
汽車(抽象類):計算租金-->抽象方法
5.梳理執行過程
汽車業務類(MoteOperation):
初始化-->構造車(轎車,客車)
-->構造這些車怎麼儲存?
-->物件陣列(MotoVehicle[])
租車 :
根據使用者的租車條件去查詢相應的車輛,並返回相應的車輛,使用者的租車條件為:方法的引數
迴圈遍歷汽車陣列(Motovehicle[])
拿到每一輛車
將車轉換為自己實際的子類型別 instanceof
分別根據使用者的不用條件進行比較
car:brand、type
bus:brand、seatCount
發現符合使用者條件的相應車輛,返回
二.最後進行測試和除錯
三.總結
/** * 汽車父類 */ public abstract class MotoVehicle { // 品牌、日租金、車牌號 private String brand; private String vehicleId; private int perRent; // 構造方法 public MotoVehicle() { } public MotoVehicle(String brand, String vehicleId, int perRent) { this.brand = brand; this.vehicleId = vehicleId; this.perRent = perRent; } // set/get方法 public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getVehicleId() { return vehicleId; } public void setVehicleId(String vehicleId) { this.vehicleId = vehicleId; } public int getPerRent() { return perRent; } public void setPerRent(int perRent) { this.perRent = perRent; } // 抽象的計算租金的方法(根據租車的天數) public abstract float calcRent(int days); }
/** * 轎車子類 */ public class Car extends MotoVehicle { private String type;// 型號 public Car() { } public Car(String brand, String vehicleId, int perRent, String type) { super(brand, vehicleId, perRent); this.type = type; } public String getType() { return type; } public void setType(String type) { this.type = type; } // 重寫父類計算租金的方法 public float calcRent(int days) { float price = this.getPerRent() * days; if (days >= 150) { price = price * 0.7f; } else if (days >= 30) { price = price * 0.8f; } else if (days >= 7) { price = price * 0.9f; } return price; } }
/**
* 客車子類
*/
public class Bus extends MotoVehicle {
// 座位數
private int seatCount;
public Bus() {
}
public Bus(String brand, String vehicleId, int perRent, int seatCount) {
super(brand, vehicleId, perRent);
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
// 重寫計算租金
public float calcRent(int days) {
float price = this.getPerRent() * days;
if (days >= 150) {
price = price * 0.6f;
} else if (days >= 30) {
price = price * 0.7f;
} else if (days >= 7) {
price = price * 0.8f;
} else if (days >= 3) {
price = price * 0.9f;
}
return price;
}
}
/**
* 汽車業務類
*/
public class MotoOperation {
MotoVehicle[] motos = new MotoVehicle[8];
// 初始化汽車
public void init() {
// 轎車
// String brand, String vehicleId, int perRent,String type
motos[0] = new Car("寶馬", "京N28588", 800, "X6");
motos[1] = new Car("寶馬", "京N26566", 600, "550i");
motos[2] = new Car("別克", "京L98588", 300, "林蔭大道");
motos[3] = new Car("別克", "京L96566", 600, "GL8");
// 客車
// String brand, String vehicleId, int perRent,int seatCount
motos[4] = new Bus("金龍", "京M18288", 800, 16);
motos[5] = new Bus("金龍", "京M16266", 1500, 34);
motos[6] = new Bus("金盃", "京H28388", 800, 16);
motos[7] = new Bus("金盃", "京H26366", 1500, 34);
}
// 提供租賃服務(根據條件查詢相應車輛並返回)
public MotoVehicle rentVehicle(String brand, String type, int seatCount) {
MotoVehicle rentMoto = null;
for (MotoVehicle moto : motos) {
if (moto instanceof Car) {
Car car = (Car) moto;
if (brand.equals(moto.getBrand()) && type.equals(car.getType())) {
rentMoto = car;
break;
}
} else if (moto instanceof Bus) {
Bus bus = (Bus) moto;
if (brand.equals(moto.getBrand()) && bus.getSeatCount() == seatCount) {
rentMoto = bus;
break;
}
}
}
return rentMoto;
}
/*
* public static void main(String[] args) { MotoOperation motoOpr=new
* MotoOperation(); motoOpr.init(); MotoVehicle
* moto=motoOpr.rentVehicle("寶馬", "X6", 0); int days=5;
* System.out.println(moto.getBrand());
* System.out.println("租金"+moto.getPerRent()*days);
* System.out.println(moto.getVehicleId());
* System.out.println(((Car)moto).getType()); }
*/
}
import java.util.Scanner;
/**
* 汽車租賃管理類(入口和系統介面)
*/
public class RentVehicle {
public static void main(String[] args) {
System.out.println("**********歡迎光臨騰飛汽車租賃公司**********");
System.out.print("請選擇租車型別:(1.轎車 2.客車)");
Scanner input = new Scanner(System.in);
int choose = input.nextInt();
String brand = "";
String type = "";
int seatCount = 0;
switch (choose) {
case 1:
// 租賃轎車
System.out.print("請選擇品牌:(1、寶馬 2、別克)");
int brandChoose = input.nextInt();
if (brandChoose == 1) {
brand = "寶馬";
System.out.print("請選擇型別:(1、X6 2、550i)");
/*
* int typeChoose=input.nextInt();
* type=(typeChoose==1)?"X6":"550i"
*/;
type = (input.nextInt() == 1) ? "X6" : "550i";
} else {
brand = "別克";
System.out.print("請選擇型別:(1.林蔭大道 2.GL8)");
/* int typeChoose=input.nextInt(); */
type = (input.nextInt() == 1) ? "林蔭大道" : "GL8";
}
break;
case 2:
// 租賃客車
System.out.print("請選擇品牌:(1、金龍 2、金盃)");
brand = (input.nextInt() == 1) ? "金龍" : "金盃";
System.out.print("請選擇座位數:(1、16 2、34)");
seatCount = (input.nextInt() == 1) ? 16 : 34;
break;
}
// 租車
System.out.print("請輸入租賃天數:");
int days = input.nextInt();
MotoOperation motoOpr = new MotoOperation();
motoOpr.init();
MotoVehicle moto = motoOpr.rentVehicle(brand, type, seatCount);
// 提示租車資訊給使用者
System.out.println("租車成功!您租的車是:" + moto.getVehicleId());
System.out.print("您應付的租金是(元):" + moto.calcRent(days));
}
}