答答租車系統規範程式碼示例

lotus_ruan發表於2021-09-09

父類

package practice;

public class Car {
    public String carname;
    public int  busload;//載客量
    public double  burden;//載貨量
    public double money;//金額
    public String getCarname() {
        return carname;
    }
    public void setCarname(String carname) {
        this.carname = carname;
    }
    public int getBusload() {
        return busload;
    }
    public void setBusload(int busload) {
        this.busload = busload;
    }
    public double getBurden() {
        return burden;
    }
    public void setBurden(double burden) {
        this.burden = burden;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
}

載貨子類

package practice;

public class Busdencar extends Car {

    public String carname;
    public int  busload;//載客量
    public double  burden;//載貨量
    public double money;//金額

    public Busdencar(String carname,int busload, double burden,double money){
    this.carname = carname;
    this.busload = busload;
    this.burden = burden;
    this.money = money;

}

    public String getCarname() {
        return carname;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }

    public int getBusload() {
        return busload;
    }

    public void setBusload(int busload) {
        this.busload = busload;
    }

    public double getBurden() {
        return burden;
    }

    public void setBurden(double burden) {
        this.burden = burden;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

}

載客子類

package practice;

public class Busloadcar extends Car {

    public String carname;
    public int  busload;//載客量
    public double  burden;//載貨量
    public double money;//金額

    public Busloadcar(String carname,int busload, double burden,double money){
    this.carname = carname;
    this.busload = busload;
    this.burden = burden;
    this.money = money;
}

    public String getCarname() {
        return carname;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }

    public int getBusload() {
        return busload;
    }

    public void setBusload(int busload) {
        this.busload = busload;
    }

    public double getBurden() {
        return burden;
    }

    public void setBurden(double burden) {
        this.burden = burden;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

}

載客載貨子類

package practice;

public class PackUp extends Car {

    public String carname;
    public int  busload;//載客量
    public double  burden;//載貨量
    public double money;//金額

    public PackUp(String carname,int busload, double burden,double money){
    this.carname = carname;
    this.busload = busload;
    this.burden = burden;
    this.money = money;
}

    public String getCarname() {
        return carname;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }

    public int getBusload() {
        return busload;
    }

    public void setBusload(int busload) {
        this.busload = busload;
    }

    public double getBurden() {
        return burden;
    }

    public void setBurden(double burden) {
        this.burden = burden;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

}

使用者測試程式碼

package practice;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car[] car = {new Busloadcar("奧迪A4",4,0,500),new Busloadcar("馬自達6",4,0,400),
                new PackUp("皮卡雪6",4,2,450),new Busloadcar("金龍",20,0,800),new Busdencar("松花江",0,4,400),
                new Busdencar("依維柯",0,20,1000)};

        //詢問使用者
        System.out.println("歡迎使用答答租車系統:");
        System.out.println("您是否要租車:1是 0否");
        Scanner console = new Scanner(System.in);
        int answer = console.nextInt();
        int i = 1;
        if(answer == 1){
            //輸出可租車型及其價目表
            System.out.println("您可租輸的型別及其價目表:");
            System.out.println("序號" + " " + "汽車名稱" + " " + "租金" +  " " + "容量");
            for(Car foreachcar : car){
                System.out.println(i + " " + foreachcar.getCarname() + " " + foreachcar.getMoney() + "元/天" + " "+ "載人:" + 
            foreachcar.getBusload() + "人"  + " " + "載貨" + foreachcar.getBurden() + "噸");
                i++;
            }

            //詢問使用者租車數量
            System.out.println("請輸入您要租汽車的數量:");
            int count = console.nextInt();
            Car[] rentcar = new Car[count];
            for(int j = 0 ; j

圖片描述

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

相關文章