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

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

一個(gè)理解過(guò)于復(fù)雜的租車系統(tǒng)

標(biāo)簽:
Java

java课程2 写一个租车系统

理解成了全部变成交互。。于是还处理了输入异常等等情况

进入:

package com.imooc;

public class Initial {
    public static void main(String[] args) {
        System.out.println("Welcome to rent car system");
        User user = new User();
        user.chooseV();
    }
}

User 类

package com.imooc;
import java.util.Scanner;

public class User {
    private String name;
    private Vehicle ve;

    public User() {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter user name: ");
        name = input.next();
    }

    public void chooseV() {
        Scanner input = new Scanner(System.in);
        System.out.println("Hi " + name + ", please enter the class of vehicle you want to rent");
        System.out.println("1. Truck - for goods only");
        System.out.println("2. Car - for people only");
        System.out.println("3. Van - for both goods and people");
        System.out.println("Other number to exit");

        int label = 0;
        try {
            label = input.nextInt();
        } catch (Exception e) {
            System.out.println("!!!!Input error!!!!");
            chooseV();
            return;
        }
        if (label == 1) {
            ve = new Truck();
        } else if (label == 2) {
            ve = new Car();
        } else if (label == 3) {
            ve = new Van();
        } else {
            System.out.println("~~~~Bye~~~~");
            return;
        }
        rent();
    }

    public void rent() {
        Scanner input = new Scanner(System.in);
        System.out.println("Ability to load people: " + ve.getLoadPeopleAbility());
        System.out.println("Ability to load goods: " + ve.getLoadGoodsAbility());
        System.out.println("-----------------------------");
        if (ve instanceof Truck) {
            System.out.println("Please enter the weight of goods to load: ");
            float weight = 0f;
            try {
                weight = input.nextFloat();
            } catch (Exception e) {
                System.out.println("!!!!Input error!!!!");
                rent();
                return;
            }
            if (weight <= 5.0f) {
                ve = new LightTruck();
            } else {
                ve = new HeavyTruck();
            }

        } else if (ve instanceof Car) {
            System.out.println("Please enter the number of people to load: ");
            int people = 0;
            try {
                people = input.nextInt();
            } catch (Exception e) {
                System.out.println("!!!!Input error!!!!");
                rent();
                return;
            }
            if (people >= 1 && people <= 5) {
                ve = new LightTruck();
            } else if (people >= 10) {
                ve = new HeavyTruck();
            } else if (people < 10 && people > 5) {
                System.out.println("!!!!You should choose a van!!!!");
                chooseV();
                return;
            } else {
                System.out.println("!!!!Wrong number of people!!!!");
                rent();
                return;
            }
        }
        System.out.println("Please enter the number of days to rent: ");
        int day = input.nextInt();
        ve.rent(day);
    }
}

Vehicle abstract class

package com.imooc;

public abstract class Vehicle {
    public abstract float getPrice();
    public abstract String getName();
    public abstract boolean getLoadPeopleAbility();
    public abstract boolean getLoadGoodsAbility();
    public abstract void rent(int day);

}

五种车型, truck,car,van继承vehicle,其中truck和car又分两种型号

package com.imooc;

public class Truck extends Vehicle {
    @Override
    public boolean getLoadPeopleAbility() {
        return false;
    }
    @Override
    public boolean getLoadGoodsAbility() {
        return true;
    }
    @Override
    public float getPrice() {
        return 0f;
    }
    @Override
    public String getName() {
        return "Truck";
    }
    public void rent(int day) {}
}
package com.imooc;

public class LightTruck extends Truck {
    private float loadGoodsMin = 0f;
    private float loadGoodsMax = 5.0f;
    private String name = "Light truck";
    private float price = 200f;

    public float getLoadGoodsMax() {
        return loadGoodsMax;
    }

    public float getLoadGoodsMin() {
        return loadGoodsMin;
    }

    @Override
    public float getPrice() {
        return price;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void rent(int day) {
        float money = price * day;
        System.out.println("You rent a " + name +
                " for " + day + " days, needing RMB: " + money);
    }
}
package com.imooc;

public class HeavyTruck extends Truck {
    private float loadGoodsMin = 5.0f;
    private float loadGoodsMax = 20.0f;
    private String name = "Heavy truck";
    private float price = 500f;

    public float getLoadGoodsMax() {
        return loadGoodsMax;
    }

    public float getLoadGoodsMin() {
        return loadGoodsMin;
    }

    @Override
    public float getPrice() {
        return price;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void rent(int day) {
        float money = price * day;
        System.out.println("You rent a " + name +
                " for " + day + " days, needing RMB: " + money);
    }
}
package com.imooc;

public class Car extends Vehicle {
    @Override
    public float getPrice() {
        return 0;
    }
    @Override
    public String getName() {
        return null;
    }
    @Override
    public boolean getLoadPeopleAbility() {
        return true;
    }
    @Override
    public boolean getLoadGoodsAbility() {
        return false;
    }
    @Override
    public void rent(int day) {

    }
}
package com.imooc;

public class SmallCar extends Car {
    private int loadPeopleMin = 1;
    private int loadPeopleMax = 5;
    private float price = 80f;
    private String name = "Small car";

    public int getLoadPeopleMax() {
        return loadPeopleMax;
    }

    public int getLoadPeopleMin() {
        return loadPeopleMin;
    }

    @Override
    public void rent(int day) {
        float money = price * day;
        System.out.println("You rent a " + name +
                " for " + day + " days, needing RMB: " + money);
    }
}
package com.imooc;

public class LargeCar extends Car {
    private int loadPeopleMin = 10;
    private int loadPeopleMax = 50;
    private String name = "Large car";
    private float price = 250f;

    public int getLoadPeopleMax() {
        return loadPeopleMax;
    }

    public int getLoadPeopleMin() {
        return loadPeopleMin;
    }

    @Override
    public void rent(int day) {
        float money = price * day;
        System.out.println("You rent a " + name +
                " for " + day + " days, needing RMB: " + money);
    }
}
package com.imooc;
import java.util.Scanner;

public class Van extends Vehicle {
    private int people;
    private float weight;

    private String name = "Van";
    private float price = 400f;

    public Van() {
        loadPeople();
    }
    private void loadGoods() {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter weight of goods to load");
        try {
            weight = input.nextFloat();
        } catch (Exception e) {
            System.out.println("!!!!Wrong form for weight of goods!!!!");
            loadGoods();
        }
        if (weight < 0) {
            System.out.println("!!!!Wrong weight of goods!!!!");
            loadGoods();
        }
    }

    private void loadPeople() {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter number of people to load: ");
        try {
            people = input.nextInt();
        } catch (Exception e) {
            System.out.println("!!!!Wrong form for number of people!!!!");
        }
        if (people <= 0) {
            System.out.println("!!!!Wrong number of people!!!!");
            loadPeople();
        }
    }
    public float getWeight() {
        return weight;
    }

    public int getPeople() {
        return people;
    }

    @Override
    public float getPrice() {
        return price;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public boolean getLoadPeopleAbility() {
        return true;
    }

    @Override
    public boolean getLoadGoodsAbility() {
        return true;
    }

    @Override
    public void rent(int day) {
        float money = price * day;
        System.out.println("You rent a " + name +
                " for " + day + " days, needing RMB: " + money);
    }
}
點(diǎn)擊查看更多內(nèi)容
28人點(diǎn)贊

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

評(píng)論

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

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(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
提交
取消