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

為了賬號安全,請及時綁定郵箱和手機立即綁定

Java入門第二季 答答租車系統(tǒng) 類視圖及源代碼

標簽:
Java
示例

图片描述

类视图

图片描述

源码

ITranHuman.java

public interface ITranHuman {
    void setSeatCount(int seatCount);
    int getSeatCount();
}

ITranGood.java

public interface ITranGood {
    void setGoodWeight(int goodWeight);
    int getGoodWeight();
}

Car.java

public abstract class Car {
    private String name;
    private int price;

    public Car(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public abstract String getCapacity();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

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

HumanCar.java

public class HumanCar extends Car implements ITranHuman {
    private int seatCount;

    public HumanCar(String name, int price, int seatCount) {
        super(name, price);
        this.seatCount = seatCount;
    }

    @Override
    public String getCapacity() {
        return "载人:" + seatCount + "人";
    }

    @Override
    public int getSeatCount() {
        return seatCount;
    }

    @Override
    public void setSeatCount(int seatCount) {
        this.seatCount = seatCount;
    }
}

GoodCar.java

public class GoodCar extends Car implements ITranGood {
    private int goodWeight;

    public GoodCar(String name, int price, int goodWeight) {
        super(name, price);
        this.goodWeight = goodWeight;
    }

    @Override
    public String getCapacity() {
        return "载重:" + goodWeight + "吨";
    }

    @Override
    public int getGoodWeight() {
        return goodWeight;
    }

    @Override
    public void setGoodWeight(int goodWeight) {
        this.goodWeight = goodWeight;
    }
}

HumanGoodCar.java

public class HumanGoodCar extends Car implements ITranHuman, ITranGood {
    private int seatCount;
    private int goodWeight;

    public HumanGoodCar(String name, int price, int seatCount, int goodWeight) {
        super(name, price);
        this.seatCount = seatCount;
        this.goodWeight = goodWeight;
    }

    @Override
    public String getCapacity() {
        return "载人:" + seatCount + "人,载重:" + goodWeight + "吨";
    }

    @Override
    public int getSeatCount() {
        return seatCount;
    }

    @Override
    public void setSeatCount(int seatCount) {
        this.seatCount = seatCount;
    }

    @Override
    public int getGoodWeight() {
        return goodWeight;
    }

    @Override
    public void setGoodWeight(int goodWeight) {
        this.goodWeight = goodWeight;
    }
}

Main.java

import java.util.Scanner;

public class Main {
    public static final Car[] CARS = {
            new HumanCar("奥迪A4", 500, 4),
            new HumanCar("马自达6", 400, 4),
            new HumanGoodCar("皮卡雪6", 450, 4, 2),
            new HumanCar("金龙", 800, 20),
            new GoodCar("松花江", 400, 4),
            new GoodCar("依维柯", 1000, 20)
    };

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.printf("欢迎使用答答租车系统:%n您是否要租车:1是;0否%n");
        int yesOrNo = scanner.nextInt();
        if (yesOrNo == 1) {
            System.out.printf("%n您可租车的类型及其价目表:%n");
            System.out.println("序号\t汽车名称\t租金\t\t容量");
            for (int i = 0; i < CARS.length; i++) {
                System.out.printf("%d. \t%s\t\t%d元/天\t%s%n", i + 1, CARS[i].getName(), CARS[i].getPrice(), CARS[i].getCapacity());
            }

            System.out.printf("%n请输入您要租车的数量:%n");
            int count = scanner.nextInt();

            int[] choices = new int[CARS.length];
            for (int i = 0; i < count; i++) {
                System.out.printf("请输入第%d辆车的序号:%n", i + 1);
                int no = scanner.nextInt() - 1;
                choices[no]++;
            }

            System.out.printf("%n请输入租车天数:%n");
            int days = scanner.nextInt();

            System.out.printf("%n您的帐单:%n");

            System.out.println("***可裁人的车有:");
            int seatCountTotal = 0;
            for (int i = 0; i < choices.length; i++) {
                if (choices[i] != 0 && CARS[i] instanceof ITranHuman) {
                    System.out.printf("%s*%d ", CARS[i].getName(), choices[i]);
                    seatCountTotal += ((ITranHuman) CARS[i]).getSeatCount() * choices[i];
                }
            }
            System.out.printf("共载人:%d人%n", seatCountTotal);

            System.out.println("***可裁货的车有:");
            int goodWeightTotal = 0;
            for (int i = 0; i < choices.length; i++) {
                if (choices[i] != 0 && CARS[i] instanceof ITranGood) {
                    System.out.printf("%s*%d ", CARS[i].getName(), choices[i]);
                    goodWeightTotal += ((ITranGood) CARS[i]).getGoodWeight() * choices[i];
                }
            }
            System.out.printf("共载货:%d吨%n", goodWeightTotal);

            int priceTotal = 0;
            for (int i = 0; i < choices.length; i++) {
                if (choices[i] != 0) {
                    priceTotal += CARS[i].getPrice() * choices[i];
                }
            }
            System.out.printf("***租车总价格:%d元%n", priceTotal * days);
        }

        scanner.close();
    }
}
點擊查看更多內容
89人點贊

若覺得本文不錯,就分享一下吧!

評論

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

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

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領

立即參與 放棄機會
微信客服

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

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消