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

java第二季,嗒嗒租车系统编写。

卷毛
关注TA
已关注
手记 2
粉丝 0
获赞 34

抽象类Car.java:

package com.oliver.rent;

public abstract class Car {

    public int carNumber; //编号
    public String carName;  //车名称
    public int rentValue; //租车价钱
    public int loadPeople; //载人数量
    public int loadGoods;//载货数量
    /**
     * @return the carNumber
     */
    public int getCarNumber() {
        return carNumber;
    }
    /**
     * @param carNumber the carNumber to set
     */
    public void setCarNumber(int carNumber) {
        this.carNumber = carNumber;
    }
    /**
     * @return the carName
     */
    public String getCarName() {
        return carName;
    }
    /**
     * @param carName the carName to set
     */
    public void setCarName(String carName) {
        this.carName = carName;
    }
    /**
     * @return the rentValue
     */
    public int getRentValue() {
        return rentValue;
    }
    /**
     * @param rentValue the rentValue to set
     */
    public void setRentValue(int rentValue) {
        this.rentValue = rentValue;
    }
    /**
     * @return the loadPeople
     */
    public int getLoadPeople() {
        return loadPeople;
    }
    /**
     * @param loadPeople the loadPeople to set
     */
    public void setLoadPeople(int loadPeople) {
        this.loadPeople = loadPeople;
    }
    /**
     * @return the loadGoods
     */
    public int getLoadGoods() {
        return loadGoods;
    }
    /**
     * @param loadGoods the loadGoods to set
     */
    public void setLoadGoods(int loadGoods) {
        this.loadGoods = loadGoods;
    }

}

PeopleCar.java:

package com.oliver.rent;

public class PeopleCar extends Car {

    public PeopleCar(int carNumber,String carName,int rentValue,int loadPeople) {
        this.carNumber = carNumber;
        this.carName = carName;
        this.rentValue = rentValue;
        this.loadPeople = loadPeople;
    }

}

PickCar.java:

package com.oliver.rent;

public class PickCar extends Car {

    public PickCar(int carNumber, String carName, int rentValue,
            int loadPeople,int loadGoods) {
        this.carNumber = carNumber;
        this.carName = carName;
        this.rentValue = rentValue;
        this.loadPeople = loadPeople;
        this.loadGoods = loadGoods;
    }
}

CargoCar.java:

package com.oliver.rent;

public class CargoCar extends Car {
    public CargoCar(int carNumber, String carName, int rentValue,
            int loadGoods) {
        this.carNumber = carNumber;
        this.carName = carName;
        this.rentValue = rentValue;
        this.loadGoods = loadGoods;
    }
}

主函数,逻辑核心代码:Initial.java

package com.oliver.rent;

import java.util.Scanner;

public class Initial {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // 定义Car的数组,并实例化相关车的对象
        Car[] cars = { new PeopleCar(1, "奥迪A4", 500, 4),
                new PeopleCar(2, "马自达6", 400, 4),
                new PickCar(3, "皮卡雪6", 450, 2, 4),
                new PeopleCar(4, "金龙", 800, 20),
                new CargoCar(5, "松花江", 400, 4),
                new CargoCar(6, "依维柯", 1000, 20) };

        // 租车系统的介绍
        System.out.println("------欢迎使用嗒嗒租车系统------");
        System.out.println("请问您是否要租车:1-是  0-否");

        // 建立Scanner对象,用来接收用户的输入
        Scanner input = new Scanner(System.in);
        int number = input.nextInt();

        if (number == 0) {
            System.out.println("不租车,退出系统");
            System.exit(0);
        } else if (number != 1) {
            System.out.println("输入有误,退出系统");
            System.exit(0);
        } else {
            System.out.println("您可租车的类型及价目表:");
            System.out.println("序号    汽车名称    租金    容量");
            System.out.println("1      奥迪A4  500元/天    载人:4人");
            System.out.println("2      马自达6  400元/天    载人:4人");
            System.out.println("3      皮卡雪6  450元/天    载人:2人    载货:4吨");
            System.out.println("4      金龙              400元/天    载人:20人");
            System.out.println("5      松花江          400元/天    载货:4吨");
            System.out.println("6      依维柯          1000元/天    载货:20吨");

            System.out.println("请输入本次要租车的数量:");
            number = input.nextInt();

            // 定义相关变量用来记录数据
            Car[] carList = new Car[number]; // 存放所输入的车型
            int loadPeoples = 0; // 用来记录装载的所有人数
            int loadGoods = 0; // 用来记录装载的物品数量
            int prices = 0; // 用来记录租车的总费用

            // 遍历循环输入要租车的序号
            for (int i = 0; i < number; i++) {
                System.out.println("请输入第" + (i + 1) + "辆车的序号:");
                int inputNumber = input.nextInt();
                carList[i] = cars[inputNumber - 1];
                prices += carList[i].getRentValue();
            }
            System.out.println("请输入租车的天数:");
            int rentDay = input.nextInt();
            prices = prices * rentDay;
            System.out.println("您的账单:");
            System.out.println("可载人的车有:");
            //遍历所有车辆,来判断载人的车
            for(int i = 0;i<number;i++){
                //如果车辆中有PeopleCar或是PickCar,则表明该车可以载人
                if(carList[i] instanceof PeopleCar  carList[i] instanceof PickCar){
                    //每次遍历叠加载人数量
                    loadPeoples += carList[i].getLoadPeople();
                    System.out.print(carList[i].getCarName()+"\t");
                }

            }
            System.out.println("共载人:"+loadPeoples+"人");
            System.out.println("可载货的车有:");
            //遍历所有车辆,来判断载货的车
            for(int i = 0;i<number;i++){
                //如果车辆中有PickCar或是CargoCar,则表明该车可以载货
                if(carList[i] instanceof PickCar  carList[i] instanceof CargoCar){
                    //每次遍历叠加载货数量
                    loadGoods += carList[i].getLoadGoods();
                    System.out.print(carList[i].getLoadGoods()+"\t");
                }
            }
            System.out.println("共载货:"+loadGoods+"吨");
            System.out.println("租车总价格:" + prices);

        }

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

热门评论

写得挺好的,有一个小小的建议,就是打印出可供租车信息的这部分代码可以在类Car中使用一个display方法,其它类重写这个方法以实现各自的输出格式。这样可以简化main方法中的代码,可扩展性也增加了

if(carList[i] instanceof PeopleCar  carList[i] instanceof PickCar){

}

这个这么写没有报错吗?

受教了受教了受教了受教了受教了受教了受教了受教了受教了

查看全部评论