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

Java入门第二季综合练习--哒哒租车系统

散华礼之弥
关注TA
已关注
手记 4
粉丝 2
获赞 15
Main.java
package practiceFinal02;

import java.util.Scanner;

public class Main {

    private static int totalPassengers = 0;
    private static int totalShipment = 0;
    private static int totalPrice = 0;

    private static Vehicle[] vehicles = null;

    public static void main(String[] args) {
        // 欢迎
        welcome();
        // 初始化并显示
        initData();
        // 租车
        rentCar();
        // 显示账单
        showBill();
    }

    /**
     * 输入判断--如果输入为非数字,则报错并重新输入
     * @return
     */
    private static int inputInt() {
        int result = 0;
        while (true) {
            Scanner scanner = new Scanner(System.in);
            try {
                //这里如果输入非int类型则会出现异常
                result = scanner.nextInt();
                break;
            } catch (Exception e) {
                //捕获异常并进行提示
                System.out.println("输入错误,请重新输入");
                scanner.close();
            }
        }
        return result;
    }

    /**
     * 统计最后的账单
     */
    private static void showBill() {
        System.out.println("您的账单");
        System.out.println("总载客量:" + totalPassengers);
        System.out.println("总载货量:" + totalShipment);
        System.out.println("总价格:" + totalPrice);
    }

    /**
     * 租车
     * 如果输入的序号不存在,不提示,但也不会统计
     */
    private static void rentCar() {
        System.out.println("请输入您要租车的数量");
        int nums = inputInt();
        //记录租车的序号
        int[] carNums = new int[nums];
        for (int i = 0; i < nums; i++) {
            System.out.println("请输入第" + (i + 1) + "辆车的序号");
            carNums[i] = inputInt();
        }

        int days = 0;
        System.out.println("请输入租车天数");
        days = inputInt();
        /*
         * 遍历vehicles数组,将序号与输入的序号进行比对
         * 如果对应,则统计信息
         */
        for (int i = 0; i < carNums.length; i++) {
            for (int j = 0; j < vehicles.length; j++) {
                if (carNums[i] == vehicles[j].getCarNum()) {
                    totalPassengers += vehicles[j].getPassengerCapacity();
                    totalShipment += vehicles[j].getShipmentCapacity();
                    totalPrice += vehicles[j].getDayPrice() * days;
                }
            }
        }
    }

    /**
     * 输出欢迎信息
     * 如果输入1以外的数字或字符串则退出
     */
    private static void welcome() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎使用哒哒租车系统");
        System.out.println("您是否要租车:1是   0否");
        String input = scanner.nextLine();
        if (input.charAt(0) != '1') {
            System.out.println("退出系统");
            scanner.close();
            System.exit(0);
        }
        System.out.println("您可租车的类型及其价目表:");
    }

    /**
     * 初始化数据
     * 并格式化输出显示
     */
    private static void initData() {
        vehicles = new Vehicle[] {
                // 序号 名称 价格 载客量 载货量
                //  运用多态技巧,生成Vehicle对象数组
                new PassengerCar(1, "奥迪A4", 500, 4), new PassengerCar(2, "马自达6", 400, 4),
                new PickUpTruck(3, "皮卡雪6", 450, 4, 2), new PassengerCar(4, "金龙", 800, 20),
                new GoodsCar(5, "松花江", 400, 4), new GoodsCar(6, "依维柯", 1000, 20), };

        System.out.println("序号  汽车名称    租金  容量");
        //格式化输出
        for (int i = 0; i < vehicles.length; i++) {
            // 载客量为0 即就是纯货车
            if (vehicles[i].getPassengerCapacity() == 0) {
                System.out.println(vehicles[i].getCarNum() + ". " + vehicles[i].getCarName() + "    "
                        + vehicles[i].getDayPrice() + "/天   载货" + vehicles[i].getShipmentCapacity()
                        + "吨");
                // 载货量为0 即就是纯客车
            } else if (vehicles[i].getShipmentCapacity() == 0) {
                //这里是为了让“金龙”对齐
                if (vehicles[i].getCarName().length() <= 2) {
                    System.out.println(vehicles[i].getCarNum() + ". " + vehicles[i].getCarName()
                            + "     " + vehicles[i].getDayPrice() + "/天 载人"
                            + vehicles[i].getPassengerCapacity() + "人");
                } else {
                    System.out.println(vehicles[i].getCarNum() + ". " + vehicles[i].getCarName() + "    "
                            + vehicles[i].getDayPrice() + "/天   载人" + vehicles[i].getPassengerCapacity()
                            + "人");
                }
                // 皮卡
            } else {
                System.out.println(vehicles[i].getCarNum() + ". " + vehicles[i].getCarName() + "    "
                        + vehicles[i].getDayPrice() + "/天   载人" + vehicles[i].getPassengerCapacity()
                        + "人    载货" + vehicles[i].getShipmentCapacity() + "吨");
            }
        }
    }
}
Vehicle.java
package practiceFinal02;

public class Vehicle {

    private int carNum;// 车辆序号
    private String carName;// 车辆名称
    private int dayPrice;// 日租金
    private int passengerCapacity;// 载客量
    private int shipmentCapacity;// 载货量

    public Vehicle() {
        super();
    }

    public int getCarNum() {
        return carNum;
    }

    public void setCarNum(int carNum) {
        this.carNum = carNum;
    }

    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public int getDayPrice() {
        return dayPrice;
    }

    public void setDayPrice(int dayPrice) {
        this.dayPrice = dayPrice;
    }

    public int getPassengerCapacity() {
        return passengerCapacity;
    }

    public void setPassengerCapacity(int passengerCapacity) {
        this.passengerCapacity = passengerCapacity;
    }

    public int getShipmentCapacity() {
        return shipmentCapacity;
    }

    public void setShipmentCapacity(int shipmentCapacity) {
        this.shipmentCapacity = shipmentCapacity;
    }

}
PassengerCar.java
package practiceFinal02;

public class PassengerCar extends Vehicle {

    //客车
    public PassengerCar(int carNum, String carName, int dayPrice, int passengerCapacity) {
        this.setCarNum(carNum);
        this.setCarName(carName);
        this.setDayPrice(dayPrice);
        this.setPassengerCapacity(passengerCapacity);
    }

}
GoodsCar.java
package practiceFinal02;

public class GoodsCar extends Vehicle {

    public GoodsCar(int carNum, String carName, int dayPrice, int shipmentCapacity) {
        this.setCarNum(carNum);
        this.setCarName(carName);
        this.setDayPrice(dayPrice);
        this.setShipmentCapacity(shipmentCapacity);
    }

}
PickUpTruck.java
package practiceFinal02;

//皮卡
public class PickUpTruck extends Vehicle {

    public PickUpTruck(int carNum, String carName, int dayPrice, int passengerCapacity, int shipmentCapacity) {
        this.setCarNum(carNum);
        this.setCarName(carName);
        this.setDayPrice(dayPrice);
        this.setPassengerCapacity(passengerCapacity);
        this.setShipmentCapacity(shipmentCapacity);
    }

}
打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP

热门评论

Main.java中第八行private static Vehicle[] vehicles = null;这一句,我知道他的作用但是不理解这样的用法,麻烦大佬能否解释一下


查看全部评论