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

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

Java第二季租車系統(tǒng) 要求的功能基本實現(xiàn)。若有不足,請多指教!

標簽:
Java

创建了两个接口IPeopleCapacity(具有载客容量)、ICargoCapacity(具有载货容量);一个父类Vehicle代表车辆具有序号、名字、租用价钱属性,三个子类Car汽车(只载人)、Truck货车(只载货)、Pickup皮卡(能载人也能载货)。程序做了部分注释。希望可以多多交流。请多指教!

IPeopleCapacity.java

package com.imooc;
//创建接口IPeopleCapacity,具有载人容量
public interface IPeopleCapacity {
    public int getPeopleCapacity();
}

ICargoCapacity.java

package com.imooc;
//创建接口ICargoCapacity,具有载货容量
public interface ICargoCapacity {
    public int getCargoCapacity();
}

Vehicle.java

package com.imooc;
//创建父类Vehicle,子类将拥有父类所有的方法和属性
public class Vehicle {
    public int id;
    public String name;
    public int rentPrice;
}

Car.java

package com.imooc;
//创建子类Car汽车,继承父类Vehicle,且遵守接口IPeopleCapacity具有载人容量
public class Car extends Vehicle implements IPeopleCapacity{
    public int peopleNum;
    public Car(int id,String name,int rentPrice,int peopleNum) {
        this.id=id;
        this.name=name;
        this.rentPrice=rentPrice;
        this.peopleNum=peopleNum;
    }
    public String toString() {
        return id+"\t"+name+"\t"+rentPrice+"元/天\t"+peopleNum+"人";
    }
    @Override
    public int getPeopleCapacity() {
        // TODO Auto-generated method stub
        return peopleNum;
    }
}

Truck.java

package com.imooc;
//创建子类Truck货车,继承父类Vehicle,且遵守接口ICargoCapacity具有载货容量
public class Truck extends Vehicle implements ICargoCapacity{
    public int cargoWeight;
    public Truck(int id,String name,int rentPrice,int cargoWeight) {
        this.id=id;
        this.name=name;
        this.rentPrice=rentPrice;
        this.cargoWeight=cargoWeight;
    }
    public String toString() {
        return id+"\t"+name+"\t"+rentPrice+"元/天\t"+cargoWeight+"吨";
    }
    @Override
    public int getCargoCapacity() {
        // TODO Auto-generated method stub
        return cargoWeight;
    }
}

Pickup.java

package com.imooc;
//创建子类Pickup皮卡,继承父类Vehicle,且遵守接口IPeopleCapacity和ICargoCapacity具,有载人容量和载货容量
public class Pickup extends Vehicle implements ICargoCapacity,IPeopleCapacity{
    public int peopleNum;
    public int cargoWeight;
    public Pickup(int id,String name,int rentPrice,int peopleNum,int cargoWeight) {
        this.id=id;
        this.name=name;
        this.rentPrice=rentPrice;
        this.peopleNum=peopleNum;
        this.cargoWeight=cargoWeight;
    }
    public String toString() {
        return id+"\t"+name+"\t"+rentPrice+"元/天\t"+peopleNum+"人,"+cargoWeight+"吨";
    }
    @Override
    public int getPeopleCapacity() {
        // TODO Auto-generated method stub
        return peopleNum;
    }
    @Override
    public int getCargoCapacity() {
        // TODO Auto-generated method stub
        return cargoWeight;
    }
}

Test.java

