1. 程式人生 > 其它 >Java-陣列完成租車系統案例

Java-陣列完成租車系統案例

技術標籤:Java基礎學習java

Java-陣列完成租車系統案例

要求:實現租車的種類選擇,租車的天數,折扣租賃的費用計算,分配汽車的牌號。

建立汽車類:

public class Automobile {
    private String plate;//車牌
    private String brand;//品牌
    private int rent;//租金

    public Automobile(String plate, String brand, int rent) {
        this.plate = plate;
        this.brand =
brand; this.rent = rent; } public String getPlate() { return plate; } public void setPlate(String plate) { this.plate = plate; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand;
} public int getRent() { return rent; } public void setRent(int rent) { this.rent = rent; } }

建立轎車類:

public class Car extends Automobile {
    private String model;//型號

    public Car(String plate, String brand, int rent, String model) {
        super(plate, brand,
rent); this.model = model; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } }

建立客車類:

public class Bus extends Automobile {
    private String seatNumber;//座位數

    public Bus(String plate, String brand, int rent, String seatNumber) {
        super(plate, brand, rent);
        this.seatNumber = seatNumber;
    }

    public String getSeatNumber() {
        return seatNumber;
    }

    public void setSeatNumber(String seatNumber) {
        this.seatNumber = seatNumber;
    }
}

汽車租賃管理類:

public class AutomobileManage {
    /*初始化轎車*/
    public Car[] initCar(){
        Car car1 = new Car("京NY28588","寶馬",800,"X6");
        Car car2 = new Car("京CNY3282","寶馬",600,"550i");
        Car car3 = new Car("京NT37465","別克",300,"綠蔭大道");
        Car car4 = new Car("京NT96968","別克",600,"GL8");

        Car[] cars={car1,car2,car3,car4};

        return cars;
    }
    /*初始化客車*/
    public Bus[] initBus(){
        Bus bus1 = new Bus("京5673432","金盃",800,"16");
        Bus bus2 = new Bus("京NY28555","金龍",800,"16");
        Bus bus3 = new Bus("京NY23274","金盃",1500,"34");
        Bus bus4 = new Bus("京NY28573","金龍",1500,"34");

        Bus[] buses={bus1,bus2,bus3,bus4};

        return buses;
    }
}

汽車業務類:

public class AutomobileService {
    /*租車服務入口*/
    public void rentService(){
        System.out.println("**********歡迎來到秋名山租車系統************");

        Scanner scanner = new Scanner(System.in);
        System.out.print("請選擇你要租的汽車型別:1.轎車,2.客車:");
        int type = scanner.nextInt();
        AutomobileManage automobileManage = new AutomobileManage();
        String brand;/*車的牌號*/
        int day;/*租車的天數*/
        if(type==1){
            Car[] cars = automobileManage.initCar();

            System.out.print("請選擇你要租憑的汽車品牌:別克,寶馬:");
            brand = scanner.next();
            String model;//車的型號

            if(brand.equals("寶馬")){
                System.out.print("請選擇你要租賃的汽車型號:X6,550i:");
                model = scanner.next();
                /*呼叫選擇汽車的方法*/
                Car car = carChooesService(cars, brand, model);

                System.out.print("請輸入您要租賃的天數:");
                day = scanner.nextInt();
                /*呼叫轎車的算錢方法*/
                carRantService(car,day);
            }else{
                System.out.print("請選擇你要租賃的汽車型號:林蔭大道,GL8:");
                model = scanner.next();
                Car car = carChooesService(cars, brand, model);
                System.out.print("請輸入您要租賃的天數:");
                day = scanner.nextInt();
                carRantService(car,day);
            }
        }else{
            /*得到客車陣列*/
            Bus[] buses = automobileManage.initBus();
            System.out.print("請選擇你要租憑的汽車品牌:金龍,金盃:");
            brand = scanner.next();
            System.out.print("請選擇你要租賃的汽車座位數:16,34:");
            String seatNumber=scanner.next();
            /*呼叫選擇客車的方法*/
            Bus bus = busChooesService(buses, brand, seatNumber);
            System.out.print("請輸入您要租賃的天數:");
            day = scanner.nextInt();
            /*呼叫計算客車租金的方法*/
            busRantService(bus,day);
        }
    }

    /*選擇汽車服務服務*/
    public Car carChooesService(Car[] cars,String brand,String model){
        for (int i = 0; i < cars.length; i++) {
            if(brand.equals(cars[i].getBrand()) &&   model.equals(cars[i].getModel())){
                return cars[i];
            }
        }
        return null;
    }
    /*選擇客車服務服務*/
    public Bus busChooesService(Bus[] buses,String brand,String seatNumber){
        for (int i = 0; i < buses.length; i++) {
            if(brand.equals(buses[i].getBrand()) &&  seatNumber.equals(buses[i].getSeatNumber())){
                return buses[i];
            }
        }
        return null;
    }

    /*計算轎車租金*/
    public void carRantService(Car car,int day){
        System.out.println("分配你的汽車牌號是:"+car.getPlate());
        if(day>150){
            System.out.println("您需要支付的租賃費用是:"+car.getRent() * day * 0.7+"元");
        }else if(day>30){
            System.out.println("您需要支付的租賃費用是:"+car.getRent() * day * 0.8+"元");
        }else if(day>7){
            System.out.println("您需要支付的租賃費用是:"+car.getRent() * day * 0.9+"元");
        }else{
            System.out.println("您需要支付的租賃費用是:"+car.getRent() * day+"元");
        }
    }
    /*計算客車租金*/
    public void busRantService(Bus bus,int day){
        System.out.println("分配你的汽車牌號是:"+bus.getPlate());
        if(day>=150){
            System.out.println("您需要支付的租賃費用是:"+bus.getRent() * day * 0.6+"元");
        }else if(day>=30){
            System.out.println("您需要支付的租賃費用是:"+bus.getRent() * day * 0.7+"元");
        }else if(day>=7){
            System.out.println("您需要支付的租賃費用是:"+bus.getRent() * day * 0.8+"元");
        }else if(day>=3){
            System.out.println("您需要支付的租賃費用是:"+bus.getRent() * day * 0.9+"元");
        }else{
            System.out.println("您需要支付的租賃費用是:"+bus.getRent() * day+"元");
        }
    }
}

測試類:

public class Test {
    public static void main(String[] args) {
        AutomobileService automobileService = new AutomobileService();
        automobileService.rentService();
    }
}

實現效果展示

在這裡插入圖片描述