Java实现简单汽车租赁系统

yizhihongxing

Java实现简单汽车租赁系统的完整攻略

系统需求分析

该汽车租赁系统应该具备以下功能:

  1. 显示当前的租赁车辆列表

  2. 租客可以查询所需汽车类型的库存量

  3. 租客可以租车,并计算租车天数、费用等信息

  4. 车辆归还,更新库存

系统设计

类的设计-属性和方法

Car(汽车类)

属性:

  1. carType:汽车类型

  2. carId:汽车编号

  3. carPrice:汽车租金(元/天)

  4. isRented:是否已被租赁

方法:

  1. showCar():展示汽车信息

  2. getIsRented(): 获取汽车是否已被租赁状态

  3. setIsRented(): 设置汽车是否已被租赁状态

Customer(租户类)

属性:

  1. cusId:租户编号

  2. cusName:租户姓名

  3. rentedCar:所租汽车

  4. rentDays:租车天数

  5. rentFee:租车费用

方法:

  1. showRentInfo():展示租车信息

  2. rentCar():租车

  3. returnCar():还车

流程

初始化

  1. 创建两个集合,分别存储汽车和租户

  2. 创建Car和Customer实例,并添加到集合中(模拟汽车和租户库存信息)

展示租赁信息

  1. 遍历汽车集合,展示所有可租赁的汽车信息

  2. 类似的,展示租户信息

租车

  1. 根据租户输入查询车辆库存

  2. 如果车辆库存不足,则提示租户,否则进入下一步

  3. 获取租车天数,计算租车费用

  4. 将租车的租户信息存入租户集合中,并将车辆状态更改为“已租赁”

还车

  1. 根据租户编号和汽车编号查询租户和车辆信息

  2. 计算租车费用,更新租户信息(金额、租车天数等),更新汽车状态为“未租赁”

代码实现

以下是Java实现的汽车租赁系统示例:

import java.util.ArrayList;
import java.util.Scanner;

class Car {
    private String carType;
    private int carId;
    private double carPrice;
    private boolean isRented;

    // 构造方法
    public Car(String carType, int carId, double carPrice, boolean isRented) {
        this.carType = carType;
        this.carId = carId;
        this.carPrice = carPrice;
        this.isRented = isRented;
    }

    public void showCar() {
        System.out.println("车型:" + this.carType);
        System.out.println("编号:" + this.carId);
        System.out.println("价格:" + this.carPrice + "元/天");
        System.out.println("状态:" + (this.isRented ? "已租赁" : "未租赁"));
    }

    public boolean getIsRented() {
        return isRented;
    }

    public void setIsRented(boolean isRented) {
        this.isRented = isRented;
    }
}

class Customer {
    private int cusId;
    private String cusName;
    private Car rentedCar;
    private int rentDays;
    private double rentFee;

    public Customer(int cusId, String cusName) {
        this.cusId = cusId;
        this.cusName = cusName;
    }

    public void showRentInfo() {
        System.out.println("客户编号:" + this.cusId);
        System.out.println("客户姓名:" + this.cusName);
        System.out.println("租用的汽车");
        this.rentedCar.showCar();
        System.out.println("租用日期:" + rentDays + "天");
        System.out.println("租用费用:" + rentFee + "元");
    }

    public void rentCar(Car car, int days) {
        if (car.getIsRented()) {
            System.out.println("车辆 " + car.carId + " 已被租赁,请选择其他车辆。");
            return;
        }

        this.rentedCar = car;
        this.rentDays = days;
        this.rentFee = car.carPrice * days;

        car.setIsRented(true);
        System.out.println("客户编号:" + cusId);
        System.out.println("客户姓名:" + cusName);
        System.out.println("租用的汽车");
        this.rentedCar.showCar();
        System.out.println("租用日期:" + this.rentDays + "天");
        System.out.println("租用费用:" + this.rentFee + "元");
    }

