汽车租赁系统

 根据下面提供的信息编写一个简单的 Java 代码

        在这里使用了 封装、继承、多态

1、父类 

package CarRental;

/*
 *     我想休息
 *   不,你没有钱!
 *@ Time : 2023/7/19 15:03
 *@Author: 
 *
 *
 */
public abstract class Vehicle {
    private String vehicleId;   // 车牌号
    private String brand;       // 品牌
    private int perRent;        // 日租金

    public Vehicle() {
    }

    public Vehicle(String vehicleId, String brand, int perRent) {
        this.vehicleId = vehicleId;
        this.brand = brand;
        this.perRent = perRent;
    }

    public String getVehicleId() {
        return vehicleId;
    }

    public void setVehicleId(String vehicleId) {
        this.vehicleId = vehicleId;
    }

    public String getBrand() {
        return brand;
    }

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

    public int getPerRent() {
        return perRent;
    }

    public void setPerRent(int perRent) {
        this.perRent = perRent;
    }
    // 抽象方法:计算租金
    public abstract double clackRent(int days);
}

2、汽车类

package CarRental;

/*
 *     我想休息
 *   不,你没有钱!
 *@ Time : 2023/7/19 15:12
 *@Author: 
 *
 *      ** 汽车类
 */
public class Car extends Vehicle {
    private String type;    // 型号

    public Car() {

    }

    public Car(String vehicleId, String brand, int perRent, String type) {
        super(vehicleId, brand, perRent);
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    // 汽车租金
    public double clackRent(int days) {
        // 租金 = 日租金 * 天数
        double price = this.getPerRent() * days;
        if (days > 7 && days <= 30) {
            price = price * 0.9;
        } else if (days > 30 && days <= 150) {
            price = price * 0.8;
        } else if (days > 150) {
            price = price * 0.7;
        }
        return price;
    }
}

3、客车类

package CarRental;

/*
 *     我想休息
 *   不,你没有钱!
 *@ Time : 2023/7/19 15:19
 *@Author: 
 *
 *      ** 客车类
 */
public class Bus extends Vehicle {
    private String count;  // 座位数

    public Bus() {

    }

    public Bus(String vehicleId, String brand, int perRent, String count) {
        super(vehicleId, brand, perRent);
        this.count = count;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    @Override
    // 客车租金
    public double clackRent(int days) {
        // 租金 = 日租金 * 天数
        double price = this.getPerRent() * days;
        if (days >= 3 && days < 7) {
            price = price * 0.9;
        } else if (days >= 7 && days < 30) {
            price = price * 0.8;
        } else if (days >= 30 && days < 150) {
            price = price * 0.7;
        } else if (days >= 150) {
            price = price * 0.6;
        }
        return price;
    }
}

 4、业务类

package CarRental;

/*
 *     我想休息
 *   不,你没有钱!
 *@ Time : 2023/7/19 15:30
 *@Author: 
 *
 *      ** 业务类
 */
public class Business {
    // 汽车数组
    Vehicle[] ve = new Vehicle[8];

    // 汽车信息初始化
    public void init() {
        ve[0] = new Car("京NY28588", "宝马", 800, "X6");
        ve[1] = new Car("京CNY3284", "宝马", 600, "550i");
        ve[2] = new Car("京NT37465", "别克", 300, "林萌大道");
        ve[3] = new Car("京NT96968", "别克", 600, "GL8");
        ve[4] = new Bus("京6566754", "金杯", 800, "16座");
        ve[5] = new Bus("京8696997", "金龙", 800, "34座");
        ve[6] = new Bus("京9696996", "金杯", 1500, "16座");
        ve[7] = new Bus("京8696998", "金龙", 1500, "34座");
    }

    // 租车
    public Vehicle rent(String brand, String count, String type) {
        Vehicle v = null;
        // 遍历汽车数组,找到相应车辆返回用户
        for (Vehicle vehicle : ve) {
            if (vehicle instanceof Car) {
                Car car = (Car) vehicle;
                if (car.getBrand().equals(brand) && car.getType().equals(type)) {
                    v = car;
                    break;
                }
            } else {
                Bus bus = (Bus) vehicle;
                if (bus.getBrand().equals(brand) && bus.getCount().equals(count)) {
                    v = bus;
                }
            }
        }
        return v;
    }
}

4、测试类

package CarRental;

import java.util.Scanner;

/*
 *     我想休息
 *   不,你没有钱!
 *@ Time : 2023/7/19 16:38
 *@Author: 
 *
 *      ** 测试类
 */
public class Manage {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Business bu = new Business();
        System.out.println("*********欢迎光临启源汽车租贷公司*********");
        System.out.println("请选择您要租贷的车型:(1、轿车\t2、客车)");
        int num = sc.nextInt();

        String brand = "";    // 品牌
        String type = "";     // 型号
        String count = "";    // 座位数

        switch (num) {
            case 1:     // 租贷轿车
                System.out.println("请选择您要租贷的汽车品牌:(1、别克\t2、宝马)");
                int choose = sc.nextInt();
                if (choose == 1) {  // 别克
                    brand = "别克";
                    System.out.println("请选择您要租贷的汽车型号:(1、林萌大道\t2、GL8)");
                    type = (sc.nextInt() == 1) ? "林萌大道" : "GL8";
                } else {     // 宝马
                    brand = "宝马";
                    System.out.println("请选择您要租贷的汽车型号:(1、X6\t2、550i)");
                    type = (sc.nextInt() == 1) ? "X6" : "550i";
                }
                break;
            case 2:     // 租贷客车
                System.out.println("请选择您要租贷的汽车品牌:(1、金杯\t2、金龙)");
                int choose1 = sc.nextInt();
                if (choose1 == 1) {  // 金杯
                    brand = "金杯";
                    System.out.println("请选择您要租贷的汽车座位数:(1、16座\t2、34座)");
                    count = (sc.nextInt() == 1) ? "16座" : "34座";
                } else {     // 金龙
                    brand = "金龙";
                    System.out.println("请选择您要租贷的汽车型号:(1、16座\t2、34座)");
                    count = (sc.nextInt() == 1) ? "16座" : "34座";
                }
                break;
        }
        // 初始化汽车
        bu.init();
        // 租车
        Vehicle ve = bu.rent(brand, count, type);
        // 租车的车牌号  计算租金(根据具体返回的子类对象,调用重写后的计算租金方法)
        System.out.println("请输入您要租贷汽车的天数:");
        int days = sc.nextInt();
        double price = ve.clackRent(days);
        System.out.println("分配给您的车牌号是;" + ve.getVehicleId());
        System.out.println("您需要支付的费用是:" + price + " 元");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值