汽车租赁系统(Java)

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

1.Moto类:汽车抽象类 属性汽车品牌、车牌号、日租金等还有计算租金抽象方法、构造方法。

package Moto;

public abstract class Moto {
    private String brand;//品牌
    private String carId;//车牌号
    private int dayMoney;//日租金

    public Moto(String brand, String carId, int dayMoney) {
        this.brand = brand;
        this.carId = carId;
        this.dayMoney = dayMoney;
    }
    public abstract double calRent(int days);//计算租金


    public String getBrand() {
        return brand;
    }

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

    public String getCarId() {
        return carId;
    }

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

    public double getDayMoney() {
        return dayMoney;
    }

    public void setDayMoney(int dayMoney) {
        this.dayMoney = dayMoney;
    }
}

2.Bus类:客车类 特有的属性seat ,继承父类Moto的构造方法,重写计算租金的方法(calRent)。

package Moto;

public class Bus extends Moto{
    private int seat;//座位数

    public Bus(String brand, String carId, int dayMoney, int seat) {
        super(brand, carId, dayMoney);
        this.seat = seat;
    }

    @Override
    public double calRent(int days) {//重写租金方法
        double sumMoney = this.getDayMoney()*days;
        if (days >= 150){
            sumMoney *= 0.6;
        } else if (days >= 30) {
            sumMoney *= 0.7;
        }else if (days >= 7) {
            sumMoney *= 0.8;
        }else if (days >= 3) {
            sumMoney *= 0.9;
        }
        return sumMoney;
    }

    public int getSeat() {
        return seat;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }
}

3.Car类:轿车类 特有的type,继承父类Moto的构造方法,重写计算租金的方法(calRent)。

package Moto;

public class Car extends Moto{
    private String type;//车辆型号

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

    @Override
    public double calRent(int days) {//重写计算租金
        double sumMoney = this.getDayMoney()*days;
        if (days > 150){
            sumMoney *= 0.7;
        } else if (days > 30) {
            sumMoney *= 0.8;
        } else if (days >= 7) {
            sumMoney *= 0.9;
        }
        return sumMoney;
    }

    public String getType() {
        return type;
    }

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

4.MotoServer类:汽车业务类  info方法实例化对象,使系统通过不同的对象的不同属性值,确定车牌号或者日租金的属性值,再调用不同对象计算租金的方法计算所要缴纳租金。

package Moto;

public class MotoServer {
    private Moto[] motos = new Moto[8];
    public void info(){//初始化车辆信息,方便系统查找
        motos[0] = new Car("宝马", "京NY28588", 800,"X6");
        motos[1] = new Car("宝马","京CNY3284" , 600,"550i");
        motos[2] = new Car( "别克","京NT37465", 300,"林荫大道");
        motos[3] = new Car("别克","京NT96968",  600,"GL8");
        motos[4] = new Bus("金杯", "京6566754", 800,16);
        motos[5] = new Bus("金龙", "京8696997", 800,16);
        motos[6] = new Bus("金杯", "京9696996", 1500,34);
        motos[7] = new Bus("金龙", "京8696998", 1500,34);
    }
    public Moto rent(String brand,String type,int seat){//输入条件时返回汽车对象
        for(Moto moto: motos){
            if(moto instanceof Car){
                Car car = (Car) moto;
                if(car.getBrand().equals(brand)&&car.getType().equals(type)){
                    //找到了
                    return car;
                }
            }
            if(moto instanceof Bus){
                Bus bus = (Bus)moto;
                if(bus.getBrand().equals(brand)&&bus.getSeat()==seat){
                    return bus;
                }
            }
        }
        return null;
    }
}

5.Test类:测试类

package Moto;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        MotoServer motoServer = new MotoServer();
        String type = "";//型号
        String brand ="";//品牌
        int seat = 0;//座位数
        motoServer.info();
        System.out.println("********欢迎使用汽车租赁系统********");
        System.out.print("请选择车辆类型:(1. 汽车    2. 轿车)");
        int choose =scanner.nextInt();
        if (choose == 1) {
            System.out.print("请输入汽车品牌:(1. 金龙    2. 金杯)");
            int num = scanner.nextInt();
            brand = (num == 1)?"金龙":"金杯";
            System.out.print("请输入汽车的座位数:(1. 16座    2.34座) ");
            int num1 = scanner.nextInt();
            seat = (num1 == 1)?16:34;
        } else if (choose == 2){
            System.out.print("请输入轿车品牌:(1. 宝马    2. 别克)");
            int num = scanner.nextInt();
            if(num == 1){
                brand = "宝马";
                System.out.print("请输入轿车型号:(1. X6    2. 550i)");
                int num1 = scanner.nextInt();
                type = (num1 == 1)?"X6":"550i";
            } else if(num == 2){
                brand = "别克";
                System.out.print("请输入轿车型号:(1. 林荫大道    2. GL8)");
                int num1 = scanner.nextInt();
                type = (num1 == 1)?"林荫大道":"GL8";
            }
        }
        Moto moto = motoServer.rent(brand,type,seat);//返回不同的对象
        System.out.print("请输入您要租赁的天数:");
        int days = scanner.nextInt();
        double money = moto.calRent(days);//通过不同对象调用不同类中的方法
        System.out.println("给您分配的汽车为:"+moto.getCarId());
        System.out.println("您要支付的租金:"+money);

    }
}

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值