Java入門第二季 6-1綜合練習程式碼

kboypkb發表於2021-09-09

程式碼參考了@nuanchazy的

建立了Vehicle父類,Car、Cargo、Pickup子類,沒有使用介面。
Test.java


```package com.DaDa;

import java.util.Scanner;

public class Test {
    int sumPeople;//總載客量
    int sumCargo;//總載貨量
    int sumPrice;//總價格
    String cargoName="";//可載貨的汽車名稱
    String carName="";//可載人的汽車名稱
    //詢問是否使用系統
    public void start() {
        System.out.println("歡迎使用答答租車系統:n您是否要租車:1.是      0.否");
        Scanner sc1=new Scanner(System.in);
        int sc1a=sc1.nextInt();
        while (sc1a!=1&&sc1a!=0) {//當輸入值不是1或0時要求重新輸入
            System.out.println("您輸入的資訊有誤,請重新輸入:");
            Scanner sc2=new Scanner(System.in);
            sc1a=sc2.nextInt();
        }
        if (sc1a==0) {
            System.out.println("******感謝使用答答租車系統******");
            return;
        }
        //輸出價目表
        System.out.println("您可租車的型別及其價目表:");
        System.out.println("序號t汽車名稱t租金t容量");
        Vehicle[] vehicles= {new Car(1, "奧迪A4", 500, 4),
                             new Car(2, "馬自達6", 400, 4),
                             new Pickup(3, "皮卡", 450, 4, 2),
                             new Car(4, "金龍", 800, 20),
                             new Cargo(5, "松花江", 400, 4),
                             new Cargo(6, "依維柯", 1000, 20)};
        for (int i = 0; i =7) {//確保輸入值為1~6
                System.out.println("您輸入的序號有誤,請重新輸入:");
                Scanner sc6=new Scanner(System.in);
                sc5a=sc6.nextInt();
            }
            sumPeople+=vehicles[sc5a-1].people;//將所選汽車的載客量相加得出總載客量
            sumCargo+=vehicles[sc5a-1].cargo;//將所選汽車的載貨量相加得出總載貨量
            sumPrice+=vehicles[sc5a-1].price;//將所選汽車的價格相加得出總價格
            if (vehicles[sc5a-1].getClass()==Car.class){//判斷所選汽車可是否可載人
                carName+=vehicles[sc5a-1].name+"   ";
            } else if (vehicles[sc5a-1].getClass()==Cargo.class) {//判斷所選汽車可是否可載貨
                cargoName+=vehicles[sc5a-1].name+"   ";
            }else if (vehicles[sc5a-1].getClass()==Pickup.class) {
                carName+=vehicles[sc5a-1].name+"   ";
                cargoName+=vehicles[sc5a-1].name+"   ";
            }
        }
        //輸入租車天數
        System.out.println("請輸入您要租車的天數:");
        Scanner sc7=new Scanner(System.in);
        int sc7a=sc7.nextInt();
        while (sc7a0) {//無可載人汽車則不輸出
            System.out.println("***可載人的車有:n"+carName+"     共載:"+sumPeople+"人");
        }
        if (sumCargo>0) {//無可載貨汽車則不輸出
            System.out.println("***可載貨的車有:n"+cargoName+"     共載:"+sumCargo+"噸");
        }
        System.out.println("***租車總價格:"+sumPrice+"元");
    }
    //主方法
    public static void main(String[] args) {
        Test aTest=new Test();
        aTest.start();
    }

}

Vehicle.java

package com.DaDa;
//建立車輛父類,用於被3種車型的子類基礎,子類可使用父類的成員變數
public abstract class Vehicle {
    int id;
    String name;
    int price;
    int people;
    int cargo;
}

Car.java

package com.DaDa;
//只載人汽車
public class Car extends Vehicle {
    public Car() {
    }
    public Car(int id,String name,int price,int people) {
        this.id=id;
        this.name=name;
        this.price=price;
        this.people=people;
    }
    @Override
    public String toString() {
        return id+".t"+name+"t"+price+"元/天t"+"載人:"+people+"人";
    }
}

Cargo.java

package com.DaDa;
//只載貨的貨車
public class Cargo extends Vehicle {

    public Cargo(int id,String name,int price,int cargo) {
        this.id=id;
        this.name=name;
        this.price=price;
        this.cargo=cargo;
    }
    @Override
    public String toString() {
        return id+".t"+name+"t"+price+"元/天t"+"載貨:"+cargo+"噸";
    }
}

Pickup.java

package com.DaDa;
//即可載人又可載貨的皮卡
public class Pickup extends Vehicle {

    public Pickup(int id,String name,int price,int people,int cargo) {
        this.id=id;
        this.name=name;
        this.price=price;
        this.people=people;
        this.cargo=cargo;
    }
    @Override
    public String toString() {
        return id+".t"+name+"t"+price+"元/天t"+"載人:"+people+"人,載貨:"+cargo+"噸";
    }
}

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

相關文章