第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Java入門第二季項(xiàng)目實(shí)戰(zhàn)——答答租車

標(biāo)簽:
C

定义Car类:

package com.imooc.mika.dadarent;

public class Car {
    private String carName;
    private double price;

    public Car(String carName, double price) {
        this.carName = carName;
        this.price = price;
    }

    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return carName + "\t" + price;
    }

}

创建3个继承Car的子类:
客车BusCar :

package com.imooc.mika.dadarent;

public class BusCar extends Car {

    private int busLoad;

    public BusCar(String carName, double price) {
        super(carName, price);
    }

    public BusCar(String carName, int busLoad, double price) {
        super(carName, price);

        this.busLoad = busLoad;
    }

    public int getBusLoad() {
        return busLoad;
    }

    public void setBusLoad(int busLoad) {
        this.busLoad = busLoad;
    }

    @Override
    public String toString() {
        return super.toString() + "\t" + busLoad + "\t-";
    }
}

货车TrunkCar :

package com.imooc.mika.dadarent;

public class TrunkCar extends Car {

    private double deadWeight;

    public TrunkCar(String carName, double price) {
        super(carName, price);
    }

    public TrunkCar(String carName, double deadWeight, double price) {
        super(carName, price);
        this.deadWeight = deadWeight;
    }

    public double getDeadWeight() {
        return deadWeight;
    }

    public void setDeadWeight(double deadWeight) {
        this.deadWeight = deadWeight;
    }
    @Override
    public String toString() {
        return super.toString() + "\t-\t" + deadWeight;
    }
}

客货两用PickUpCar :

package com.imooc.mika.dadarent;

public class PickUpCar extends Car {

    private int busLoad;
    private double deadWeight;

    public PickUpCar(String carName, double price) {
        super(carName, price);
    }

    public PickUpCar(String carName, int busLoad, double deadWeight, double price) {
        super(carName, price);
        this.busLoad = busLoad;
        this.deadWeight = deadWeight;
    }

    public int getBusLoad() {
        return busLoad;
    }

    public void setBusLoad(int busLoad) {
        this.busLoad = busLoad;
    }

    public double getDeadWeight() {
        return deadWeight;
    }

    public void setDeadWeight(double deadWeight) {
        this.deadWeight = deadWeight;
    }

    @Override
    public String toString() {
        return super.toString() + "\t" + busLoad + "\t" + deadWeight;
    }

}

主程序:

package com.imooc.mika.dadarent;

import java.util.HashSet;
import java.util.Scanner;

public class DaDaRent {
    private static Scanner sn = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("欢迎使用答答租车系统!");
        boolean quit = false;

        while (!quit) {
            System.out.println("租车请输入数字'1',退出系统请输入数字'0'");

            switch (inputCommand()) {
            case 0:
                quit = true;
                break;
            case 1:
                rentCars();
                break;
            default:
                System.out.println("命令输入错误!请根据提示输入命令值!");
                break;
            }
        }

        sn.close();
        System.out.println("退出系统~");
    }

    private static void rentCars() {
        // 可选车型初始化
        Car[] carList = initCarList();
        // 打印可选车型信息
        printCarList(carList);
        // 输入租车数量
        int rentCount = setRentCount();
        // 选择具体车型
        Car[] selectCars = selectCars(carList, rentCount);
        // 输入租车天数
        int days = setRentDays();
        // 打印账单
        printBill(selectCars, days);
    }

    private static Car[] initCarList() {
        Car[] carList = new Car[5];
        carList[0] = new TrunkCar("货车1号", 5, 500);
        carList[1] = new TrunkCar("货车2号", 10, 800);
        carList[2] = new BusCar("客车1号", 4, 400);
        carList[3] = new BusCar("客车2号", 20, 1500);
        carList[4] = new PickUpCar("客货两用", 4, 8, 1000);
        return carList;
    }

    private static void printCarList(Car[] carList) {

        System.out.println("可选车型一览表:");
        System.out.println("编号\t车型\t日租金(元)\t载客量(人)\t载重量(吨)");

        for (int i = 0; i < carList.length; i++) {
            System.out.println((i + 1) + "\t" + carList[i].toString());
        }
    }

    private static int setRentCount() {
        int rentCount = 0;
        do {
            System.out.println("请输入租车数量(1-100):");
            rentCount = inputCommand();
        } while (!(rentCount > 0 && rentCount <= 100));
        return rentCount;
    }

    private static Car[] selectCars(Car[] carList, int rentCount) {

        System.out.println("请输入每辆车辆的编号:");
        Car[] selectCars = new Car[rentCount];
        int carNo = 0;
        for (int i = 0; i < rentCount; i++) {

            do {
                System.out.println("第" + (i + 1) + "辆车的编号(1-" + carList.length + "):");
                carNo = inputCommand();
            } while (!(carNo > 0 && carNo <= carList.length));

            selectCars[i] = carList[carNo - 1];
        }

        return selectCars;
    }

    private static int setRentDays() {
        int days = 0;
        do {
            System.out.println("请输入租车的天数(1-365):");
            days = inputCommand();
        } while (!(days > 0 && days <= 365));
        return days;
    }

    private static void printBill(Car[] selectCars, int days) {

        System.out.println("账单:————————————————————————");
        System.out.println("租用车辆数量:" + selectCars.length);
        System.out.println("租用天数:" + days);

        // 计算选择车辆的总载客数、载重量、总租用金额、车型名称
        HashSet<String> carNames = new HashSet<String>();
        int sumBusLoad = 0;
        double sumDeadWeight = 0;
        double sumPrice = 0;

        for (Car car : selectCars) {

            carNames.add(car.getCarName());

            sumPrice += car.getPrice();

            if (car instanceof BusCar)
                sumBusLoad += ((BusCar) car).getBusLoad();

            if (car instanceof TrunkCar)
                sumDeadWeight += ((TrunkCar) car).getDeadWeight();

            if (car instanceof PickUpCar) {
                sumBusLoad += ((PickUpCar) car).getBusLoad();
                sumDeadWeight += ((PickUpCar) car).getDeadWeight();
            }
        }

        System.out.println("总载客数(人):" + sumBusLoad);
        System.out.println("总载重量(吨):" + sumDeadWeight);
        System.out.println("总租用金额(元):" + sumPrice * days);
        System.out.println("租用车辆型号:" + carNames.toString());
        System.out.println("————————————————————————————");
    }

    private static int inputCommand() {
        try {
            return sn.nextInt();
        } catch (Exception e) {
            sn = new Scanner(System.in);
            return -1;
        }
    }
}
點(diǎn)擊查看更多內(nèi)容
13人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消