    public void returnCar(int carId) {
        if (this.rentedCar == null || this.rentedCar.carId != carId) {
            System.out.println("客户 " + this.cusName + " 没有租用编号为 " + carId + " 的车辆。");
            return;
        }

        this.rentedCar.setIsRented(false);
        System.out.println("客户编号:" + cusId);
        System.out.println("客户姓名:" + cusName);
        System.out.println("租用的汽车");
        this.rentedCar.showCar();
        System.out.println("租用日期:" + this.rentDays + "天");
        System.out.println("租用费用:" + this.rentFee + "元");

        this.rentedCar = null;
        this.rentDays = 0;
        this.rentFee = 0;
    }
}

public class CarRentalSystem {
    public static void main(String[] args) {
        ArrayList<Car> carList = new ArrayList<>();
        ArrayList<Customer> customerList = new ArrayList<>();

        carList.add(new Car("小轿车", 1, 100, false));
        carList.add(new Car("面包车", 2, 150, true));
        carList.add(new Car("越野车", 3, 200, false));
        carList.add(new Car("商务车", 4, 250, false));
        carList.add(new Car("大巴车", 5, 300, false));

        customerList.add(new Customer(1, "张三"));
        customerList.add(new Customer(2, "李四"));
        customerList.add(new Customer(3, "王五"));

        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("欢迎使用汽车租赁系统,请选择以下功能:");
            System.out.println("1. 查看车辆信息");
            System.out.println("2. 查看客户信息");
            System.out.println("3. 租车");
            System.out.println("4. 还车");
            System.out.println("0. 退出系统");

            int choice = sc.nextInt();

            switch (choice) {
                case 1:
                    for (Car car : carList) {
                        car.showCar();
                    }
                    break;
                case 2:
                    for (Customer customer : customerList) {
                        customer.showRentInfo();
                    }
                    break;
                case 3:
                    System.out.println("请输入客户编号:");
                    int id = sc.nextInt();
                    System.out.println("请输入租赁天数:");
                    int days = sc.nextInt();
                    System.out.println("请选择车辆类型(输入数字1-5):");
                    System.out.println("1. 小轿车");
                    System.out.println("2. 面包车");
                    System.out.println("3. 越野车");
                    System.out.println("4. 商务车");
                    System.out.println("5. 大巴车");
                    int carType = sc.nextInt();

                    for (Car car : carList) {
                        if (car.carType.equals("小轿车") && carType == 1) {
                            customerList.get(id - 1).rentCar(car, days);
                            break;
                        } else if (car.carType.equals("面包车") && carType == 2) {
                            customerList.get(id - 1).rentCar(car, days);
                            break;
                        } else if (car.carType.equals("越野车") && carType == 3) {
                            customerList.get(id - 1).rentCar(car, days);
                            break;
                        } else if (car.carType.equals("商务车") && carType == 4) {
                            customerList.get(id - 1).rentCar(car, days);
                            break;
                        } else if (car.carType.equals("大巴车") && carType == 5) {
                            customerList.get(id - 1).rentCar(car, days);
                            break;
                        }
                    }
                    break;
                case 4:
                    System.out.println("请输入客户编号:");
                    id = sc.nextInt();
                    System.out.println("请输入车辆编号:");
                    int carId = sc.nextInt();
                    customerList.get(id - 1).returnCar(carId);
                    break;
                case 0:
                    System.exit(0);
                default:
                    System.out.println("输入错误,请重新输入。");
            }
        }
    }
}

上述示例提供了展示租车信息、租车、还车等的实现,可以自行测试、优化。对于输入、输出等交互细节,可以根据实际需求进行适当调整。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java实现简单汽车租赁系统 - Python技术站

(0)
上一篇 2023年5月19日
下一篇 2023年5月19日

