Java面向对象实现汽车租赁系统

 更新时间:2022年02月24日 10:21:48   作者:勤奋的小镇青年、  
这篇文章主要为大家详细介绍了Java面向对象实现汽车租赁系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现汽车租赁系统的具体代码,供大家参考,具体内容如下

父类Vehicle

public abstract class Vehicle {
    private String num;
    private String brand;
    private  double rent;

    //重写equals方法
    public abstract boolean equals(Vehicle v);

    //计算费用
    public abstract double cost(int days,double rent);

    //3个参数的有参构造
    public Vehicle(String num, String brand, double rent) {
        this.num = num;
        this.brand = brand;
        this.rent = rent;
    }

    //无参构造
    public Vehicle() {
    }

    //get,set方法
    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public String getBrand() {
        return brand;
    }

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

    public double getRent() {
        return rent;
    }

    public void setRent(double rent) {
        this.rent = rent;
    }
}

子类Cars

public class Cars extends Vehicle{
    private String type;

    //重写父类equals方法
    @Override
    public boolean equals(Vehicle v) {
        if(v instanceof Cars){
            Cars c=(Cars) v;
            return this.getBrand().equals(c.getBrand())&&this.getType().equals(c.getType());
        }
        return false;
    }

    //重写父类费用方法
    @Override
    public double cost(int days,double sent) {
        if (days>150){
            return days*sent*0.7;
        }else if (days>30){
            return days*sent*0.8;
        }else if (days>7){
            return days*sent*0.9;
        }else {
            return days*sent;
        }
    }

    //无参构造
    public Cars() {
    }

    //有参构造
    public Cars(String num, String brand, double rent, String type) {
        super(num, brand, rent);
        this.type = type;
    }

    //2个有参构造的方法
    public Cars(String brand,String type){
        super.setBrand(brand);
        this.type = type;
    }

    public String getType() {
        return type;
    }

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

子类Bus

public class Bus extends Vehicle{
    private int seat;

    //重写父类的equals方法
    @Override
    public boolean equals(Vehicle v) {
        if(v instanceof Bus){
            Bus b=(Bus) v;
            return this.getBrand().equals(b.getBrand())&&this.getSeat()==b.getSeat();
        }
        return false;
    }

    //重写父类费用方法
    @Override
    public double cost(int days,double rent) {
        if (days>=150){
            return days*rent*0.6;
        }else if (days>=30){
            return days*rent*0.7;
        }else if (days>=7){
            return days*rent*0.8;
        }else if (days>=3){
            return days*rent*0.9;
        }else {
            return days*rent;
        }
    }

    //2个参数的有参构造
    public Bus(String brand,int seat){
        super.setBrand(brand);
        this.seat=seat;
    }

    //子类有参构造
    public Bus(String num, String brand, double rent, int seat) {
        super(num, brand, rent);
        this.seat = seat;
    }

    //子类无参构造
    public Bus(){}

    //子类get set 方法
    public int getSeat() {
        return seat;
    }

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

汽车业务类VehicleServicer

public class VehicleServicer {

