汽车租赁系统设计思路---java实现

汽车租赁系统设计思路:

1.题目要求

2.基本思路

  1. 类继承图

  1. 基本思路:
    1. 定义计算租金接口,里面有计算租金方法
    2. 定义抽象类Vehicle类,继承计算租金接口
    3. 定义Car类、Bus类,继承抽象类,让Car类和Bus类重写计算租金方法
    4. 定义AutomotiveBusiness类,是租金业务类,实现显示面板

3.具体代码

2.1 Vehicle类

  1. 此类仅仅定义抽象的属性和方法,并不实现
  2. 定义车牌号、品牌、日租金和租赁状态
package com.atguigu.rentalsystem.cars;

import com.atguigu.rentalsystem.rental.RentalPriceCal;

/**
 * 
 * 1.Vehicle是车辆的公共类,记录车辆数量、品牌、日租金和租赁状态
 *    1.1 无参构造器+有参构造器+继承RentalPriceCal接口
 * @author Jean_Leung
 * @version 1.0
 * @description TODO
 * @date 2024/3/15 14:56
 * @email cw252128385@gmail.com
 */

public abstract class Vehicle implements RentalPriceCal {
    private String carNumber;

    private String brand;

    private double dayRentalPrice;

    private boolean rentalStatus; // 租赁状态

//    abstract double getRentalPrice(Object object);


    public Vehicle(String carNumber, String brand, double dayRentalPrice, boolean rentalStatus) {
        this.carNumber = carNumber;
        this.brand = brand;
        this.dayRentalPrice = dayRentalPrice;
        this.rentalStatus = rentalStatus;
    }

    public Vehicle() {
    }


    public abstract double getRentalPrice(int days); // 可以继承接口,不实现接口

    public String getCarNumber() {
        return carNumber;
    }

    public void setCarNumber(String carNumber) {
        this.carNumber = carNumber;
    }

    public String getBrand() {
        return brand;
    }

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

    public double getDayRentalPrice() {
        return dayRentalPrice;
    }

    public void setDayRentalPrice(double dayRentalPrice) {
        this.dayRentalPrice = dayRentalPrice;
    }

    public boolean isRentalStatus() {
        return rentalStatus;
    }

    public void setRentalStatus(boolean rentalStatus) {
        this.rentalStatus = rentalStatus;
    }
}

2.2 Car 类

  • 1.Car类继承抽象类Vehicle

    ​ 1.1 私有属性:是model型号

    ​ 1.2 重写实现getRentalPrice方法

package com.atguigu.rentalsystem.cars;


import java.util.Vector;

/**
 * 1.Car类继承抽象类Vehicle
 *    1.1 私有属性:是model型号
 *    1.2 重写实现getRentalPrice方法
 * @author Jean_Leung
 * @version 1.0
 * @description TODO
 * @date 2024/3/15 15:11
 * @email cw252128385@gmail.com
 */
public class Car extends Vehicle {
    private String model;

    public Car(String carNumber, String brand, double dayRentalPrice, boolean rentalStatus, String model) {
        super(carNumber, brand, dayRentalPrice, rentalStatus);
        this.model = model;
    }

    public Car() {
    }

    public String getModel() {
        return model;
    }

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

    @Override
    public double getRentalPrice(int days) {
        if (days > 7) return this.getDayRentalPrice() * 0.9 * days;
        else if (days > 30) {
            return this.getDayRentalPrice() * 0.8 * days;
        } else if (days > 150) {
            return this.getDayRentalPrice() * 0.7 * days;
        } else {
            return this.getDayRentalPrice() * days;
        }
    }
}

2.3 Bus类

1.记录大巴座位数

​ 1.2 重写计算租金接口

package com.atguigu.rentalsystem.cars;


/**
 * 1.记录大巴座位数
 *     1.2 重写计算租金接口
 * @author Jean_Leung
 * @version 1.0
 * @description TODO
 * @date 2024/3/15 15:04
 * @email cw252128385@gmail.com
 */
