利用Java设计一个停车场信息管理系统,能够实现对停车位的信息管理(车位号、车主姓名、性别、年龄、 车牌号)。最后,停车信息输出到out.txt中
时间: 2023-07-10 10:12:48 浏览: 118
基于Java的停车场管理系统(车牌识别)
5星 · 资源好评率100%
好的,我可以帮您设计一个停车场信息管理系统。首先,我们需要设计一个停车位类 ParkingSpot,该类包含车位号、车主姓名、性别、年龄、车牌号等属性和相应的 set 和 get 方法。
```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;
}
public int getSpotNumber() {
return spotNumber;
}
public void setSpotNumber(int spotNumber) {
this.spotNumber = spotNumber;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLicensePlateNumber() {
return licensePlateNumber;
}
public void setLicensePlateNumber(String licensePlateNumber) {
this.licensePlateNumber = licensePlateNumber;
}
}
```
接下来,我们需要设计一个停车场类 ParkingLot,该类包含停车位数组、入场、出场、查询等方法。
```java
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class ParkingLot {
private ParkingSpot[] spots;
public ParkingLot(int size) {
spots = new ParkingSpot[size];
for (int i = 0; i < size; i++) {
spots[i] = null;
}
}
// 停车
public boolean park(int spotNumber, String ownerName, String gender, int age, String licensePlateNumber) {
if (spots[spotNumber] == null) {
ParkingSpot spot = new ParkingSpot(spotNumber, ownerName, gender, age, licensePlateNumber);
spots[spotNumber] = spot;
return true;
} else {
return false;
}
}
// 取车
public boolean unpark(int spotNumber) {
if (spots[spotNumber] != null) {
spots[spotNumber] = null;
return true;
} else {
return false;
}
}
// 查询
public String inquiry(int spotNumber) {
if (spots[spotNumber] != null) {
ParkingSpot spot = spots[spotNumber];
return "车位号:" + spot.getSpotNumber()
+ " 车主姓名:" + spot.getOwnerName()
+ " 性别:" + spot.getGender()
+ " 年龄:" + spot.getAge()
+ " 车牌号:" + spot.getLicensePlateNumber();
} else {
return "该车位未停车";
}
}
// 输出到文件
public void outputToFile(String fileName) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter(fileName));
for (int i = 0; i < spots.length; i++) {
if (spots[i] != null) {
pw.println(inquiry(i));
}
}
pw.close();
}
}
```
我们可以在主函数中调用 ParkingLot 类的方法进行测试,并将结果输出到 out.txt 文件中。
```java
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
ParkingLot parkingLot = new ParkingLot(100);
// 停车
parkingLot.park(1, "张三", "男", 30, "浙A12345");
parkingLot.park(2, "李四", "女", 25, "浙B54321");
// 取车
parkingLot.unpark(1);
// 查询
System.out.println(parkingLot.inquiry(1));
System.out.println(parkingLot.inquiry(2));
// 输出到文件
parkingLot.outputToFile("out.txt");
}
}
```
运行结果如下:
```
该车位未停车
车位号:2 车主姓名:李四 性别:女 年龄:25 车牌号:浙B54321
```
out.txt 文件内容如下:
```
车位号:2 车主姓名:李四 性别:女 年龄:25 车牌号:浙B54321
```
阅读全文