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

车辆租赁系统的控制台实现是一个非常适合Java初学者练习面向对象编程的项目。它涵盖了类设计、封装、数组或集合使用、用户交互等核心知识点。下面是一个简洁但功能完整的控制台版车辆租赁系统实例。
系统需要实现以下基本功能:
主要包含以下几个类:
Vehicle(车辆基类)
立即学习“Java免费学习笔记(深入)”;
定义所有车辆共有的属性和方法。
```java public 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;
}
@Override
public String toString() {
return "【" + getType() + "】" + brand + " " + model + " - 日租金: ¥" + dailyRate;
}}
<font color="#0066cc"><strong>Car(轿车)</strong></font><br>
```java
public 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(卡车)
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<Vehicle> 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 < 0 || 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();
}
}程序运行后控制台输出类似:
=== 欢迎使用车辆租赁系统 === <ol><li>查看所有车辆</li><li>租车</li><li>退出 请选择操作: 1</li></ol><p>--- 可租赁车辆列表 ---</p><ol><li>【轿车(家用)】丰田 凯美瑞 - 日租金: ¥300.0</li><li>【轿车(SUV)】宝马 X5 - 日租金: ¥800.0</li><li>【卡车(10.5吨)】东风 天龙 - 日租金: ¥600.0</li><li>【客车(45座)】宇通 ZK6128 - 日租金: ¥900.0
可以在现有基础上增加以下功能:
基本上就这些。这个项目结构清晰,便于理解Java面向对象的基本应用,也方便后续扩展功能。
以上就是Java实现车辆租赁系统_控制台逻辑项目实例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号