public class Bus extends Vehicle {
    private int seatNumber;

    public Bus(String carNumber, String brand, double dayRentalPrice, boolean rentalStatus, int seatNumber) {
        super(carNumber, brand, dayRentalPrice, rentalStatus);
        this.seatNumber = seatNumber;
    }

    public int getSeatNumber() {
        return seatNumber;
    }

    public void setSeatNumber(int seatNumber) {
        this.seatNumber = seatNumber;
    }


    public Bus() {
    }

    @Override
    public double getRentalPrice(int days) {
        if (days >= 3) {
            return this.getDayRentalPrice() * 0.9 * days;
        } else if (days >= 7) {
            return this.getDayRentalPrice() * 0.8 * days;
        } else if (days >= 30) {
            return this.getDayRentalPrice() * 0.7 * days;
        } else if (days >= 150) {
            return this.getDayRentalPrice() * 0.6 * days;
        } else {
            return this.getDayRentalPrice() * days;
        }
    }
}

2.4 RentalPrice接口

定义计算租金接口

package com.atguigu.rentalsystem.cars;

/**
 * @author Jean_Leung
 * @version 1.0
 * @description TODO
 * @date 2024/3/15 15:31
 * @email cw252128385@gmail.com
 */
public interface RentalPriceCal {
    double getRentalPrice(int days);
}

2.5 AutomotivateBussniess类

  1. 面板显示
  2. 定义静态 Vehicle[]数组,多态数组
  3. 在静态代码块里面进行初始化
  4. 多重if-else进行比较
  5. 输入天数和输入数字校验
package com.atguigu.rentalsystem.rental;

import com.atguigu.rentalsystem.cars.Bus;
import com.atguigu.rentalsystem.cars.Car;
import com.atguigu.rentalsystem.cars.Vehicle;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

/**
 * @author Jean_Leung
 * @version 1.0
 * @description TODO
 * @date 2024/3/15 15:15
 * @email cw252128385@gmail.com
 */
public class AutomotiveBusiness {

//    private int[] rentalStatus = {1,1,1,1,1,1,1,1}; // 显示租赁状态

    static Vehicle[] vehicles; // 静态代码块最好使用静态变量

    // 初始化租赁车辆信息
    static
    {
        vehicles = new Vehicle[8];
        vehicles[0] = new Car("京NY28588","宝马",800,true,"X6");
        vehicles[1] = new Car("京CNY3284","宝马", 600,true,"550i");
        vehicles[2] = new Car("京NT37465","别克", 300,true,"林荫大道");
        vehicles[3] = new Car("京NT96968","别克",600,true,"GL8");
        vehicles[4] = new Bus("京6566754","金杯",800,true,16);
        vehicles[5] = new Bus("京8696997","金龙",800,true,16);
        vehicles[6] = new Bus("京9696996","金杯",1500,true,34);
        vehicles[7] = new Bus("京8696998","金龙",1500,true,34);
    }