相关文章

  • 浅谈springboot如何保证多线程安全

    下面是关于“浅谈Spring Boot如何保证多线程安全”的攻略: 1. 了解多线程 要理解多线程安全,我们需要先了解多线程的概念。简单来说,多线程是一种执行多个任务的机制,可以让程序同时完成多项任务,提高程序的运行效率。 但是,多线程也会带来线程安全问题。如果多个线程同时访问同一个资源,就有可能造成数据的混乱或错误。 2. Spring Boot的多线程机…

    Java 2023年5月19日
    00
  • Spring mvc Controller和RestFul原理解析

    下面是关于Spring MVC Controller和RestFul的原理解析,包含两个示例说明。 Spring MVC Controller和RestFul原理解析 Spring MVC是一个流行的Java Web框架,它可以帮助我们快速构建Web应用程序。在Spring MVC中,Controller是一个重要的组件,它用于处理请求并返回响应。在本文中,…

    Java 2023年5月17日
    00
  • Java class文件格式之属性详解_动力节点java学院整理

    Java Class文件格式之属性详解 在Java中,每个类都有一个对应的.class文件,它包含了该类的所有信息,包括成员变量、方法等。.class文件由以下几个部分构成: 魔数:4个字节,用于标识.class文件是否合法,固定值为0xCAFEBABE。 版本号:4个字节,分别表示主版本号和次版本号,用于标识该文件所对应的JVM版本。 常量池:变长结构,存…

    Java 2023年5月20日
    00
  • Java基础之内存泄漏与溢出详解

    Java基础之内存泄漏与溢出详解 什么是内存泄漏 内存泄漏指的是在程序中使用了堆内存空间,但是这些空间已经不再被使用,却没有被释放。这样一来,这些无用的内存空间就会一直占用着计算机的资源,导致计算机变得越来越慢,甚至会崩溃。 内存泄漏的原因 内存泄漏的原因有很多种,常见的包括: 变量的生命周期过长:当一个变量的生命周期过长时,如果不及时将它释放,那么它占用的…

    Java 2023年5月25日
    00
  • 一名Java高级工程师需要学什么?

    作为一名Java高级工程师,需要掌握以下技能和知识: 1. Java核心技术 Java核心技术是Java开发的基础,包括Java SE、Java EE、Java ME等。Java SE是Java标准版,包括Java语言基础、集合框架、IO、多线程、反射、注解等。Java EE是Java企业版,包括Servlet、JSP、EJB、JPA、JMS、Web Ser…

    Java 2023年5月18日
    00
  • CAS操作的实现原理是什么?

    CAS(Compare And Swap)是一种并发控制机制,用于保证多线程并发修改时的数据一致性。它主要包括三个操作数:内存地址V、旧的预期值A和新的值B。当且仅当内存地址V的值和预期值A相同时,才把新的值B赋值给内存地址V,否则就什么都不做。下面就来详细讲解一下CAS操作的实现原理: CAS操作的实现原理 在计算机能够完成CAS操作的原理中,有两个非常重…

    Java 2023年5月10日
    00
  • Java中类的加载器及其加载过程

    Java中类的加载器是Java虚拟机的一个重要组成部分,主要负责将Java字节码文件加载到JVM中。类的加载器是Java虚拟机的一个根本特性,通过加载器机制,Java虚拟机可以实现动态链接,提高系统的灵活性和可扩展性。下面将从Java类的加载器的基本概念、分类以及加载过程等方面来进行详细讲解。 1. 类加载器的基本概念 Java类加载器是Java虚拟机的一个…

    Java 2023年6月15日
    00
  • 解决使用json-lib包实现xml转json时空值被转为空中括号的问题

    首先,我们需要了解为什么会出现空值被转为空中括号的问题。这是因为json-lib默认不支持将空值转化为null,而将空值转化为空数组,为空数组的标志就是”[]”空中括号。 那么解决这个问题的方法就是需要我们手动配置json-lib。具体操作如下: 首先,引入json-lib的jar包到项目中,并且依赖于lib目录下的ezmorph.jar, commons-…

    Java 2023年5月26日
    00
合作推广
合作推广
分享本页
返回顶部