package com.imooc;
import java.util.Scanner;
public class Test {
    int rentSum=0;//总租金
    int peopleSum=0;//总载客数
    int cargoSum=0;//总载货数
    String peopleVehicle="",cargoVehicle="";//所租用的载人车型和载客车型
    public void method() {
        // TODO Auto-generated method stub
        //通过父类Vehicle的引用指向子类Car、Pickup、Truck的对象,存放在Vehicles[]数组中
        Vehicle[] Vehicles= {new Car(1,"奥迪A4",500,4),
                            new Car(2,"马自达6",400,4),
                            new Pickup(3,"皮卡雪6",450,4,2),
                            new Car(4,"金龙",800,20),
                            new Truck(5,"松花江",400,4),
                            new Truck(6,"依维柯",1000,20)};

        System.out.println("欢迎使用嗒嗒租车系统:");
        System.out.println("您是否要租车:1.是;2.否");
        Scanner input1=new Scanner(System.in);
        switch(input1.nextInt()){
        case 1:{
            System.out.println("您可租用的类型及其价目表:"+"\n"+"序号\t"+"车型\t"+"租金\t"+"容量");
            for(int i=0;i<Vehicles.length;i++) {//通过for遍历数组下标的方式输出所能租用的所有车辆的信息
                System.out.println(Vehicles[i]);
            }
            System.out.println("请输入您要租用的汽车数量:");
            Scanner input2=new Scanner(System.in);
            int vehicleNum=input2.nextInt();
            if(vehicleNum>0) {
                for(int j=1;j<=vehicleNum;j++) {
                System.out.println("请输入第"+j+"辆车的序号:");
                Scanner input3=new Scanner(System.in);
                int v=input3.nextInt();//将输入的车序号保存在变量v中
                if(Vehicles[v-1].getClass()==Car.class) {//判断输入的租用车型是否为Car
                    peopleVehicle+=Vehicles[v-1].name+" ";//将其车型名称加入载人车型中
                    rentSum+=Vehicles[v-1].rentPrice;//计算租金
                    peopleSum+=((Car)Vehicles[v-1]).getPeopleCapacity();//计算载客数
                }else if(Vehicles[v-1].getClass()==Truck.class){
                    cargoVehicle+=Vehicles[v-1].name+" ";//将其车型名称加入载货车型中
                    rentSum+=Vehicles[v-1].rentPrice;//计算租金
                    cargoSum+=((Truck)Vehicles[v-1]).getCargoCapacity();//计算载货数
                }else if(Vehicles[v-1].getClass()==Pickup.class) {
                    peopleVehicle+=Vehicles[v-1].name+" ";//将其车型名称加入载人车型中
                    cargoVehicle+=Vehicles[v-1].name+" ";//将其车型名称加入载货车型中
                    rentSum+=Vehicles[v-1].rentPrice;//计算租金
                    peopleSum+=((Pickup)Vehicles[v-1]).getPeopleCapacity();//计算载客数
                    cargoSum+=((Pickup)Vehicles[v-1]).getCargoCapacity();//计算载货数
                }
                }
            System.out.println("请输入租车天数:");
            Scanner input4=new Scanner(System.in);
            int day=input4.nextInt();
            if(day>0) {
                rentSum=rentSum*day; 
                System.out.println("****您的账单:\n****您租用的可载人的车:"+peopleVehicle+"\n****共可载客:"+peopleSum+"人");
                System.out.println("****您租用的可载货的车:"+cargoVehicle+"\n****共可载货:"+cargoSum+"吨");
                System.out.println("****租车总价格:"+rentSum+"元");
                System.out.println("\n******感谢使用嗒嗒租车系统,祝您生活愉快!******");
                System.exit(0);
                } else{
                System.out.println("输入的租车天数有误,系统无法验证!");
                System.exit(0);}
            }else {
                System.out.println("输入的租车数量有误,系统无法验证!");
                System.exit(0);
            }
            break;
        }
        case 2: {
            System.out.println("******感谢使用嗒嗒租车系统,祝您生活愉快!******");
            System.exit(0);
            break;}
        default:{
            System.out.println("您输入的数字有误,系统无法验证!");
            System.out.println("\n******感谢使用嗒嗒租车系统,祝您生活愉快!******");
            System.exit(0);}
            }
        }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test service=new Test();
        service.method();
    }
}

运行结果预览

點擊查看更多內(nèi)容
42人點贊

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

評論

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

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

100積分直接送

付費專欄免費學

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

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

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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

舉報

0/150
提交
取消