    /**
     * @description: 为了显示公司租赁信息
     * @return void
     */
    public void showInformation(){
        Scanner input = new Scanner(System.in); // Scanner类
        int inputIndex = 0; // 输入数字
        String isOn = "y"; // 退出标志

        do {
            System.out.println("************欢迎光临秋名山守望者汽车租赁公司************");
            System.out.println("1、轿车   2、客车");
            System.out.println("请选择你要租赁的汽车类型:");

            inputIndex = input.nextInt();
            if (!inputIndexCheck(inputIndex)){
                System.out.println("输入有误,请重新输入");
                continue;
            }

            if (inputIndex == 1){
                System.out.println("请选择你要租赁的汽车品牌:1、别克 2、宝马");
                inputIndex = input.nextInt();
                if (!inputIndexCheck(inputIndex)){
                    System.out.println("输入有误,请重新输入");
                    continue;
                }
                if (inputIndex == 1){
                    System.out.println("请选择你要租赁的汽车类型: 1、林荫大道 2、GL8");
                    inputIndex = input.nextInt();
                    if (!inputIndexCheck(inputIndex)){
                        System.out.println("输入有误,请重新输入");
                        continue;
                    }

                    if (inputIndex == 1){
                        System.out.println("请输入你要租赁的天数:");
                        inputIndex = input.nextInt();
                        if (!dayCheck(inputIndex)){
                            System.out.println("输入有误,请重新输入");
                            continue;
                        }
                        // 校验是否有租出去
                        if ((vehicles[2].isRentalStatus())){
                            System.out.println("你需要支付的租赁费用是: "+vehicles[2].getRentalPrice(inputIndex)+"元"); // 动态绑定机制,多态数组
                            vehicles[2].setRentalStatus(false); // 将车设置为false
                        }else {
                            System.out.println("对不起,没车了,请重新租赁其他车");
                            continue;
                        }

                    }else {
                        System.out.println("请输入你要租赁的天数:");
                        inputIndex = input.nextInt();
                        if (!dayCheck(inputIndex)){
                            System.out.println("输入有误,请重新输入");
                            continue;
                        }
                        // 校验是否有租出去
                        if ((vehicles[3].isRentalStatus())){
                            System.out.println("你需要支付的租赁费用是: "+vehicles[3].getRentalPrice(inputIndex)+"元");
                            vehicles[3].setRentalStatus(false); // 将车设置为false
                        }else {
                            System.out.println("对不起,没车了,请重新租赁其他车");
                            continue;
                        }
                    }

                }else {
                    System.out.println("请选择你要租赁的汽车类型: 1、X6 2、550i");
                    inputIndex = input.nextInt();
                    if (!inputIndexCheck(inputIndex)){
                        System.out.println("输入有误,请重新输入");
                        continue;
                    }

                    if (inputIndex == 1){
                        System.out.println("请输入你要租赁的天数:");
                        inputIndex = input.nextInt();
                        if (!dayCheck(inputIndex)){
                            System.out.println("输入有误,请重新输入");
                            continue;
                        }

                        if ((vehicles[0].isRentalStatus())){
                            System.out.println("你需要支付的租赁费用是: "+vehicles[0].getRentalPrice(inputIndex)+"元");
                            vehicles[0].setRentalStatus(false); // 将车设置为false
                        }else {
                            System.out.println("对不起,没车了,请重新租赁其他车");
                            continue;
                        }

                    }else {
                        System.out.println("请输入你要租赁的天数:");
                        inputIndex = input.nextInt();
                        if (!dayCheck(inputIndex)){
                            System.out.println("输入有误,请重新输入");
                            continue;
                        }
                        if ((vehicles[1].isRentalStatus())){
                            System.out.println("你需要支付的租赁费用是: "+vehicles[0].getRentalPrice(inputIndex)+"元");
                            vehicles[1].setRentalStatus(false); // 将车设置为false
                        }else {
                            System.out.println("对不起,没车了,请重新租赁其他车");
                            continue;
                        }
                    }
                }
            }else {
                System.out.println("请选择你要租赁的汽车品牌:1、金龙 2、金杯");
                inputIndex = input.nextInt();
                if (!inputIndexCheck(inputIndex)){
                    System.out.println("输入有误,请重新输入");
                    continue;
                }
                if (inputIndex == 1){
                    System.out.println("请选择你要租赁的汽车座位数: 1、16座 2、34座");

                    inputIndex = input.nextInt();
                    if (!inputIndexCheck(inputIndex)){
                        System.out.println("输入有误,请重新输入");
                        continue;
                    }

                    if (inputIndex == 1){
                        System.out.println("请输入你要租赁的天数:");
                        inputIndex = input.nextInt();
                        if (!dayCheck(inputIndex)){
                            System.out.println("输入有误,请重新输入");
                            continue;
                        }

                        if ((vehicles[5].isRentalStatus())){
                            System.out.println("你需要支付的租赁费用是: "+vehicles[5].getRentalPrice(inputIndex)+"元");
                            vehicles[5].setRentalStatus(false); // 将车设置为false
                        }else {
                            System.out.println("对不起,没车了,请重新租赁其他车");
                            continue;
                        }

                    }else {
                        System.out.println("请输入你要租赁的天数:");
                        inputIndex = input.nextInt();
                        if (!dayCheck(inputIndex)){
                            System.out.println("输入有误,请重新输入");
                            continue;
                        }

                        if ((vehicles[7].isRentalStatus())){
                            System.out.println("你需要支付的租赁费用是: "+vehicles[7].getRentalPrice(inputIndex)+"元");
                            vehicles[7].setRentalStatus(false); // 将车设置为false
                        }else {
                            System.out.println("对不起,没车了,请重新租赁其他车");
                            continue;
                        }
                    }

                }else {
                    System.out.println("请选择你要租赁的汽车座位数: 1、16座 2、34座");

                    inputIndex = input.nextInt();
                    if (!inputIndexCheck(inputIndex)){
                        System.out.println("输入有误,请重新输入");
                        continue;
                    }

                    if (inputIndex == 1){
                        System.out.println("请输入你要租赁的天数:");
                        inputIndex = input.nextInt();
                        if (!dayCheck(inputIndex)){
                            System.out.println("输入有误,请重新输入");
                            continue;
                        }

                        if ((vehicles[4].isRentalStatus())){
                            System.out.println("你需要支付的租赁费用是: "+vehicles[4].getRentalPrice(inputIndex)+"元");
                            vehicles[4].setRentalStatus(false); // 将车设置为false
                        }else {
                            System.out.println("对不起,没车了,请重新租赁其他车");
                            continue;
                        }

                    }else {
                        System.out.println("请输入你要租赁的天数:");
                        inputIndex = input.nextInt();
                        if (!dayCheck(inputIndex)){
                            System.out.println("输入有误,请重新输入");
                            continue;
                        }

                        if ((vehicles[6].isRentalStatus())){
                            System.out.println("你需要支付的租赁费用是: "+vehicles[6].getRentalPrice(inputIndex)+"元");
                            vehicles[6].setRentalStatus(false); // 将车设置为false
                        }else {
                            System.out.println("对不起,没车了,请重新租赁其他车");
                            continue;
                        }
                    }
                }
            }

            System.out.println("是否继续输入信息:y/n");
            isOn = input.next();

        } while (isOn.equals("y"));

        System.out.println("------------------------程序退出--------------------------------");
    }

    /**
     * 输入车辆类型校验
     * @param inputIndex
     * @return boolean
     */
    public boolean inputIndexCheck(int inputIndex){
        if (inputIndex < 0 || inputIndex > 2){
            return false;
        }
        return true;
    }

    /**
     *  输入天数校验
     * @param day
     * @return
     */
    public boolean dayCheck(int day){
        if (day < 1 || day > 365){
            return false;
        }
        return true;
    }
}

2.6 Test类

package com.atguigu.rentalsystem.retaltest;

import com.atguigu.rentalsystem.rental.AutomotiveBusiness;

/**
 * @author Jean_Leung
 * @version 1.0
 * @description TODO
 * @date 2024/3/15 18:58
 * @email cw252128385@gmail.com
 */
public class CarRentalSystemTest {
    public static void main(String[] args) {
        AutomotiveBusiness automotiveBusiness = new AutomotiveBusiness();
        automotiveBusiness.showInformation();
    }
}

4. 优化思路:

  1. 思路
    1. 如果数据是动态变化应该怎么办??? --------后期使用数据库解决
    2. 多重if-else代码冗长------还没有思路
    3. 本继承代码的开闭原则并不是很好,还没想到更好的优化思路???

5. 运行结果

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值