Java第二季租車系統參考

liuxuhui發表於2021-09-09

首先建立一個汽車的抽象類CarStore,裡面包含汽車的編號ID,車名Name以及價格Price,並且建立set和get方法。

package cw.lojako.carrent;

public abstract class CarStore {
    private int id;
    private String name;
    private double price;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }

}

之後我們可以建立兩個介面,載客量(CarryingPeople),載貨量(CarryingGoods)。
這裡引出一個問題:什麼時候用介面呢?就是一個功能並不是所有物件都擁有時,可以把這個功能寫進介面中,如若該物件需要此功能則實現該介面即可。就拿我們這個例子而言,貨車的載貨功能客車並不具備,所以需要用介面來實現。

介面(CarryingPeople)

package cw.lojako.carrent;

public interface CarryingPeople {
    public void setPeople(int n);
    public int getPeople();
}

介面(CarryingGoods)

package cw.lojako.carrent;

public interface CarryingGoods {
    public void setGoods(int n);
    public int getGoods();
}

第三步就是建立客車類,貨車類,皮卡類並繼承抽象類CarStore,實現相對應的介面。程式碼如下:

客車類(Car.java)

package cw.lojako.carrent;

public class Car extends CarStore implements CarryingPeople{

    private int people;
    //初始化值
    public Car(int id,String name,double price,int people){
        super.setId(id);
        super.setName(name);
        super.setPrice(price);
        this.setPeople(people);
    }

    @Override
    public void setPeople(int people) {
        this.people = people;       
    }

    @Override
    public int getPeople() {
        return people;  
    }

    @Override
    public String toString() {
        return "編號:"+super.getId()+"t"+"車名:"+super.getName()+"t"+"價格:"+super.getPrice()+"元/天t"+"載客量:"+this.getPeople()+"人";
    }

}

貨車類(Van)

package cw.lojako.carrent;

public class Van extends CarStore implements CarryingGoods {

    private int goods;
    //初始化值
    public Van(int id,String name,double price,int goods){
        super.setId(id);
        super.setName(name);
        super.setPrice(price);
        this.setGoods(goods);
    }
    @Override
    public void setGoods(int goods) {   
        this.goods = goods;
    }

    @Override
    public int getGoods() {
        return goods;
    }
    @Override
    public String toString() {
        return "編號:"+super.getId()+"t"+"車名:"+super.getName()+"t"+"價格:"+super.getPrice()+"元/天t"+"載貨量:"+this.getGoods()+"T";
    }
}

皮卡類(PickUp)

package cw.lojako.carrent;

public class PickUp extends CarStore implements CarryingGoods,CarryingPeople{

    private int goods;
    private int people;
    //初始化值
    public PickUp(int id,String name,double price,int people,int goods){
        super.setId(id);
        super.setName(name);
        super.setPrice(price);
        this.setPeople(people);
        this.setGoods(goods);
    }
    @Override
    public void setGoods(int goods) {   
        this.goods = goods;
    }

    @Override
    public int getGoods() {
        return goods;
    }
    @Override
    public void setPeople(int people) {
        this.people = people;
    }
    @Override
    public int getPeople() {
        return people;
    }
    @Override
    public String toString() {
        return "編號:"+super.getId()+"t"+"車名:"+super.getName()+"t"+"價格:"+super.getPrice()+"元/天t"+"載客量:"+this.getPeople()+"人"+"  "+"載貨量:"+this.getGoods()+"T";
    }
}

最後一步是寫主頁面

package cw.lojako.carrent;

import java.util.Scanner;

public class RentSystem {

    CarStore[] cars ={new Car(1,"奧迪A4",500,4), 
                      new Car(2,"馬自達",400,4), 
                      new PickUp(3,"皮卡雪",450,4,20), 
                      new Car(4,"保時捷",800,20), 
                      new Van(5,"松花江",400,4), 
                      new Van(6,"依維柯",1000,20)};
    Scanner in = new Scanner(System.in);
    public void rent(){
        System.out.println("歡迎使用答答租車系統: n是否租車???    1(是)  0(否)");
        int input = in.nextInt();
        if(input==1){
            System.out.println("您可租車的型別和價目表: "+"n  序號"+"======"+"車名"+"==============="+"租金"+"==========="+"容量");
            for(int i=0;i=cars.length||j

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

相關文章