一、Drive.java(汽車類 - 父類)
package Z_CarManger;
import java.io.ObjectInputStream.GetField;
public abstract class Drive {
private String brand; // 品牌 brand
private String id; // 車牌號
private int rent; // 日租金 rent
// =================================== 【設置構造方法】
public Drive() {}
public Drive(String brand,String id,int rent)
{
this.brand=brand;
this.id=id;
this.rent=rent;
}
// =================================== 【設置屬性】
public void setBrand(String brand) {
this.brand=brand; // 品牌 brand
}
public String getBrand() {
return this.brand; // 品牌 brand
}
public void setId(String id)
{
this.id=id; // 車牌號
}
public String getId() {
return this.id; // 車牌號
}
public void setRent(int rent) {
this.rent=rent; // 日租金 rent
}
public int getRent() {
return rent; // 日租金 rent
}
// 計算租金,傳入天數,計算租金。 (抽象方法,要聲明抽像類)
public abstract float Money(int days);
}
二、Car.java(轎車類 - 子類)
package Z_CarManger;
//===== 【轎車類】
public class Car extends Drive{
private String type;
// =================================== 【設置構造方法】
public Car() {}
public Car(String brand,String id,int rent, String type)
{
super(brand,id,rent); // 引用父類的構造方法
this.type=type;
}
// =================================== 【設置屬性】
public void setType(String type) {
this.type=type;
}
public String getType()
{
return this.type;
}
// 計算租金,傳入天數,計算租金。 (抽象方法,要聲明抽像類)
public float Money(int days) {
float price=this.getRent();
if(days>7)
{
price=price*0.9f;
}
if(days>30)
{
price=price*0.8f;
}
if(days>150)
{
price=price*0.7f;
}
return price;
}
}
三、Bus.java(客車類 - 子類)
package Z_CarManger;
import org.omg.CORBA.PRIVATE_MEMBER;
// ===== 【客車類】
public class Bus extends Drive{
private int seat;
// =================================== 【設置構造方法】
public Bus() {};
public Bus(String brand,String id,int rent,int seat) {
super(brand,id,rent); // 引用父類的構造方法
this.seat=seat;
}
// -------- 【設置屬性 - 座位】
public void setSeat(int seat)
{
this.seat=seat;
}
public int getSeat()
{
return this.seat;
}
// 計算租金,傳入天數,計算租金。 (抽象方法,要聲明抽像類)
public float Money(int days) {
float price=this.getRent();
if(days>3)
{
price=price*0.9f;
}
if(days>7)
{
price=price*0.8f;
}
if(days>30)
{
price=price*0.7f;
}
if(days>150)
{
price=price*0.6f;
}
return price;
}
}
四、Data1.java(數據類 - 存放需要比對和處理的對象數組)
package Z_CarManger;
public class Data1 {
Drive[] m_car=new Drive[8]; // 通過父類創建對象數組
public int init(String brand,String id,int days,String type,int seat)
{
// System.out.println("品牌:"+brand+" 牌照號:"+id+" 天數:" +days+" 車型:"+type +" 座位:"+seat);
// 【轎車】:品牌、車輛號、日租金、型號
m_car[0]=new Car("寶馬","京N28588",800,"X6"); // 對應Car類的構造函數
m_car[1]=new Car("寶馬","京CNY3284",600,"550i"); // 對應Car類的構造函數
m_car[2]=new Car("別克","京NT37465",300,"林蔭大道"); // 對應Car類的構造函數
m_car[3]=new Car("別克","京NT96968",600,"GL8"); // 對應Car類的構造函數
// 【客車】:品牌、車輛號、日租金、型號
m_car[4]=new Bus("金盃","京6566754",800,16); // 對應Bus類的構造函數
m_car[5]=new Bus("金龍","京8696997",800,16); // 對應Bus類的構造函數
m_car[6]=new Bus("金盃","京9696996",1500,34); // 對應Bus類的構造函數
m_car[7]=new Bus("金龍","京8696998",1500,34); // 對應Bus類的構造函數
// String brand="寶馬";
// String id="京N28588";
// int days=3;
// String type="X6";
int money=0;
// ====================================== 【正確方法,遍歷對象數組】
Drive m_meney=null; // 創建一個對象變量
for(Drive moto:m_car) // 用父類Drive建立一個新的數組對象moto,進行遍歷和比較
{
if(moto instanceof Car) // 遍歷Car子類的對象
{
moto=(Car)moto;
if(brand.equals(moto.getBrand()) && id.equals(moto.getId()) && type.equals(((Car)moto).getType()))
{
money=days*(moto.getRent());
System.out.println("應該交費:"+money);
}
}
if(moto instanceof Bus) // 遍歷Bus子類的對象
{
moto=(Bus)moto;
if(brand.equals(moto.getBrand()) && id.equals(moto.getId()) && seat==(((Bus)moto).getSeat()))
{
money=days*(moto.getRent());
System.out.println("應該交費:"+money);
}
}
}
return money;
}
}
五、App.java(主程序 main)
package Z_CarManger;
import java.util.Scanner;
import org.omg.CORBA.OMGVMCID;
import Book.database;
// ========= 【主程序】
public class App {
public static void main(String[] args)
{
int m_car=1; // 轎車或客車
String m_brand=""; // 品牌
String m_id=""; // 牌照號
int m_seat=0; // 座位數
int m_rent=0; // 日租金
String m_type=""; // 車型
int m_days=0; // 天數
System.out.println("--------------- 騰飛汽車租賃管理系統 ---------------");
System.out.println(" 1、轎車 2、客車");
System.out.println(" 請選擇汽車類型:");
Scanner input=new Scanner(System.in);
int m_input = input.nextInt();
m_car=m_input;
switch(m_car) {
case 1: // 轎車
System.out.print("請繼續選擇品牌:1.寶馬 2、別克");
m_input = input.nextInt();
if(m_input==1)
{
m_brand="寶馬";
System.out.print("請繼續選擇車型:1.X6 2、550i");
m_type=(input.nextInt()==1)?"X6":"550i";
}else
{
m_brand="別克";
System.out.print("請繼續選擇車型:1.林蔭大道 2、GL8");
m_type=(input.nextInt()==1)?"林蔭大道":"GL8";
}
break;
case 2: // 客車
System.out.print("請繼續選擇品牌:1.金盃 2、金龍");
m_brand=(input.nextInt()==1)?"金盃":"金龍";
System.out.print("請繼續選擇座位:1.16座 2、34座");
m_seat=Integer.valueOf((input.nextInt()==1)?"16":"34");
break;
}
System.out.print("輸入租賃天數:");
m_days=input.nextInt();
System.out.print("輸入牌照號:");
m_id=input.next();
Data1 uuu = new Data1();
uuu.init(m_brand,m_id,m_days,m_type,m_seat);
}
}