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

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

Java入門 第二季 第六章練習(xí)

標(biāo)簽:
Java
差不多一个下午的时间,将老师最后要求的那个练习进行了实现,使用eclipse测试结果界面完全与测试用例一致!
还希望大家有兴趣的一起讨论。

/**

  • 父类 Car
    */
public class Car {
    public String name;
    public int rent;

    public String getName() {
        return name;
    }

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

    public int getRent() {
        return rent;
    }

    public void setRent(int rent) {
        this.rent = rent;
    }

    public void display() {
        System.out.println("输出自己的相关信息");
    }
}

/**

  • 子类 Manned
    */
public class Manned extends Car {
    public int peopleCapacity;

    public Manned(String name, int rent, int peoplecapacity) {
        this.name = name;
        this.rent = rent;
        this.peopleCapacity = peoplecapacity;
    }

    public int getPeopleCapacity() {
        return peopleCapacity;
    }

    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }

    @Override
    public void display() {
        System.out.println(name + " " + rent + "元/天" + "    " + "载人:"
                + peopleCapacity + "人");
    }

}

/**

  • 子类 Pickup
    */
public class Pickup extends Car {
    private int peopleCapacity;
    private int load;

    public Pickup(String name, int rent, int peoplecapcaity, int load) {
        this.name = name;
        this.rent = rent;
        this.peopleCapacity = peoplecapcaity;
        this.load = load;
    }

    public int getPeopleCapacity() {
        return peopleCapacity;
    }

    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }

    public int getLoad() {
        return load;
    }

    public void setLoad(int load) {
        this.load = load;
    }

    @Override
    public void display() {
        System.out.println(name + " " + rent + "元/天" + "    " + "载人:"
                + peopleCapacity + "人" +" "+ "载货:" + load + "吨");
    }

}

/**

  • 子类 Truck
    */
public class Truck extends Car {
    private int load;

    public Truck(String name, int rent, int load) {
        this.name = name;
        this.rent = rent;
        this.load = load;
    }

    public int getLoad() {
        return load;
    }

    public void setLoad(int load) {
        this.load = load;
    }

    @Override
    public void display() {
        System.out
                .println(name + "   " + rent + "元/天" + "    " + "载货:" + load + "吨");
    }

}

/**

  • 测试类 DadaCarTest
    */
import java.util.Scanner;

public class DadaCarTest {
    public void display(Car[] cars) {
        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号  汽车名称    租金  容量");
        for (int i = 0; i < cars.length; i++) {
            System.out.print(i + 1 + "  ");
            cars[i].display();
        }
    }

    public static void main(String[] args) {
        Car[] cars = { new Manned("奥迪A4", 500, 4), new Manned("马自达6", 400, 4),
                new Pickup("皮卡雪6", 450, 4, 2), new Manned("金龙", 800, 20),
                new Truck("松花江", 400, 4), new Truck("依维柯", 1000, 20) };
        DadaCarTest dct = new DadaCarTest();
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车:1是 0否");
        int selectNum = sc.nextInt();
        if (selectNum == 1) {
            dct.display(cars);
            System.out.println("请输入您要租汽车的数量:");
            int num = sc.nextInt();
            int[] carNum = new int[num];
            for (int i = 1; i < num + 1; i++) {
                System.out.println("请输入第" + i + "辆车的序号:");
                carNum[i - 1] = sc.nextInt();
            }
            System.out.println("请输入租车天数:");
            int days = sc.nextInt();
            System.out.println("您的账单:");            
            int totalPeopleNum = 0;
            int totalLoadNum = 0;
            double totalRent = 0.0;
            System.out.println("***可载人的车有:");
            System.out.print("  ");
            for (int i = 0; i < num; i++) {
                Car car = cars[carNum[i] - 1];
                if (car instanceof Manned) {
                    Manned manned = (Manned) car;
                    System.out.print(manned.name + "    ");
                    totalPeopleNum += manned.getPeopleCapacity();
                    totalRent += manned.getRent() * days;
                } else if (car instanceof Pickup) {
                    Pickup pickup = (Pickup) car;
                    System.out.print(pickup.name + "    ");
                    totalPeopleNum += pickup.getPeopleCapacity();
                    totalLoadNum += pickup.getLoad();
                    //totalRent += pickup.getRent();
                }
            }           
            System.out.println("共载人:" + totalPeopleNum + "人");
            System.out.println("***载货的车有:");
            System.out.print("  ");
            for (int i = 0; i < num; i++) {
                Car car = cars[carNum[i] - 1];
                if (car instanceof Truck) {
                    Truck truck = (Truck) car;
                    System.out.print(truck.name + " ");
                    totalLoadNum += truck.getLoad();
                    totalRent += truck.getRent() * days;
                } else if (car instanceof Pickup) {
                    Pickup pickup = (Pickup) car;
                    System.out.print(pickup.name + "    ");
                    totalLoadNum += pickup.getLoad();
                    totalRent += pickup.getRent() * days;
                }
            }
            System.out.println("共载货:" + totalLoadNum + "吨");
            System.out.println("***租车总价格:" + totalRent + "元");
        }
    }
}
點(diǎn)擊查看更多內(nèi)容
7人點(diǎn)贊

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

評論

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

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

100積分直接送

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

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

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

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

幫助反饋 APP下載

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

公眾號

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

舉報(bào)

0/150
提交
取消