用Java设计一个停车场信息管理系统,能够实现对停车位的信息管理(车位号、车主姓名、性别、年龄、 车牌号)。包含增删改查,最后,停车信息输出到out.txt中,下次打开可以读取数据的代码...
时间: 2024-02-25 15:56:46 浏览: 60
基于Java的停车场管理系统
好的,我可以帮您设计一个停车场信息管理系统。首先,我们需要定义一个ParkingLot类,用于表示停车场。该类需要包含如下属性:
```java
public class ParkingLot {
private List<ParkingSpot> spots;
private Map<String, ParkingSpot> parkingSpotMap;
private Map<String, ParkingSpot> licensePlateMap;
private String outputFile;
}
```
接下来,我们需要定义一个ParkingSpot类,用于表示停车位。该类需要包含如下属性:
```java
public class ParkingSpot {
private String spotNumber;
private String ownerName;
private String gender;
private int age;
private String licensePlate;
}
```
然后,我们需要实现增删改查的功能。这里我提供一个简单的实现方式:
```java
public class ParkingLot {
// 添加停车位
public void addParkingSpot(ParkingSpot parkingSpot) {
spots.add(parkingSpot);
parkingSpotMap.put(parkingSpot.getSpotNumber(), parkingSpot);
licensePlateMap.put(parkingSpot.getLicensePlate(), parkingSpot);
}
// 删除停车位
public void removeParkingSpot(String spotNumber) {
ParkingSpot parkingSpot = parkingSpotMap.get(spotNumber);
if (parkingSpot != null) {
spots.remove(parkingSpot);
parkingSpotMap.remove(spotNumber);
licensePlateMap.remove(parkingSpot.getLicensePlate());
}
}
// 修改停车位
public void updateParkingSpot(ParkingSpot parkingSpot) {
ParkingSpot oldSpot = parkingSpotMap.get(parkingSpot.getSpotNumber());
if (oldSpot != null) {
oldSpot.setOwnerName(parkingSpot.getOwnerName());
oldSpot.setGender(parkingSpot.getGender());
oldSpot.setAge(parkingSpot.getAge());
oldSpot.setLicensePlate(parkingSpot.getLicensePlate());
licensePlateMap.remove(oldSpot.getLicensePlate());
licensePlateMap.put(parkingSpot.getLicensePlate(), parkingSpot);
}
}
// 根据车位号查找停车位
public ParkingSpot findParkingSpotBySpotNumber(String spotNumber) {
return parkingSpotMap.get(spotNumber);
}
// 根据车牌号查找停车位
public ParkingSpot findParkingSpotByLicensePlate(String licensePlate) {
return licensePlateMap.get(licensePlate);
}
}
```
最后,我们需要实现停车信息输出到out.txt中,下次打开可以读取数据的代码。这里我提供一个简单的实现方式:
```java
public class ParkingLot {
// 将停车信息输出到文件中
public void outputToFile() {
try (PrintWriter writer = new PrintWriter(outputFile)) {
for (ParkingSpot parkingSpot : spots) {
writer.println(parkingSpot.getSpotNumber() + "," +
parkingSpot.getOwnerName() + "," +
parkingSpot.getGender() + "," +
parkingSpot.getAge() + "," +
parkingSpot.getLicensePlate());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// 从文件中读取停车信息
public void readFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(outputFile))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
ParkingSpot parkingSpot = new ParkingSpot();
parkingSpot.setSpotNumber(fields[0]);
parkingSpot.setOwnerName(fields[1]);
parkingSpot.setGender(fields[2]);
parkingSpot.setAge(Integer.parseInt(fields[3]));
parkingSpot.setLicensePlate(fields[4]);
addParkingSpot(parkingSpot);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这样,一个简单的停车场信息管理系统就实现了。完整代码如下:
阅读全文