Java面向对象——汽车租赁系统项目(超详细!!!!)

题目

某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示

从题目抽象出的类与类的属性:

汽车类:车牌号、品牌、日租金
客车类:车牌号、品牌、日租金、座位数
轿车类:车牌号、品牌、日租金、型号
汽车业务类:忽略
汽车租赁管理类:忽略

从题目找出方法:

1.计算租金

2.租赁  getRent()

3.展示信息(初始化信息)info()

车类(父类):

为什么要把父类设为抽象类?

  • 抽象类可以包含抽象方法,这些方法没有具体实现。所有非抽象的子类必须实现这些抽象方法,从而保证子类实现了某些关键功能,比如calculateRent方法。

为什么要在子类中计算租金?

  • 每个 Car 对象自己知道自己的租赁费用,因此 Car 类内部可以直接计算租赁费用。
public abstract class Car {
    private String carId;
    private  String brand;
    private  Integer dailyMoney;

    public abstract double calculateRent(int days);

    public Car(String carId, String brand, Integer dailyMoney) {
        this.carId = carId;
        this.brand = brand;
        this.dailyMoney = dailyMoney;
    }

    public Car() {

    }


    public String getCarId() {
        return carId;
    }

    public void setCarId(String carId) {
        this.carId = carId;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Integer getDailyMoney() {
        return dailyMoney;
    }

    public void setDailyMoney(Integer dailyMoney) {
        this.dailyMoney = dailyMoney;

    }


}

轿车类(子类):

轿车独有的方法是model类型

/**
 * 轿车类
 */
public class Sedan extends  Car{
    private String model;  //型号
    public  double calculateRent(int days){

        double sum = this.getDailyMoney()*days;
        if (days>7&&days<30){
            sum = 0.9*sum;
        } else if (days>30&&days<150) {
            sum = 0.8*sum;

        } else if (days>150) {
            sum = 0.7*sum;

        }
        return  sum;


    }

    public Sedan(String carId, String brand, Integer dailyMoney, String model) {
        super(carId, brand, dailyMoney);
        this.model = model;
    }


    public Sedan() {

    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }



}

客车类(子类):

客车独有的方法是seatCount座位数

/**
 * 客车类
 */
public class BusCar extends  Car{

private Integer  seatCount;  //    座位数

    public double calculateRent(int days){
        double sum = this.getDailyMoney()*days;
        if (days>7&&days<=30){
            sum = 0.8*sum;
        } else if (days>30&&days<=150) {
            sum = 0.7*sum;

        } else if (days>150) {
            sum = 0.6*sum;

        } else if (days>3&&days<=7) {
            sum = 0.9*sum;

        }
        return sum;

    }

    public BusCar(String carId, String brand, Integer dailyMoney, Integer seatCount) {
        super(carId, brand, dailyMoney);
        this.seatCount = seatCount;
    }

    public Integer getSeatCount() {
        return seatCount;
    }

    public void setSeatCount(Integer seatCount) {
        this.seatCount = seatCount;
    }

    public BusCar() {

    }


}

汽车业务类:

info方法是做什么的?

  •     当我们创建出Carwork汽车业务类时,里面没有小汽车,我们需要实例化出来几个车子供顾客选择。这个对象包括车牌,品牌,日租金,型号

getRent方法的作用:

  1. 当顾客在测试方法中选择了我们需要的品牌和型号,汽车业务类需要给顾客返回一个符合条件的汽车实例给顾客;getRent方法的返回值类型是car(父类),因为需要给顾客返回的汽车可能是轿车或客车。
  2. 这个数组是Car类型的  private Car motos[] = new Car[8]   ,  java允许父类的数组存储所有子类的对象,当传入brand,model,seatcount时,会先遍历数组,然后判断它们的类型,然后转成相应的类型再比较,因为得先有一个实例才能够使用get方法比较传入的参数是否符合条件,然后返回符合条件的轿车或汽车。

    

public class CarWork {
    private Car motos[] = new Car[8];

