写一个java车库管理系统
时间: 2024-05-29 07:05:23 浏览: 154
一个Java车库管理系统可以包括以下功能:
1. 车辆管理:可以添加、删除、修改车辆信息,包括车牌号、品牌、型号、颜色等。
2. 停车管理:记录车辆的进入和离开时间,计算停车时长和费用。
3. 车位管理:管理车库内的停车位,包括车位编号、是否空闲等信息。
4. 收费管理:根据停车时长和费率计算停车费用,并提供收费记录。
5. 查询统计:提供查询功能,可以按照车牌号、品牌等条件查询车辆信息和停车记录,并提供统计功能,如统计每天的停车收入等。
下面是一个简单的Java车库管理系统的示例代码:
```java
import java.util.ArrayList;
import java.util.List;
class Vehicle {
private String licensePlate; private String brand;
private String model;
private String color;
public Vehicle(String licensePlate, String brand, String model, String color) {
this.licensePlate = licensePlate;
this.brand = brand;
this.model = model;
this.color = color;
}
// getters and setters
}
class ParkingSpot {
private int spotNumber;
private boolean isAvailable;
public ParkingSpot(int spotNumber) {
this.spotNumber = spotNumber;
this.isAvailable = true;
}
// getters and setters
}
class ParkingRecord {
private Vehicle vehicle;
private long entryTime;
private long exitTime;
public ParkingRecord(Vehicle vehicle, long entryTime) {
this.vehicle = vehicle;
this.entryTime = entryTime;
}
public void setExitTime(long exitTime) {
this.exitTime = exitTime;
}
// getters
}
class Garage {
private List<ParkingSpot> parkingSpots;
private List<ParkingRecord> parkingRecords;
public Garage(int capacity) {
parkingSpots = new ArrayList<>(); for (int i = 1; i <= capacity; i++) {
parkingSpots.add(new ParkingSpot(i));
}
parkingRecords = new ArrayList<>();
}
public void parkVehicle(Vehicle vehicle, long entryTime) {
ParkingSpot availableSpot = findAvailableSpot();
if (availableSpot != null) {
availableSpot.setAvailable(false);
parkingRecords.add(new ParkingRecord(vehicle, entryTime));
System.out.println("Vehicle parked at spot " + availableSpot.getSpotNumber());
} else {
System.out.println("No available parking spot.");
}
}
public void removeVehicle(Vehicle vehicle, long exitTime) {
for (ParkingRecord record : parkingRecords) {
if (record.getVehicle().equals(vehicle) && record.getExitTime() == 0) {
record.setExitTime(exitTime);
ParkingSpot spot = findSpotByVehicle(vehicle);
spot.setAvailable(true);
System.out.println("Vehicle removed from spot " + spot.getSpotNumber());
return;
}
}
System.out.println("Vehicle not found or already removed.");
}
private ParkingSpot findAvailableSpot() {
for (ParkingSpot spot : parkingSpots) {
if (spot.isAvailable()) {
return spot;
}
}
return null;
}
private ParkingSpot findSpotByVehicle(Vehicle vehicle) {
for (ParkingRecord record : parkingRecords) {
if (record.getVehicle().equals(vehicle) && record.getExitTime() == 0) {
return parkingSpots.get(record.getSpotNumber() - 1);
}
}
return null;
}
}
public class GarageManagementSystem {
public static void main(String[] args) {
Garage garage = new Garage(10);
Vehicle vehicle1 = new Vehicle("ABC123", "Toyota", "Camry", "Red");
Vehicle vehicle2 = new Vehicle("XYZ789", "Honda", "Accord", "Blue");
long entryTime1 = System.currentTimeMillis();
garage.parkVehicle(vehicle1, entryTime1);
long entryTime2 = System.currentTimeMillis();
garage.parkVehicle(vehicle2, entryTime2);
long exitTime1 = System.currentTimeMillis();
garage.removeVehicle(vehicle1, exitTime1);
long exitTime2 = System.currentTimeMillis();
garage.removeVehicle(vehicle2, exitTime2);
}
}
```
这个示例代码实现了一个简单的车库管理系统,包括车辆管理、停车管理和收费管理等功能。你可以根据自己的需求进行扩展和修改。
阅读全文