Java实现简单汽车租赁系统

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("输入错误,请重新输入。");
            }
        }
    }
}

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

阅读剩余 83%

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

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

相关文章

  • Java字节与字符流永久存储json数据

    我来为你分享一下关于Java字节与字符流永久存储json数据的攻略。下面我们将分为以下三个步骤来讲解: 理解Java字节与字符流的概念与区别 将json数据通过字节流或字符流写入文件 从文件中读取json数据,并转换成对应的Java对象 1. 理解Java字节与字符流的概念与区别 在Java中,字节流和字符流是用于输入/输出数据的重要类。字节流是用于处理二进…

    Java 2023年5月26日
    00
  • 使用kafka如何选择分区数及kafka性能测试

    使用kafka如何选择分区数及kafka性能测试 选择分区数 在Kafka中,分区数是非常重要的一个概念,因为这个参数会影响消息的并发能力、可扩展性以及消息的有序性等方面。当我们在创建一个Kafka主题时,需要选择分区数。那么如何根据需要选择合适的分区数呢?下面是一些考虑因素: 1. 数据并发性的需求 数据的并发性是指可以同时处理多少消息。对于数据并发性要求…

    Java 2023年5月20日
    00
  • Java中让界面内的时间及时更新示例代码

    下面我来详细讲解一下“Java中让界面内的时间及时更新”的完整攻略,具体步骤如下: 1. 确定界面组件 首先需要确定要更新时间的界面组件,可以是JLabel、JTextField、JTextPane等。通常情况下,我们会选用JLabel组件来显示时间。 2. 创建时间更新线程 由于时间是需要不断更新的,所以我们需要创建一个线程来负责更新时间。这个线程可以用J…

    Java 2023年5月20日
    00
  • springboot结合全局异常处理实现登录注册验证

    下面我将为你详细讲解“Spring Boot结合全局异常处理实现登录注册验证”的完整攻略。 1. 前置知识 在学习此内容之前,你需要对以下技术有一定的了解: Spring Boot Spring MVC Spring Security Maven 2. 添加依赖 首先,我们需要在pom.xml文件中添加一些依赖。这些依赖包括: <!– Spring …

    Java 2023年5月25日
    00
  • 详解java中的四种代码块

    下面为您详细讲解“详解Java中的四种代码块”的攻略。 代码块 在Java中,代码块是一段被一对花括号包围的代码。Java中共有四种类型的代码块: 普通代码块 静态代码块 同步代码块 构造代码块 下面我们将分别对这四种代码块进行介绍。 普通代码块 普通代码块是被一对花括号包围的代码块,它可以出现在方法中、类中、循环体中等。 public class Code…

    Java 2023年5月30日
    00
  • SpringBoot2整合Drools规则引擎及案例详解

    Spring Boot 2整合Drools规则引擎及案例详解可以分为以下几个步骤: 第一步:引入Drools依赖 在pom.xml文件中引入Drools的依赖: <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</arti…

    Java 2023年5月19日
    00
  • Spring整合Mybatis详细步骤

    下面我将为您详细讲解 Spring 整合 MyBatis 的步骤,步骤如下: 第一步、导入相关依赖 首先需要在项目的 pom.xml 文件中导入 Spring 和 MyBatis 的相关依赖,具体依赖版本根据自己的需要进行选择。 <dependencies> <dependency> <groupId>org.spring…

    Java 2023年5月19日
    00
  • Ajax修改购物车示例

    下面是详细的“Ajax修改购物车示例”的攻略: 第一步:创建购物车页面 首先,需要创建一个基础的购物车页面,包含商品列表和购物车数量和总价等信息。可以使用 HTML 和 CSS 来创建一个简单的购物车页面。 第二步:添加商品和购物车的数据 在购物车页面上添加一些商品和购物车的数据,可以使用 JavaScript 来处理这些数据。例如,可以在 JavaScri…

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