java编写汽车租赁系统

 更新时间:2022年02月24日 11:19:53   作者:꧁এ悲宸๓₯㎕  
这篇文章主要为大家详细介绍了java编写汽车租赁系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

题目要求:

1,汽车租赁信息表如下:

2,类和属性:

3,运行效果:

效果实现:

代码实现:

1,车类:

package homework.exam;

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

    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;
    }

    public Vehicle() {
    }
    //含参构造
    public Vehicle(String num, String brand, double rent) {
        this.num = num;
        this.brand = brand;
        this.rent = rent;
    }

    @Override
    public String toString() {
        return "汽车{" +
                "车牌号='" + num + '\'' +
                ", 品牌='" + brand + '\'' +
                ", 日租金=" + rent +
                '}';
    }
    public abstract double totalmoney(int days , double rent);
    public abstract boolean equals(Vehicle o);
}

2,汽车类:

package homework.exam;

public class Cars extends Vehicle{
    private String type;


    public String getType() {
        return type;
    }

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

    public Cars(String brand,String type) {
        this.type = type;

    }

    public Cars(String num, String brand, double rent, String type) {
        super(num, brand, rent);
        this.type = type;
    }

    @Override
    public String toString() {
        return "Cars{" +
                "type='" + type + '\'' +
                '}';
    }

    //计算小汽车的总租金
    @Override
    public double totalmoney(int days, double rent) {
        if (days>7){
            return days*rent*0.9;
        }else if (days>30){
            return days*rent*0.8;
        }else if (days>150){
            return days*rent*0.7;
        }
        return days*rent;
    }
    //重写equals方法
    @Override
    public boolean equals(Vehicle o) {
        if (o instanceof Cars){
            Cars cars= (Cars) o;
            return this.getType().equals(cars.getType())&&this.getBrand().equals(o.getBrand());
        }
        return false;
    }
}

3,客车类:

package homework.exam;

public class Bus extends Vehicle {
    private String seat;

    public String getSeat() {
        return seat;
    }

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

    public Bus(String num, String brand, double rent, String seat) {
        super(num, brand, rent);
        this.seat = seat;
    }

    //计算客车的租金
    @Override
    public double totalmoney(int days, double rent) {
        if (days>=3){
            return days*rent*0.9;
        }else if (days>=7){
            return days*rent*0.8;
        }else if (days>=30){
            return days*rent*0.7;
        }else if (days>=150){
            return days*rent*0.6;
        }
        return days*rent;
    }
    //重写equals方法
    @Override
    public boolean equals(Vehicle o) {
        return false;
    }
}

4,车辆管理类:

package homework.exam;

public class CarRent {
    //创建汽车数组,将汽车的信息放在数组中
    public Cars[] carMake(){
        Cars c1 = new Cars("京NY28588", "宝马", 800, "x6");
        Cars c2 = new Cars("京CNY3284", "宝马", 600, "550i");
        Cars c3 = new Cars("京NT37465", "别克", 300, "林荫大道");
        Cars c4 = new Cars("京NT96928", "别克", 600, "GL8");
        Cars[] arr1 ={c1,c2,c3,c4};
        return arr1;
    }
    //创建客车数组,将汽车的信息放在数组中
    public Bus[] busMake(){
        Bus b1 = new Bus("京6566754", "金杯", 800, "16座");
        Bus b2 = new Bus("京8696667", "金龙", 800, "16座");
        Bus b3 = new Bus("京9696996", "金杯", 1500, "34座");
        Bus b4 = new Bus("京8696998", "金龙", 1500, "34座");
        Bus[] arr2={b1,b2,b3,b4};
        return arr2;
    }
}

5,业务服务类:

package homework.exam;

import java.util.Scanner;

