.Java停车场信息管理 设计一个停车场信息管理系统,能够实现对停车位的信息管理(车位号、车主姓名、性别、年龄、车牌 号)。最后,停车信息输出到out.txt中。
时间: 2024-05-15 22:14:06 浏览: 71
基于java的停车场管理系统,应用于车辆的出入管理,优秀毕业设计系统!
实现这个停车场信息管理系统,可以按照以下步骤来进行:
1. 定义一个停车位类,包括车位号、车主姓名、性别、年龄、车牌号等属性。
```java
public class ParkingSpot {
private int spotNumber;
private String ownerName;
private String gender;
private int age;
private String licensePlateNumber;
public ParkingSpot(int spotNumber, String ownerName, String gender, int age, String licensePlateNumber) {
this.spotNumber = spotNumber;
this.ownerName = ownerName;
this.gender = gender;
this.age = age;
this.licensePlateNumber = licensePlateNumber;
}
// getters and setters
}
```
2. 定义一个停车场类,包括停车位数组、添加车位、删除车位、修改车位信息、查找车位信息等方法。
```java
import java.util.ArrayList;
import java.util.List;
public class ParkingLot {
private List<ParkingSpot> parkingSpots;
public ParkingLot() {
this.parkingSpots = new ArrayList<>();
}
public void addParkingSpot(ParkingSpot parkingSpot) {
this.parkingSpots.add(parkingSpot);
}
public void removeParkingSpot(int spotNumber) {
for (ParkingSpot parkingSpot : this.parkingSpots) {
if (parkingSpot.getSpotNumber() == spotNumber) {
this.parkingSpots.remove(parkingSpot);
break;
}
}
}
public void updateParkingSpot(int spotNumber, ParkingSpot updatedParkingSpot) {
for (int i = 0; i < this.parkingSpots.size(); i++) {
ParkingSpot parkingSpot = this.parkingSpots.get(i);
if (parkingSpot.getSpotNumber() == spotNumber) {
this.parkingSpots.set(i, updatedParkingSpot);
break;
}
}
}
public ParkingSpot findParkingSpot(int spotNumber) {
for (ParkingSpot parkingSpot : this.parkingSpots) {
if (parkingSpot.getSpotNumber() == spotNumber) {
return parkingSpot;
}
}
return null;
}
public void printParkingLotToFile(String filename) {
try {
PrintWriter writer = new PrintWriter(new FileWriter(filename));
for (ParkingSpot parkingSpot : this.parkingSpots) {
writer.println("车位号:" + parkingSpot.getSpotNumber());
writer.println("车主姓名:" + parkingSpot.getOwnerName());
writer.println("性别:" + parkingSpot.getGender());
writer.println("年龄:" + parkingSpot.getAge());
writer.println("车牌号:" + parkingSpot.getLicensePlateNumber());
writer.println();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
3. 编写一个测试类来测试停车场信息管理系统的功能。
```java
public class TestParkingLot {
public static void main(String[] args) {
ParkingLot parkingLot = new ParkingLot();
// 添加车位
ParkingSpot parkingSpot1 = new ParkingSpot(1, "张三", "男", 30, "粤A12345");
ParkingSpot parkingSpot2 = new ParkingSpot(2, "李四", "女", 25, "粤B23456");
parkingLot.addParkingSpot(parkingSpot1);
parkingLot.addParkingSpot(parkingSpot2);
// 修改车位信息
ParkingSpot updatedParkingSpot = new ParkingSpot(2, "王五", "男", 40, "粤C34567");
parkingLot.updateParkingSpot(2, updatedParkingSpot);
// 查找车位信息
ParkingSpot foundParkingSpot = parkingLot.findParkingSpot(1);
// 删除车位
parkingLot.removeParkingSpot(1);
// 输出停车场信息到文件
parkingLot.printParkingLotToFile("out.txt");
}
}
```
这样,我们就完成了一个简单的停车场信息管理系统。你可以根据你的实际需求修改上述代码。
阅读全文