车辆租赁系统通过面向对象设计实现租车功能,包含Vehicle基类及Car、Truck、Bus子类,RentalSystem管理车辆列表与用户交互,支持查看车辆、计算租金、租车操作,运行时通过控制台输入选择功能,展示车辆信息并完成租赁流程。

车辆租赁系统的控制台实现是一个非常适合Java初学者练习面向对象编程的项目。它涵盖了类设计、封装、数组或集合使用、用户交互等核心知识点。下面是一个简洁但功能完整的控制台版车辆租赁系统实例。
1. 系统需求分析
系统需要实现以下基本功能:
展示可租赁的车辆列表根据车型计算租赁费用记录租车信息(如租期、客户姓名)支持多种车辆类型(如轿车、卡车、客车)
2. 类设计与结构
主要包含以下几个类:
Vehicle(车辆基类)
立即学习“Java免费学习笔记(深入)”;
定义所有车辆共有的属性和方法。
“`javapublic abstract class Vehicle { protected String brand; protected String model; protected double dailyRate;
public Vehicle(String brand, String model, double dailyRate) { this.brand = brand; this.model = model; this.dailyRate = dailyRate;}public abstract String getType();public double calculateRentalCost(int days) { return dailyRate * days;}@Overridepublic String toString() { return "【" + getType() + "】" + brand + " " + model + " - 日租金: ¥" + dailyRate;}
}
Car(轿车)
```javapublic class Car extends Vehicle { private String carType; public Car(String brand, String model, double dailyRate, String carType) { super(brand, model, dailyRate); this.carType = carType; } @Override public String getType() { return "轿车(" + carType + ")"; }}
Truck(卡车)
九歌
九歌–人工智能诗歌写作系统
322 查看详情
public class Truck extends Vehicle { private double loadCapacity; // 载重吨数 public Truck(String brand, String model, double dailyRate, double loadCapacity) { super(brand, model, dailyRate); this.loadCapacity = loadCapacity; } @Override public String getType() { return "卡车(" + loadCapacity + "吨)"; }}
Bus(客车)
public class Bus extends Vehicle { private int seatCount; public Bus(String brand, String model, double dailyRate, int seatCount) { super(brand, model, dailyRate); this.seatCount = seatCount; } @Override public String getType() { return "客车(" + seatCount + "座)"; }}
RentalSystem(主系统逻辑)
import java.util.*;public class RentalSystem { private List vehicles; private Scanner scanner; public RentalSystem() { vehicles = new ArrayList(); scanner = new Scanner(System.in); initVehicles(); } private void initVehicles() { vehicles.add(new Car("丰田", "凯美瑞", 300, "家用")); vehicles.add(new Car("宝马", "X5", 800, "SUV")); vehicles.add(new Truck("东风", "天龙", 600, 10.5)); vehicles.add(new Bus("宇通", "ZK6128", 900, 45)); } public void start() { System.out.println("=== 欢迎使用车辆租赁系统 ==="); while (true) { showMenu(); int choice = scanner.nextInt(); scanner.nextLine(); // 消费换行符 switch (choice) { case 1: listVehicles(); break; case 2: rentVehicle(); break; case 3: System.out.println("感谢使用,再见!"); return; default: System.out.println("无效选择,请重新输入。"); } } } private void showMenu() { System.out.println("\n1. 查看所有车辆"); System.out.println("2. 租车"); System.out.println("3. 退出"); System.out.print("请选择操作: "); } private void listVehicles() { System.out.println("\n--- 可租赁车辆列表 ---"); for (int i = 0; i < vehicles.size(); i++) { System.out.println((i + 1) + ". " + vehicles.get(i)); } } private void rentVehicle() { listVehicles(); System.out.print("请输入要租赁的车辆编号: "); int index = scanner.nextInt() - 1; scanner.nextLine(); if (index = vehicles.size()) { System.out.println("车辆编号无效!"); return; } System.out.print("请输入租赁天数: "); int days = scanner.nextInt(); scanner.nextLine(); Vehicle selected = vehicles.get(index); double total = selected.calculateRentalCost(days); System.out.print("请输入客户姓名: "); String name = scanner.nextLine(); System.out.println("\n--- 租赁成功 ---"); System.out.println("客户: " + name); System.out.println("车辆: " + selected); System.out.println("租赁天数: " + days + "天"); System.out.println("总费用: ¥" + total); } public static void main(String[] args) { new RentalSystem().start(); }}
3. 运行效果示例
程序运行后控制台输出类似:
=== 欢迎使用车辆租赁系统 ===
- 查看所有车辆
- 租车
- 退出请选择操作: 1
--- 可租赁车辆列表 ---
- 【轿车(家用)】丰田 凯美瑞 - 日租金: ¥300.0
- 【轿车(SUV)】宝马 X5 - 日租金: ¥800.0
- 【卡车(10.5吨)】东风 天龙 - 日租金: ¥600.0
- 【客车(45座)】宇通 ZK6128 - 日租金: ¥900.0
4. 扩展建议
可以在现有基础上增加以下功能:
添加还车功能,标记车辆为可用状态使用文件存储车辆和订单数据增加用户登录模块支持按品牌或类型筛选车辆添加租赁订单管理功能
基本上就这些。这个项目结构清晰,便于理解Java面向对象的基本应用,也方便后续扩展功能。
以上就是Java实现车辆租赁系统_控制台逻辑项目实例的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1029873.html
微信扫一扫
支付宝扫一扫