有一些冗餘程式碼, 只是實現了功能

dunne21發表於2021-09-09

package com.dada;

public class Cars {
//定義子類應該有的屬性
public String carName; //車名
public int id, carryMan; //編號, 載客量
public float unitPrice, carryGoods; //每日價格, 載貨量
}

package com.dada;

public class Bus extends Cars{
//建立有參構造方法
public Bus(int id, String carName, float unitPrice, int carryMan ) {
this.carName = carName;
this.id = id;
this.carryMan = carryMan;
this.unitPrice = unitPrice;
}
}

package com.dada;

public class Truck extends Cars {
public Truck( int id, String carName, float unitPrice, float carryGoods ){
this.carName = carName;
this.id = id;
this.unitPrice = unitPrice;
this.carryGoods = carryGoods;
}
}

package com.dada;

public class PickUp extends Cars {
public PickUp( int id, String carName, float unitPrice, int carryMan, float carryGoods ){
this.carName = carName;
this.id = id;
this.carryMan = carryMan;
this.unitPrice = unitPrice;
this.carryGoods = carryGoods;
}
}

package com.dada;
import java.util.Scanner;

public class Users {
static Cars[] cars;
static int[] carsId;
static int days;
static float allMoney;
static int allPersons;
static float allGoods;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("歡迎進入噠噠租車系統:n 是否要租車:1 是 0 否");
Scanner input = new Scanner(System.in);
int entry = input.nextInt();
if(entry == 1) {
//羅列車目型別
Users use = new Users();
System.out.println("您可租車的型別及價目表:n 編號t 車名t 單價tt 容量");
use.getCars();
System.out.println("請選擇要租車的數量:");
int numbers = input.nextInt();
//呼叫租車數量
use.carsNum(numbers);
System.out.println("輸入要租的天數: ");
days = input.nextInt();
//呼叫列印結果
use.display();
}

}
//車目型別
public void getCars(){
    Bus audi = new Bus(1, "奧迪a4", 500f, 5);
    Bus mzd = new Bus(2, "馬自達6", 400f, 5);
    Truck shj = new Truck(3, "松花江", 400f, 4);
    Truck ywk = new Truck(4, "依維柯", 1000f, 20);
    PickUp mq = new PickUp(5, "猛禽", 1200f, 5, 5);
    PickUp cc = new PickUp(6, "長城", 600f, 5, 3);
    Cars cars[] = {audi, mzd, shj, ywk, mq, cc};
    for(Cars car:cars){
        if(car.getClass() == Bus.class) {
            System.out.println(car.id + "t" + car.carName + "t" + car.unitPrice + "/天tt" + car.carryMan + "人");
        } else if(car.getClass() == Truck.class) {
            System.out.println(car.id + "t" + car.carName + "t" + car.unitPrice + "/天tt" + car.carryGoods + "噸");
        } else if(car.getClass() == PickUp.class) {
            System.out.println(car.id + "t" + car.carName + "t" + car.unitPrice + "/天tt" + car.carryMan + "人," + car.carryGoods + "噸");
        }
    }
}
//租車數量
void carsNum(int numbers) {
    //選擇每輛車的編號組成的陣列
    Scanner input = new Scanner(System.in);
    //定義陣列長度
    carsId = new int[numbers];
    for(int i = 1; i 

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1600/viewspace-2798914/,如需轉載,請註明出處,否則將追究法律責任。

相關文章