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

答答租车系统(改进版)-Java面向对象的学习

慕粉3848213
关注TA
已关注
手记 5
粉丝 5
获赞 110
定义 2 个接口
  • 载人接口 IMannedCar.java

public interface IMannedCar {
    int mannedNumber();
}
  • 载货接口 ICarryFreight.java

public interface ICarryFreight {
    int carryFreightNumber();
}
定义抽象父类 Car.java,并定义成员变量和相关抽象方法

public abstract class Car {
    protected String name;
    protected int rental;

    public String getName() {
        return name;
    }

    public int getRental(){
        return rental;
    }

    public void setName(String name){
        this.name = name;
    }

    public void setRental(int rental){
        this.rental = rental;
    }
}
定义三个类,继承 Car.java,并根据不同的需求实现不同的接口
  • CarryCar.java,载货汽车,实现 ICarryFreight 接口

public class CarryCar extends Car implements ICarryFreight {
    private int carryNumber;

    public CarryCar(String name, int rental, int carryNumber) {
        this.name = name;
        this.carryNumber = carryNumber;
        this.rental = rental;
    }

    @Override
    public int carryFreightNumber() {
        return this.carryNumber;
    }

    @Override
    public String toString() {
        return getName() + "  " + getRental() + "元/天  " + "载货:" + carryFreightNumber();
    }
}
  • PersonCar.java,载人汽车,实现 IMannedCar 接口

public class PersonCar extends Car implements IMannedCar {
    private int mannedNumber;

    public PersonCar(String name, int rental, int mannedNumber) {
        this.name = name;
        this.rental = rental;
        this.mannedNumber = mannedNumber;
    }

    @Override
    public int mannedNumber() {
        return this.mannedNumber;
    }

    @Override
    public String toString() {
        return getName() + "  " + getRental() + "元/天  载人:" + mannedNumber();
    }
}
  • PickupTruck.java,皮卡类,实现2个接口

public class PickupTruck extends Car implements IMannedCar, ICarryFreight {
    private int carryNumber;
    private int mannedNumber;

    public PickupTruck(String name, int rental, int mannedNumber, int carryNumber) {
        this.name = name;
        this.rental = rental;
        this.carryNumber = carryNumber;
        this.mannedNumber = mannedNumber;
    }

    @Override
    public int mannedNumber() {
        return this.mannedNumber;
    }

    @Override
    public int carryFreightNumber() {
        return this.carryNumber;
    }

    @Override
    public String toString() {
        return getName() + "  " + getRental() + "元/天  载人:" + mannedNumber()+" 载货:" + carryFreightNumber();
    }
}
定义一个输入和转换异常类 ParseErrorException.java

public class ParseErrorException extends Exception {

    public ParseErrorException() {

    }

    public ParseErrorException(String message) {
        super(message);
    }
}
租车系统 RentCar

public class RentCar {

    private List<Car> cars;
    private List<Car> rentCars;

    public void rent() {
        rentCars = new ArrayList<>();
        System.out.println("您可租车的类型及其价目表:");
        initData();
        showRentCarList();
    }

    private void initData(){
        cars = new ArrayList<>();
        Car a4 = new PersonCar("奥迪A4",500,4);
        Car mazada6 = new PersonCar("马自达6",400,4);
        Car songhuajiang= new CarryCar("松花江",400,4);
        Car pikaxue6 = new PickupTruck("皮卡雪6",450,4,2);
        Car jinlong = new PersonCar("金龙",800,20);
        Car yiweike = new CarryCar("依维柯",1000,20);
        Car pika = new PickupTruck("皮卡2",500,4,3);
        cars.add(a4);
        cars.add(pika);
        cars.add(yiweike);
        cars.add(songhuajiang);
        cars.add(mazada6);
        cars.add(pikaxue6);
        cars.add(jinlong);
    }

