继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JAVA哒哒租车新手个人版

时光_Oliver
关注TA
已关注
手记 1
粉丝 1
获赞 2
Vehicle.java
package com.example;

public class Vehicle {
    int serial;
    String name;
    public int manLoad;
    public float goodLoad;
    public int cost;

    public int getSerial() {
        return serial;
    }

    public String getName() {
        return name;
    }

    public int getManLoad() {
        return manLoad;
    }

    public float getGoodLoad() {
        return goodLoad;
    }

    public int getCost() {
        return cost;
    }

}

Car.java
package com.example;
public class Car extends Vehicle {
    public Car(int serial, String name, int cost, int manLoad) {
        this.serial = serial;
        this.name = name;
        this.cost = cost;
        this.manLoad = manLoad;
    }
}

Pickup.java
package com.example;

public class Pickup extends Vehicle {
    public Pickup(int serial, String name, int cost, float goodLoad, int manLoad) {
        this.serial = serial;
        this.name = name;
        this.cost = cost;
        this.goodLoad = goodLoad;
        this.manLoad = manLoad;

    }
}

Truck.java
package com.example;

public class Truck extends Vehicle {
    public Truck(int serial, String name, int cost, float goodLoad) {
        this.serial = serial;
        this.name = name;
        this.cost = cost;
        this.goodLoad = goodLoad;
    }
}
RentCar.java
package com.example;
import java.util.Scanner;

public class RentCar {
    public static void main(String[] args) {

        //选择是否租车
        System.out.println("欢迎来到哒哒租车");
        System.out.print("是否进行租车?1进入,0退出:");
        while(true) {
            Scanner us_input = new Scanner(System.in);
            String user_selection = us_input.next();
            if(user_selection.equals("1")) {
                break;
            } else if(user_selection.equals("0")) {
                us_input.close();
                System.out.println("Bye");
                System.exit(0);
            } else {
                System.out.print("输入错误,请重新输入:");
            }
        }

        //进入系统,列出可选车辆及参数
        Vehicle[] cars = {
                new Car(1,"奥迪A4",500,4),
                new Car(2,"马自达6",400,4),
                new Pickup(3,"皮卡雪6",450,2,4),
                new Car(4,"金龙",800,20),
                new Truck(5,"松花江",400,4),
                new Truck(6,"依维柯",1000,20)
        };
        System.out.println("序号 车型    租金    容量");
        for(Vehicle item:cars) {
            System.out.print(item.getSerial() + "  " +
                    item.getName() + " " +
                    item.getCost() + "元/天 ");
            if(item instanceof Car) {
                System.out.println("载人:" + item.getManLoad() + "人");
            } else if(item instanceof Pickup) {
                System.out.println(
                    "载人:" + item.getManLoad() + "人 " +
                    "载货:" + item.getGoodLoad() + "吨"
                );
            } else if(item instanceof Truck) {
                System.out.println("载货:" + item.getGoodLoad() + "吨");
            }
        }

        //车辆总数选择
        int vNum;  //租车总量
        while(true) {
            System.out.print("输入要租车的数量(一次不超过5辆):");
            Scanner vn_input = new Scanner(System.in);
            vNum = vn_input.nextInt();
            if(vNum>0 && vNum<=5) {
                break;
            } else {
                System.out.println("一次租车不能超过5辆");
            }
        }

        //具体车辆选择,及统计
        int allCost = 0;
        int allManLoad = 0;
        int allGoodLoad = 0;
        String manLoadList = "";  //载人说明文字
        String goodLoadList = "";  //载货说明文字
        for(int i=1; i<vNum + 1; i++) {
            int vSerial;  //用户所选车辆编号
            while(true) {
                System.out.print("输入第" + i + "辆车的编号:");
                Scanner vs_input = new Scanner(System.in);
                vSerial = vs_input.nextInt();
                if(vSerial>0 && vSerial<=cars.length) {
                    break;
                } else {
                    System.out.println("请输入有效的序号");
                }
            }

            allCost += cars[i-1].cost;
            if(cars[vSerial-1].manLoad > 0) {
                allManLoad += cars[vSerial-1].manLoad;
                manLoadList += " " + cars[vSerial-1].getName();
            }
            if(cars[vSerial-1].goodLoad > 0) {
                allGoodLoad += cars[vSerial-1].goodLoad;
                goodLoadList += " " + cars[vSerial-1].getName();
            }
        }

        //租车天数
        System.out.print("请输入租车天数:");
        Scanner rd_input = new Scanner(System.in);
        int rentDay = rd_input.nextInt();

        //根据所选车辆列表进行计算,得出结果
        System.out.println("您的账单:");
        System.out.println("共租车:" + vNum + "辆");
        System.out.println("***可载人的车有:");
        System.out.println(manLoadList + " 共载人:" + allManLoad + "人");
        System.out.println("***可载货的车有:");
        System.out.println(goodLoadList + " 共载货:" + allGoodLoad + "吨");
        System.out.print("***租车总价格:" + allCost*rentDay + "元");
    }
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP