手记

JAVA第二季 答答租车系统 欢迎各位提提意见(实现封装,继承,多态,接口)

运行结果

测试类

package com.imooc.test;

import com.imooc.model.Car;
import com.imooc.model.CarSystem;
import com.imooc.model.PassengerCar;
import com.imooc.model.PickupCar;
import com.imooc.model.TruckCar;

public class Test {

    public static void main(String[] args) {

        Car[] carArr = { new PassengerCar(1, "奥迪A4", 500, 4), new PassengerCar(2, "马自达6", 400, 4),
                new PickupCar(3, "皮卡雪6", 450, 4, 2), new PassengerCar(4, "金龙", 800, 20), new TruckCar(5, "松花江", 400, 4),
                new TruckCar(6, "依维河", 1000, 20) };
        CarSystem carSystem = new CarSystem();
        carSystem.welcome();
        carSystem.showCarTable(carArr);
        carSystem.selectCar(carArr);
        carSystem.inptRentDay();
        System.out.println("您的账单");
        carSystem.showPeopleCarryCar();
        carSystem.showGoodsCarryCar();
        carSystem.showPrice();

    }

}

租车系统类

package com.imooc.model;

import java.util.Scanner;

public class CarSystem {
    Car[] carToRent;
    int rentDay;

    //欢迎菜单
    public void welcome() {
        System.out.println("欢迎使用达达租车系统:\n您是否要租车:1是 0否");
        // 如果输出结果不是1,退出系统
        if (this.getValue() != 1) {
            System.out.println("欢迎再来!");
            System.exit(1);
        }
    }

    //显示租车类型和价格
    public void showCarTable(Car[] car) {
        System.out.println("您可租车的类型及其价目表:\n序号    租车名称        租金                   容量");
        for (int i = 0; i < car.length; i++) {
            System.out.println(car[i]);
        }
    }

    //数入租车的数量和序号
    public void selectCar(Car[] existCar) {

        System.out.println("请输入您要租车的数量");
        int num=this.getValue();
        if(!(num>=1&&num<=6)){
            System.out.println("请输入0-6的整数");
            num=this.getValue();
        }

        Car[] carToRent = new Car[num];
        for (int i = 0; i < num; i++) {
            System.out.println("请输入第" + (i+1) + "辆要预定车的序号");
            carToRent[i] = existCar[this.getValue()-1];
        }
        this.setCarToRent(carToRent);
    }

    //数入要租车的天数
    public void inptRentDay(){
        System.out.println("请输入租车天数");
        this.setRentDay(this.getValue());
    }

    //显示预定的可载人的车信息
    public void showPeopleCarryCar(){
        int passengerSum = 0;
        System.out.println("**可载人的车有:");
        for (int i = 0; i < this.carToRent.length; i++) {
            if (this.carToRent[i] instanceof PassengerCar) {    
                System.out.print(" " + this.carToRent[i].getName());
                passengerSum = passengerSum + ((PassengerCar) this.carToRent[i]).getPeopleCapacity();

            } else if (this.carToRent[i] instanceof PickupCar) {
                System.out.print(" " + this.carToRent[i].getName());
                passengerSum = passengerSum + ((PickupCar) this.carToRent[i]).getPeopleCapacity();

            }
        }
        System.out.println(" 共载人:" + passengerSum + "人!");
    }

    //显示预定的可载物的车信息
    public void showGoodsCarryCar(){
        int LoadSum = 0;
        System.out.println("**可载货的车有:");
        for (int i = 0; i < this.carToRent.length; i++) {
            if (this.carToRent[i] instanceof TruckCar) {
                System.out.print(" " + this.carToRent[i].getName());
                LoadSum = LoadSum + ((TruckCar) this.carToRent[i]).getGoodsCapacity();
            } else if (this.carToRent[i] instanceof PickupCar) {
                System.out.print(" " + this.carToRent[i].getName());
                LoadSum = LoadSum + ((PickupCar) this.carToRent[i]).getGoodsCapacity();
            }
        }
        System.out.println(" 共载货:" + LoadSum + "吨!");
    }

    //显示预定车辆的总租金
    public void showPrice() {
        double rent=0;
        for (int i = 0; i < this.carToRent.length; i++){
            rent=rent+carToRent[i].getPrice();
        }
        System.out.println("**租车总价格:"+rent*this.getRentDay());
    }

    //从键盘获取输入的数字
    public int getValue() {
        Scanner sc = new Scanner(System.in);
        int value = sc.nextInt();
        return value;
    }

    public Car[] getCarToRent() {
        return carToRent;
    }

    public void setCarToRent(Car[] carToRent) {
        this.carToRent = carToRent;
    }
    public int getRentDay() {
        return rentDay;
    }

    public void setRentDay(int rentDay) {
        this.rentDay = rentDay;
    }
}

父类:车类

package com.imooc.model;

public class abstract Car {
    private String name;
    private int carNo;
    private double price;

    public Car() {

    }

    public Car(int carNo, String name, double price) {
        this.setCarNo(carNo);
        this.setName(name);
        this.setPrice(price);
    }

    public String getName() {
        return name;
    }

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

    public int getCarNo() {
        return carNo;
    }

    public void setCarNo(int carNo) {
        this.carNo = carNo;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

}

子类1:客车类

package com.imooc.model;

public class PassengerCar extends Car implements IPeopleCarry {
    private int peopleCapacity;

    public PassengerCar() {

    }

    public PassengerCar(int carNo, String name, double price, int peopleCapacity) {
        super(carNo, name, price);
        this.setPeopleCapacity(peopleCapacity);
    }

    @Override
    public String toString() {
        String str=this.getCarNo()+"   "+this.getName()+" "+this.getPrice()+"元/天   载人"+this.getPeopleCapacity();
        return str;
    }

    public int getPeopleCapacity() {
        return peopleCapacity;
    }

    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }

}

子类2:货车类

package com.imooc.model;

public class TruckCar extends Car {
    private int goodsCapacity;

    public TruckCar(){

    }
    public TruckCar(int carNo,String name, double price, int goodsCapacity){
        super(carNo,name,price);
        this.setGoodsCapacity(goodsCapacity);
    }
    @Override
    public String toString() {
        String str=this.getCarNo()+"   "+this.getName()+" "+this.getPrice()+"元/天   载物"+this.getGoodsCapacity();
        return str;
    }
    public int getGoodsCapacity() {
        return goodsCapacity;
    }

    public void setGoodsCapacity(int goodsCapacity) {
        this.goodsCapacity = goodsCapacity;
    }
}

子类3:皮卡车类

package com.imooc.model;

public class PickupCar extends Car implements IGoodsCarry, IPeopleCarry {
    private int peopleCapacity;
    private int goodsCapacity;

    public PickupCar() {

    }

    public PickupCar(int carNo, String name, double price, int peopleCapacity, int goodsCapacity) {
        super(carNo, name, price);
        this.setPeopleCapacity(peopleCapacity);
        this.setGoodsCapacity(goodsCapacity);
    }
    @Override
    public String toString() {
        String str=this.getCarNo()+"   "+this.getName()+" "+this.getPrice()+"元/天   载人"
                        +this.getPeopleCapacity()+" 载物:"+this.getGoodsCapacity();
        return str;
    }
    public int getPeopleCapacity() {
        return peopleCapacity;
    }

    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }

    public int getGoodsCapacity() {
        return goodsCapacity;
    }

    public void setGoodsCapacity(int goodsCapacity) {
        this.goodsCapacity = goodsCapacity;
    }

}

载物接口

package com.imooc.model;

public interface IGoodsCarry {
    public int getGoodsCapacity();
}

载人接口

package com.imooc.model;

public interface IPeopleCarry {
    public int getPeopleCapacity();

}
1人推荐
随时随地看视频
慕课网APP