    public static List initVehicle(){
        Vehicle v1=new Bus("京6566754","金杯",800,16);
        Vehicle v2=new Bus("京9696996","金杯",1500,34);
        Vehicle v3=new Bus("京8696997","金龙",800,16);
        Vehicle v4=new Bus("京8696998","金龙",1500,34);
        Vehicle c1 =new Cars("京NT37465","别克",300,"林荫大道");
        Vehicle c2 =new Cars("京9696996","别克",600,"GLB");
        Vehicle c3 =new Cars("京8696997","宝马",800,"X6");
        Vehicle c4 =new Cars("京8696998","宝马",600,"550i");

        //先装入数组中
        Vehicle[] ve = {v1,v2,v3,v4,c1,c2,c3,c4};

        //将数组转换成集合
        List<Vehicle> vehicles = Arrays.asList(ve);
        return vehicles;
    }


}

测试类Test

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("**********欢迎光临秋明山守望者汽车租赁公司**********");
        System.out.println("1.轿车    2.客车");
        System.out.print("请选择你要租赁的汽车类型:");
        int type = sc.nextInt();
        //桥车
        Vehicle ve;
        String brand;
        if(type==1){
            System.out.print("请选择你要租赁的汽车品牌(1.别克  2.宝马):");
            int pinpai = sc.nextInt();
            String model=pinpai==1?"别克":"宝马";
            if(pinpai==1){
                System.out.print("请输入你要租赁的汽车类型(1.X6  2.550i):");
                int leixin = sc.nextInt();
                brand=leixin==1?"林荫大道":"GL8";
            }else {
                System.out.print("请输入你要租赁的汽车类型(1.X6  2.550i):");
                int leixin = sc.nextInt();
                brand=leixin==1?"X6":"550i";
            }
             ve = new Cars(model, brand);

        }else {//客车
            int seat;
            System.out.print("请选择你要租赁的汽车品牌(1.金龙  2.金杯):");
            int pinpai = sc.nextInt();
            String s=pinpai==1?"金龙":"金杯";
            System.out.print("请选择你要租赁的汽车座位数(1.16座 2.34座):");
            int z = sc.nextInt();
            seat =z==1?16:34;
            ve = new Bus(s, seat);
        }
        //根据选好的车型,输出车牌和总价
        List<Vehicle> list = VehicleServicer.initVehicle();
        for (Vehicle v:list) {
            if(ve.equals(v)){
                System.out.print("请输入你要租赁的天数:");
                int days = sc.nextInt();
                System.out.println("分配给您的汽车牌号是:"+v.getNum());
                System.out.println("您需要支付的租赁费用是:"+v.cost(days,v.getRent()));
            }
        }

    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 关于Spring中一级缓存、二级缓存和三级缓存的那些事

    关于Spring中一级缓存、二级缓存和三级缓存的那些事

    Spring解决循环依赖的核心思想在于提前曝,下面这篇文章主要给大家介绍了关于Spring中一级缓存、二级缓存和三级缓存的那些事,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-02-02
  • 详解Java爬虫利器Jsoup

    详解Java爬虫利器Jsoup

    Jsoup是一款Java语言开发的HTML解析器,用于解析HTML文档以及对HTML文档进行操作,处理等,本文就将详细给大家介绍一下Java中的爬虫利器Jsoup,感兴趣的同学可以参考一下
    2023-06-06
  • 阿里云发布 Spring Boot 新脚手架工程

    阿里云发布 Spring Boot 新脚手架工程

    这篇文章主要介绍了阿里云发布 Spring Boot 新脚手架的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,可以参考下
    2020-04-04
  • Java发送http请求的示例(get与post方法请求)

    Java发送http请求的示例(get与post方法请求)

    这篇文章主要介绍了Java发送http请求的示例(get与post方法请求),帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2021-01-01
  • 利用SpringBoot和LiteFlow解锁复杂流程

    利用SpringBoot和LiteFlow解锁复杂流程

    随着业务的复杂化,企业需要更加高效、便捷地管理自己的业务流程,这就需要借助一些流程引擎实现,今天,我们就来介绍一种基于Java语言开发的轻量级工作流引擎——LiteFlow,以及如何在Spring Boot框架中集成它,从而提高企业的工作效率和开发效率
    2023-06-06
  • Spring Boot Web 静态文件缓存处理的方法

    Spring Boot Web 静态文件缓存处理的方法

    本篇文章主要介绍了Spring Boot Web 静态文件缓存处理的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • java设计模式系列之装饰者模式

    java设计模式系列之装饰者模式

    这篇文章主要为大家详细介绍了java设计模式之装饰者模式,装饰者模式是一种结构式模式,感兴趣的朋友可以参考一下
    2016-02-02
  • 使用Java实现2048小游戏代码实例

    使用Java实现2048小游戏代码实例

    这篇文章主要介绍了使用Java实现2048小游戏代码实例,2048 游戏是一款益智类游戏,玩家需要通过合并相同数字的方块,不断合成更大的数字,最终达到2048,游戏规则简单,但挑战性很高,需要玩家灵活运用策略和计算能力,本文将使用Java代码实现,需要的朋友可以参考下
    2023-10-10
  • Spring Retry重试框架的使用讲解

    Spring Retry重试框架的使用讲解

    重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次。用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有,感兴趣的可以了解一下
    2022-10-10
  • IDEA2022 提示更新 TKK失败请检查网络连接的问题

    IDEA2022 提示更新 TKK失败请检查网络连接的问题

    这篇文章主要介绍了IDEA2022 提示:更新 TKK 失败,请检查网络连接,本文给大家分享解决方案,对idea2022提示更新TKK失败感兴趣的朋友跟随小编一起看看吧
    2022-11-11

最新评论