为了账号安全,请及时绑定邮箱和手机立即绑定

新鲜出炉,请多多指教

package com.sun.steven.bean;
public class Car {
    private final String name;
    private final double rentFee;
    private final int personNum;
    private final int goodsNum;
    public Car(String name, double rentFee, int personNum, int goodsNum) {
        this.name = name;
        this.rentFee = rentFee;
        this.personNum = personNum;
        this.goodsNum = goodsNum;
    }
    public String getName() {
        return name;
    }
    public double getRentFee() {
        return rentFee;
    }
    public int getPersonNum() {
        return personNum;
    }
    public int getGoodsNum() {
        return goodsNum;
    }
}

package com.sun.steven.bean;

public class PersonCar extends Car {

    public PersonCar(String name, double rentFee, int personNum, int goodsNum) {
        super(name, rentFee, personNum, goodsNum);
    }

    public PersonCar(String name, double rentFee, int personNum) {
        super(name, rentFee, personNum, 0);
    }

    @Override
    public String toString() {
        return getName() + "\t" + getRentFee() + "元/天\t载人:" + getPersonNum() + "人";
    }
}

package com.sun.steven.bean;

public class GoodsCar extends Car {

    public GoodsCar(String name, double rentFee, int personNum, int goodsNum) {
        super(name, rentFee, personNum, goodsNum);
    }

    public GoodsCar(String name, double rentFee, int goodsNum) {
        super(name, rentFee, 0, goodsNum);
    }

    @Override
    public String toString() {
        return this.getName() + "\t" + this.getRentFee() + "元/天\t载货:" + this.getGoodsNum() + "吨";
    }
}

package com.sun.steven.bean;

public class PersonGoodsCar extends Car {

    public PersonGoodsCar(String name, int rentFee, int personNum, int goodsNum) {
        super(name, rentFee, personNum, goodsNum);
    }

    @Override
    public String toString() {
        return getName() + "\t" + getRentFee() + "元/天\t载人" + getPersonNum() + "人载货:" + getGoodsNum() + "吨";
    }
}

package com.sun.steven.utils;

public class BaseCheckUtils {

    /**
     * 校验一个参数是否为整数
     *
     * @param in
     * @return
     */
    public static int string2Integer(String in) {
        try {
            return Integer.valueOf(in);
        }
        catch (Exception e) {
            throw new IllegalArgumentException(in + "不是一个整数");
        }
    }

    /**
     * 校验一个整数是否为正数
     *
     * @param in
     */
    public static void isPostiveInteger(int in) {
        isRangeInteger(in, 1, Integer.MAX_VALUE);
    }

    /**
     * 判断输入是否在某个范围之内,含边界
     *
     * @param in
     * @param min
     * @param max
     */
    public static void isRangeInteger(int in, int min, int max) {
        if (in < min || in > max) {
            throw new IllegalArgumentException("输入" + in + "不在范围[" + min + "," + max + "]之内");
        }
    }
}

package com.sun.steven.menu;

import java.util.Scanner;

import com.sun.steven.bean.Car;
import com.sun.steven.bean.GoodsCar;
import com.sun.steven.bean.PersonCar;
import com.sun.steven.bean.PersonGoodsCar;
import com.sun.steven.utils.BaseCheckUtils;

public class Menu {

    public static void main(String[] str) {
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车:1是 0否");
        Scanner sca = new Scanner(System.in);
        int rentFlag = BaseCheckUtils.string2Integer(sca.next());
        BaseCheckUtils.isRangeInteger(rentFlag, 0, 1);
        if (0 == rentFlag) {
            System.out.println("欢迎下次光临!");
            System.exit(0);
        }
        System.out.println("您可租车的类型及其价格表:");
        System.out.println("序号\t汽车名称\t租金\t\t容量");
        Car[] cars = { new PersonCar("奥迪A4", 500, 4), new PersonCar("马自达6", 400, 4),
                new PersonGoodsCar("皮卡雪6", 450, 4, 2), new PersonCar("金龙", 800, 20),
                new GoodsCar("松花江", 400, 2), new GoodsCar("依维柯", 1000, 20) };
        for (int i = 0; i < cars.length; i++) {
            System.out.println((i + 1) + ".\t" + cars[i]);
        }
        System.out.println("请输入您要租汽车的数量:");
        int rentCarNum = BaseCheckUtils.string2Integer(sca.next());
        BaseCheckUtils.isPostiveInteger(rentCarNum);
        Car[] rentCars = new Car[rentCarNum];
        for (int i = 0; i < rentCarNum; i++) {
            System.out.println("请输入第" + (i + 1) + "辆 车的序号:");
            int carNo = BaseCheckUtils.string2Integer(sca.next());
            BaseCheckUtils.isRangeInteger(carNo - 1, 0, cars.length - 1);
            rentCars[i] = cars[carNo - 1];
        }
        System.out.println("请输入租车天数:");
        int rentCarDays = BaseCheckUtils.string2Integer(sca.next());
        BaseCheckUtils.isPostiveInteger(rentCarDays);
        // 计算租金和载客载货量
        System.out.println("您的账单:");
        String personCarInfo = "";
        String goodsCarInfo = "";
        int totalPerson, totalGood;
        double totalFee = 0.;
        totalPerson = totalGood = 0;
        for (int i = 0; i < rentCars.length; i++) {
            if (!(rentCars[i] instanceof GoodsCar)) {
                personCarInfo += rentCars[i].getName() + " ";
                totalPerson += rentCars[i].getPersonNum();
            }
            if (!(rentCars[i] instanceof PersonCar)) {
                goodsCarInfo += rentCars[i].getName() + " ";
                totalGood += rentCars[i].getGoodsNum();
            }
            totalFee += rentCars[i].getRentFee();
        }
        System.out.println("***可载人的车有:" + personCarInfo + "\t共载人:" + totalPerson * rentCarDays + "人");
        System.out.println("***载货的车有:" + goodsCarInfo + "\t共载货:" + totalGood * rentCarDays + "吨");
        System.out.println("***租车总价格:" + totalFee * rentCarDays + "元");
    }
}

正在回答

0 回答

举报

0/150
提交
取消
Java入门第二季 升级版
  • 参与学习       530629    人
  • 解答问题       6091    个

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

进入课程

新鲜出炉,请多多指教

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信