    //    初始化车辆信息
    public void info() {

        motos[0] = new Sedan("京TB54467", "宝马", 800, "X6");
        motos[1] = new Sedan("京TB54467", "宝马", 600, "550i");
        motos[2] = new Sedan("京TB54467", "别克", 800, "林荫大道");
        motos[3] = new Sedan("京TB54467", "别克", 300, "GL8");
        motos[4] = new BusCar("京6566754", "金杯", 800, 16);
        motos[5] = new BusCar("京8696997", "金龙", 800, 16);
        motos[6] = new BusCar("京9696996", "金杯", 1500, 34);
        motos[7] = new BusCar("京8696998", "金龙", 1500, 34);

    }

    /**
     * 租赁汽车
     *
     * @param brand     品牌
     * @param model     型号
     * @param seatCount 座位数
     * @return 返回要租赁汽车对象
     */
    public Car getRent(String brand, String model, int seatCount) {

//        获得信息
        for (Car cars : motos) {
            if (cars instanceof Sedan) {
                Sedan sedan=(Sedan)cars;
                if (sedan.getBrand().equals(brand) && sedan.getModel().equals(model)) {


                    return sedan;
                }
            } else if (cars instanceof BusCar) {
                BusCar buscar=(BusCar)cars;
                    if (buscar.getBrand().equals(brand) && buscar.getSeatCount()==seatCount) {
                        return buscar;

                }


            }

        }
        return null;

    }
}

测试方法:

  1. 我们要传入getRent方法的三个变量因为是局部变量所以brand,model,seatcount要赋初始值,new一个carWork对象来调用info用来初始化(给车行来点车供选择),然后就根据提示选择需要的条件
  2. 用Car car = carWork.getRent(参数)来接收getRent返回的对象(车子分配的车子)然后通过对象调用得知需要花费的钱数
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
         String brand = "";
        String model = "";
        int seatCount=0;
        CarWork carWork = new CarWork();
        carWork.info();//千万别忘了初始化汽车信息!!!!!!!!!
        Scanner scanner = new Scanner(System.in);
        System.out.println("——————————————————————欢迎来到蛋蛋租车行——————————————————————");
        System.out.println("请选择您要租的车(1.轿车 2.客车)");
        int choice = scanner.nextInt();
        if (choice==1){
            System.out.println("请选择您要租的轿车品牌(1.宝马 2.别克)");
            int num=scanner.nextInt();
            if(num==1){
                brand ="宝马";
                System.out.println("请选择您要租的轿车型号(1.X6 2.550i)");
                model =scanner.nextInt()==1?"X6":"550i";


            } else if (num==2) {
                brand ="别克";
                System.out.println("请选择您要租的轿车型号(1.X6 2.550i)");
                model =scanner.nextInt()==1?"林荫大道":"GL8";
            }

        } else if (choice==2) {
            System.out.println("请选择您要租的客车品牌(1.金杯 2.金龙)");
            int num=scanner.nextInt();
            if (num==1){
                brand ="金杯";
                System.out.println("请选择您要租的客车座位数(1.16座 2.34座)");
                seatCount =scanner.nextInt()==1?16:32;

            } else if (num==2) {
                brand ="金龙";
                System.out.println("请选择您要租的客车座位数(1.16座 2.34座)");
                seatCount =scanner.nextInt()==1?16:32;

            }

        }

        Car car = carWork.getRent(brand, model, seatCount); // 这将返回一个有效的Sedan对象
        System.out.println("给您分配的汽车为:"+car.getCarId());
        System.out.println("您要租多少天");
        int days = scanner.nextInt();

        double sum = car.calculateRent(days);
        System.out.println("您花费的租车总金额为:"+sum);

    }
}

本题包含了面向对象的前半部分知识点

  1. 重写
  2. 多态
  3. 继承

其中多态中还包括数组多态,多态的传参,多态的返回值,需要大家多多留意。

  • 21
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值