    private void showRentCarList() {

        if (cars != null && cars.size() > 0) {
            System.out.println("序号  汽车名称  租金  容量");
            for (int i = 0; i < cars.size(); i++) {
                System.out.println(i + 1 + "   " + cars.get(i).toString());
            }
            int carsNumber = 0;
            boolean isCardNumber = false;
            System.out.println("请输入您要租赁车辆的数量:");
            while (!isCardNumber) {
                try {
                    carsNumber = inNumber();
                    if (carsNumber <= 0) {
                        isCardNumber = false;
                        continue;
                    }
                    isCardNumber = true;
                } catch (ParseErrorException e) {
                    isCardNumber = false;
                    System.out.println("请输入您要租赁车辆的数量:");
                }
            }

            for (int j = 0; j < carsNumber; j++) {
                System.out.println("请输入第" + (j + 1) + "辆车的序号:");
                boolean isSerial = false;
                while (!isSerial) {
                    int serial = 0;
                    try {
                        serial = inNumber();
                    } catch (ParseErrorException e) {
                        isSerial = false;
                        System.out.println("请正确的输入车辆需要");
                        continue;
                    }
                    if (serial <= 0  serial > cars.size()) {
                        System.out.println("请正确的输入车辆需要");
                        isSerial = false;
                        continue;
                    }
                    rentCars.add(cars.get(serial - 1));
                    isSerial = true;
                }

            }
            statisticsCars();
        } else {
            System.out.println("暂时没有可供租赁的车辆!");
            return;
        }

    }

    public int inNumber() throws ParseErrorException {
        Scanner scanner1 = new Scanner(System.in);
        try {
            String trim = scanner1.next().trim();
            return Integer.parseInt(trim);
        } catch (Exception e) {
            throw new ParseErrorException(e.getMessage());
        }
    }

    private void statisticsCars() {
        List<Car> carryName = new ArrayList<>();
        List<Car> personName = new ArrayList<>();
        int manned = 0;
        int carryFreight = 0;
        int rently = 0;
        if (rentCars != null && rentCars.size() > 0) {
            for (int i = 0; i < rentCars.size(); i++) {
                Car car = rentCars.get(i);
                rently += car.getRental();
                if (car instanceof PersonCar) {
                    manned += ((IMannedCar) car).mannedNumber();
                    personName.add(car);
                } else if (car instanceof CarryCar) {
                    carryFreight += ((ICarryFreight) car).carryFreightNumber();
                    carryName.add(car);
                } else {
                    manned += ((IMannedCar) car).mannedNumber();
                    carryFreight += ((ICarryFreight) car).carryFreightNumber();
                    carryName.add(car);
                    personName.add(car);
                }
            }
            System.out.println("您租赁了:" + rentCars.size() + "辆车,总金额:" + rently);
            if (carryFreight != 0) {
                System.out.println("总载货量:" + carryFreight);
                System.out.print("车辆类型为:");
                for (Car car : carryName) {
                    System.out.print(car.getName() + "  ");
                }
                System.out.println();
            }
            if (manned != 0) {
                System.out.println("总载人量:" + manned);
                System.out.print("车辆类型为:");
                for (Car car : personName) {
                    System.out.print(car.getName() + "  ");
                }
                System.out.println();
            }

        } else {
            System.out.println("租赁信息有误");
        }
    }
}
答答租车系统入口:

public class DadaRentCar {

    public static void main(String[] args) {
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车:1-是,0-否");
        boolean isRent = true;
        while (isRent) {
            Scanner scanner = new Scanner(System.in);
            String trim = scanner.next().trim();
            if (trim.equals("1")) {
                RentCar rentCar = new RentCar();
                rentCar.rent();
            } else if (trim.equals("0")) {
                isRent = false;
                System.out.println("再见!");
            } else {
                System.out.println("请输入正确的指令");
            }
        }

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

热门评论

这个牛!6666666666

这个牛!6666666666

for (Car car : carryName) { System.out.print(car.getName() + " "); 

carryName是怎么来的,我的遍历集合怎么不对,我是造你的写的

查看全部评论