汽車租賃系統(tǒng)
author:luckyboy!
version:1.1
知識儲備:變量、數(shù)據(jù)類型、選擇結(jié)構(gòu)、循環(huán)結(jié)構(gòu)、數(shù)組 、面向?qū)ο?/p>
系統(tǒng)概述:某汽車租賃公司出租多種轎車和客車,出租費用以日為單位計算。出租車型及信息如下表所示。
車型 | 具體信息 | 日租金 | 折扣 |
轎車 | 寶馬X6(京NY28588) | 800 | days>7天9折 days>30天8折 days>150天7折 |
寶馬550i(京CNY3284) | 600 | ||
別克林蔭大道(京NT37465) | 300 | ||
別克GL8(京NT96968) | 600 | ||
客車 | 金杯、16座(京6566754) | 800 | days>=3天9折 days>=7天8折 days>=30天7折 days>=150天6折 |
金龍、16座(京8696997) | |||
金杯、34座(京9696996) | 1500 | ||
金龍、34座(京8696998) |
面向?qū)ο笤O(shè)計步驟
????????首先讀懂項目需求,分析項目需求找出隱藏在其中的名詞和動詞,這些名詞可能是所能用到的類和屬性,動詞可能是需要用到的方法。根據(jù)已知的類、屬性、方法進一步優(yōu)化設(shè)計,最后梳理項目運行過程。
?需求中的名詞
? ? ? ? 汽車租賃公司、汽車、轎車、客車、別克、寶馬、金杯、金龍、X6、550i、GL8、林蔭大道、座位數(shù)、日租金、折扣、車牌號(京NY28588、京CNY3284、京NT37465、京NT96968、京6566754、京6566754、京9696996、京8696998)
類和類屬性
????????根據(jù)已知名詞找出需要使用的類和類屬性
????????汽車類:車牌號、車的品牌、日租金
????????客車類:車牌號、車的品牌、日租金、座位數(shù)
????????轎車類:車牌號、車的品牌、日租金、車的型號
????????汽車業(yè)務(wù)類:
????????汽車租賃管理類(測試類)
分析:
- 客車和轎車都屬于汽車,汽車是客車和汽車的父類,汽車和客車是汽車的子類。
- 客車和汽車都具有相同的屬性(車牌號、車的品牌、日租金);那么客車類和轎車類可以繼承汽車類的屬性。
- 除去相同的屬性還具有私有的屬性,客車類具有座位數(shù),轎車類具有車的型號。
- 還應(yīng)有一個汽車業(yè)務(wù)類來完成汽車租賃功能。
- 汽車租賃管理類用來對汽車租賃系統(tǒng)進行測試。
需求中的動詞
? ? ? ? 計算租金、租賃、程序入口是類中所需大方法。
優(yōu)化設(shè)計
?完成優(yōu)化設(shè)計代碼:
創(chuàng)建 .Java文件:MotoVehicle類(汽車類)、Car類(轎車類)、Bus類(客車類)、MotoOperation類(汽車業(yè)務(wù)類)、Test類(測試類)
階段劃分
- 第一階段:創(chuàng)建并完成汽車類
- 第二階段:創(chuàng)建并完成轎車類
- 第三階段:創(chuàng)建并完成客車類
- 第四階段:創(chuàng)建并完成汽車業(yè)務(wù)類
- 第五階段:創(chuàng)建并完成測試類
第一階段:創(chuàng)建并完成汽車類
父類MotoVehicle
//汽車類
public abstract class MotoVehicle {
//車牌號 品牌 日租金
private String vehicleId;
private String brand;
private int perRent;
//無參構(gòu)造方法
public MotoVehicle() {
}
//有參構(gòu)造方法
public MotoVehicle(String vehicleId, String brand, int perRent) {
this.vehicleId = vehicleId;
this.brand = brand;
this.perRent = perRent;
}
//get和set方法
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;
}
//計算租金(抽象方法)
public abstract double calcRent(int days);
}
?第二階段:創(chuàng)建并完成轎車類?
子類Car繼承父類MotoVehicle
//轎車類
public class Car extends MotoVehicle {
// 型號
private String type;
// 無參構(gòu)造方法
public Car() {
super();
}
// 有參構(gòu)造方法
public Car(String vehicleId, String brand, int perRent, String type) {
super(vehicleId, brand, perRent);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double calcRent(int days) {
double price = this.getPerRent() * days;
if (days > 7 && days <= 30) {
price = price * 0.9;
} else if (days > 30 && days <= 150) {
price = price * 0.8;
} else if (days > 150) {
price = price * 0.7;
}
return price;
}
}
第三階段:創(chuàng)建并完成客車類?
子類Bus繼承父類MotoVehicle
//客車類
public class Bus extends MotoVehicle {
// 座位數(shù)
private int seatCount;
// 無參構(gòu)造方法
public Bus() {
super();
}
// 有參構(gòu)造方法
public Bus(String vehicleId, String brand, int perRent, int seatCount) {
super(vehicleId, brand, perRent);
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
@Override
public double calcRent(int days) {
double price = this.getPerRent() * days;
if (days >= 3 && days < 7) {
price = price * 0.9;
} else if (days >= 7 && days < 30) {
price = price * 0.8;
} else if (days >= 30 && days < 150) {
price = price * 0.7;
} else if (days >= 150) {
price = price * 0.6;
}
return price;
}
}
第四階段:創(chuàng)建并完成汽車業(yè)務(wù)類
MotoOperation類
import java.util.ArrayList;
import java.util.Scanner;
//汽車業(yè)務(wù)類
public class MotoOperation {
Scanner in = new Scanner(System.in);
// 創(chuàng)建轎車類數(shù)組
Car[] car = new Car[4];
// 創(chuàng)建客車類
Bus[] bus = new Bus[4];
public void inti() {
// 將轎車信息保存到轎車類數(shù)組
car[0] = new Car("京NY28588", "寶馬", 800, "X6");
car[1] = new Car("京CNY28588", "寶馬", 600, "550i");
car[2] = new Car("京NT37465", "別克", 300, "林蔭大道");
car[3] = new Car("京NT96968", "別克", 600, "GL8");
// 將客車信息保存到客車類數(shù)組
bus[0] = new Bus("京6566754", "金杯", 800, 16);
bus[1] = new Bus("京9696996", "金杯", 1500, 34);
bus[2] = new Bus("京8696997", "金龍", 800, 16);
bus[3] = new Bus("京8696998", "金龍", 1500, 34);
}
// 汽車租賃方法
public void start() {
System.out.println("**********歡迎光臨租賃公司**********");
System.out.println("1-轎車\t2-客車");
System.out.println("請選擇您要租賃的汽車類型:");
int type = in.nextInt();
// 如果輸入1和2以外的數(shù)字,則重新輸入
while (type < 1 || type > 2) {
System.out.println("輸入錯誤,請重新選擇您要租賃的汽車類型:");
type = in.nextInt();
}
if (type == 1) {// 用戶選擇租轎車
// 租轎車的方法
car();
} else if (type == 2) {// 用戶選擇租客車
// 租客車的方法
bus();
}
}
// 轎車租賃方法
public void car() {
String brand = "";
String type = "";
System.out.println("請選擇您要租賃的轎車品牌:1-別克\t2-寶馬");
int chooseBrand = in.nextInt();
// 如果輸入1和2以外的數(shù)字,則重新輸入
while (chooseBrand < 1 || chooseBrand > 2) {
System.out.println("輸入錯誤,請重新選擇您要租賃的轎車品牌:");
chooseBrand = in.nextInt();
}
// 用戶選擇別克
if (chooseBrand == 1) {
// 轎車品牌賦值為別克
brand = "別克";
System.out.println("請選擇您要租賃的汽車型號:1-林蔭大道\t2-GL8");
int chooseType = in.nextInt();
// 如果輸入1和2以外的數(shù)字,則重新輸入
while (chooseType < 1 || chooseType > 2) {
System.out.println("輸入錯誤,請重新選擇您要租賃的轎車型號:");
chooseType = in.nextInt();
}
// 如果用戶選擇別克,則給轎車型號賦值為林蔭大道;如果用戶選擇別克,則給轎車型號賦值為GL8;
type = chooseType == 1 ? "林蔭大道" : "GL8";
}
// 用戶選擇寶馬
else if (chooseBrand == 2) {
// 轎車品牌賦值為寶馬
brand = "寶馬";
System.out.println("請選擇您要租賃的汽車類型:1-X6\t2-50i");
int chooseType = in.nextInt();
// 如果輸入1和2以外的數(shù)字,則重新輸入
while (chooseType < 1 || chooseType > 2) {
System.out.println("輸入錯誤,請重新選擇您要租賃的轎車型號:");
chooseType = in.nextInt();
}
// 如果用戶選擇寶馬,則給轎車型號賦值為X6;如果用戶選擇寶馬,則給轎車型號賦值為550i;
type = chooseType == 1 ? "X6" : "550i";
}
// 輸出提車信息
for (int i = 0; i < car.length; i++) {
if (car[i].getBrand().equals(brand)
&& car[i].getType().equals(type)) {
// 計算轎車租金
System.out.println("請輸入您要租的天數(shù):");
int days = in.nextInt();
double sumPrice = calcRent(days, type);
System.out.println("租車成功,請按照如下車牌號提車:" + car[i].getVehicleId());
System.out.println("您需要支付:" + sumPrice + "元");
}
}
}
// 計算轎車租金的方法
public double calcRent(int days, String type) {
double price = 0;
for (int i = 0; i < car.length; i++) {
if (car[i].getType().equals(type)) {
price = car[i].getPerRent() * days;
if (days > 7 && days <= 30) {
price = price * 0.9;
} else if (days > 30 && days <= 150) {
price = price * 0.8;
} else if (days > 150) {
price = price * 0.7;
}
}
}
return price;
}
// 客車租賃方法
public void bus() {
String brand = "";
int count = 0;
System.out.println("請選擇您要租賃的客車品牌:1-金杯\t2-金龍");
int chooseBrand = in.nextInt();
// 如果用戶輸入1和2以外的數(shù)子,則重新輸入
while (chooseBrand < 1 && chooseBrand > 2) {
System.out.println("輸入錯誤,請重新選擇您要租賃的轎車品牌:");
chooseBrand = in.nextInt();
}
// 如果用戶選擇金杯,則給客車品牌賦值為金杯;如果用戶選擇金龍,則給客車品牌賦值為金龍;
brand = chooseBrand == 1 ? "金杯" : "金龍";
System.out.println("請選擇您要租賃的客車座位數(shù):1-16座\t2-34座");
int chooseCount = in.nextInt();
// 如果用戶輸入1和2以外的數(shù)子,則重新輸入
while (chooseCount < 1 && chooseCount > 2) {
System.out.println("輸入錯誤,請重新選擇您要租賃的客車座位數(shù):1-16座\t2-34座");
chooseCount = in.nextInt();
}
// 如果用戶選擇16座,則給客車座位數(shù)賦值為16;如果用戶選擇34座,則給客車座位數(shù)賦值為34;
count = chooseCount == 1 ? 16 : 34;
// 輸出提車信息
for (int i = 0; i < bus.length; i++) {
if (bus[i].getBrand().equals(brand) && bus[i].getSeatCount() == count) {
// 計算轎車租金
System.out.println("請輸入您要租的天數(shù):");
int days = in.nextInt();
double sumPrice = calcRent(days, count);
System.out.println("租車成功,請按照如下車牌號提車:" + bus[i].getVehicleId());
System.out.println("您需要支付:" + sumPrice + "元");
}
}
}
// 計算客車租金的方法
public double calcRent(int days, int count) {
double price = 0;
for (int i = 0; i < bus.length; i++) {
if (bus[i].getSeatCount() == count) {
price = bus[i].getPerRent() * days;
if (days > 7 && days <= 30) {
price = price * 0.9;
} else if (days > 30 && days <= 150) {
price = price * 0.8;
} else if (days > 150) {
price = price * 0.7;
}
}
}
return price;
}
}
第五階段:創(chuàng)建并完成測試類文章來源:http://www.zghlxwxcb.cn/news/detail-409994.html
Test類文章來源地址http://www.zghlxwxcb.cn/news/detail-409994.html
//汽車租賃管理類:測試類
public class Text {
public static void main(String[] args) {
//實例化汽車業(yè)務(wù)類
MotoOperation moto = new MotoOperation();
//調(diào)用汽車業(yè)務(wù)類中的初始化汽車信息方法
moto.inti();
//調(diào)用汽車業(yè)務(wù)類中的汽車租賃方法
moto.start();
}
}
到了這里,關(guān)于Java汽車租賃系統(tǒng)1.2-面向?qū)ο?數(shù)組的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!