写一个停车场管理系统Java代码
时间: 2023-07-27 15:06:04 浏览: 126
毕业设计,JavaEE停车场管理系统,Java完整源代码,内含毕业论文的设计与实现、毕业答辩PPT、数据库脚本以及项目辅导视频
好的,下面是一个简单的停车场管理系统Java代码示例,供您参考:
```java
import java.util.*;
public class ParkingLot {
private int capacity;
private Map<String, Car> parkedCars;
public ParkingLot(int capacity) {
this.capacity = capacity;
this.parkedCars = new HashMap<>();
}
public boolean parkCar(Car car) {
if (parkedCars.size() >= capacity) {
System.out.println("No available parking space!");
return false;
}
parkedCars.put(car.getLicensePlate(), car);
System.out.println("Car parked successfully!");
return true;
}
public void removeCar(String licensePlate) {
if (!parkedCars.containsKey(licensePlate)) {
System.out.println("Car not found!");
return;
}
parkedCars.remove(licensePlate);
System.out.println("Car removed successfully!");
}
public void displayParkedCars() {
if (parkedCars.isEmpty()) {
System.out.println("No cars parked!");
return;
}
System.out.println("Parked cars:");
for (Car car : parkedCars.values()) {
System.out.println(car.toString());
}
}
}
class Car {
private String licensePlate;
private String ownerName;
public Car(String licensePlate, String ownerName) {
this.licensePlate = licensePlate;
this.ownerName = ownerName;
}
public String getLicensePlate() {
return licensePlate;
}
public String getOwnerName() {
return ownerName;
}
public String toString() {
return licensePlate + " - " + ownerName;
}
}
class Main {
public static void main(String[] args) {
ParkingLot parkingLot = new ParkingLot(5);
Car car1 = new Car("ABC123", "John");
Car car2 = new Car("DEF456", "Sarah");
Car car3 = new Car("GHI789", "Peter");
parkingLot.parkCar(car1);
parkingLot.parkCar(car2);
parkingLot.parkCar(car3);
parkingLot.displayParkedCars();
parkingLot.removeCar("DEF456");
parkingLot.displayParkedCars();
}
}
```
以上代码实现了一个简单的停车场管理系统,包含了停车、取车、显示当前停车场内车辆信息等功能。您可以根据自己的需求进行修改和扩展。
阅读全文