public class CarService {
    public void rentcar(){
        System.out.println("**********欢迎光临秋名山守望者汽车租赁公司**********");
        Scanner sc = new Scanner(System.in);
        System.out.println("1,轿车  2,客车");
        System.out.print("请输入您要租赁的汽车类型:");
        int i = sc.nextInt();
        CarRent carRent = new CarRent();    //创建车库对象
        Cars[] cars = carRent.carMake();    //拿到轿车数组对象
        Cars car=null;
        Bus[] buses = carRent.busMake();    //拿到客车数组对象
        Bus bus=null;
        //判断用户选择的车型
        if (i==1){
            System.out.print("请选择你要租赁的汽车品牌:(1,别克  2,宝马)");
            int i1 = sc.nextInt();
            if (i1==1){
                System.out.print("请输入你要租赁的汽车类型:(1,林荫大道 2,GL8 )");
            }else {
                System.out.print("请输入你要租赁的汽车类型:(1,x6 2,550i )");
            }
            String i2 = sc.next();
            //遍历汽车数组,拿到用户选择的汽车
            for (int j = 0; j < cars.length; j++) {
                if (cars[j].getType().equals(i2)){  //当选择的车的类型与数组中的匹配时
                    car=cars[j];        //将车赋值给car
                    break;
                }
            }
            System.out.print("请输入你要租赁的天数:");
            int days = sc.nextInt();
            System.out.print("分配给你的汽车牌号是:");
            System.out.println(car.getNum());   //获取汽车的车牌
            double totalmoney =0;           //调用total
            totalmoney = car.totalmoney(days, car.getRent());   //计算用户的租金
            System.out.print("你需要支付的租赁分费用是:");
            System.out.print(totalmoney);

        }else if (i==2){
            System.out.print("请选择你要租赁的汽车品牌:(1,金龙  2,金杯)");
            String i2 = sc.next();
            System.out.print("请输入你要租赁的汽车座位数:(1,16座  2,34座)");
            String i3 = sc.next();
            //遍历客车数组,拿到用户选择的客车
            for (int j = 0; j < buses.length; j++) {
                //当输入的客车的车牌和座位与数组中的相等,就选出用户选择的车
                if (buses[j].getBrand().equals(i2)&&buses[j].getSeat().equals(i3)){
                    bus=buses[j];   //将选择的车辆赋值给bus
                    break;
                }
            }
            System.out.print("请输入你要租赁的天数:");
            int days = sc.nextInt();
            System.out.print("分配给你的汽车牌号是:");
            System.out.println();
            System.out.println(bus.getNum());   //拿到用户选择的车牌号
            double totalmoney = 0;      //调用totalmoney方法
            totalmoney=bus.totalmoney(days, bus.getRent());     //用用户输入的天数。来计算租金
            System.out.print("你需要支付的租赁分费用是:");
            System.out.print(totalmoney);
        }
    }
}

6,测试类:

package homework.exam;

public class Test {
    public static void main(String[] args) {
        CarService cs = new CarService();
        cs.rentcar();
    }
}

控制台输入的内容,我选择的是输入字符串类型,没有按照效果图上,如果你做的话,你可以用三元运算符来实现哦!

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

相关文章

  • java 字符串转化为字符数组的3种实现案例

    java 字符串转化为字符数组的3种实现案例

    这篇文章主要介绍了java 字符串转化为字符数组的3种实现案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • Java设计模式之Adapter适配器模式

    Java设计模式之Adapter适配器模式

    这篇文章主要为大家详细介绍了Java设计模式之Adapter适配器模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • SpringAop源码及调用过程概述

    SpringAop源码及调用过程概述

    这篇文章主要介绍了SpringAop源码及调用过程概述,Spring AOP(面向切面编程)是Spring框架的一个重要特性,它提供了一种在程序运行期间动态地将额外的行为织入到代码中的方式,需要的朋友可以参考下
    2023-10-10
  • IDEA中的Kafka管理神器详解

    IDEA中的Kafka管理神器详解

    这款基于IDEA插件实现的Kafka管理工具,能够在本地IDE环境中直接运行,简化了设置流程,为开发者提供了更加紧密集成、高效且直观的Kafka操作体验
    2025-01-01
  • 说一说java关键字final和transient

    说一说java关键字final和transient

    这篇文章主要和大家说一说java关键字final和transient,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • IDEA使用Mybatis插件 MyBatisCodeHelper-Pro的图文教程

    IDEA使用Mybatis插件 MyBatisCodeHelper-Pro的图文教程

    这篇文章主要介绍了IDEA使用Mybatis插件 MyBatisCodeHelper-Pro的教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Java 信息摘要加密MD2、MD4、MD5实现详解

    Java 信息摘要加密MD2、MD4、MD5实现详解

    这篇文章主要介绍了Java 信息摘要加密MD2、MD4、MD5实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • Java中的复合数据类型

    Java中的复合数据类型

    这篇文章主要介绍了Java中的复合数据类型,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Java虚拟机GC日志分析

    Java虚拟机GC日志分析

    这篇文章主要介绍了Java虚拟机GC日志分析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • Java如何实现实体类转Map、Map转实体类

    Java如何实现实体类转Map、Map转实体类

    这篇文章主要介绍了Java 实现实体类转Map、Map转实体类的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08

最新评论