用Java设计一个停车场信息管理系统,能够实现对停车位的信息管理(车位号、车主姓名、性别、年龄、 车牌号)。包含增删改查,最后,停车信息输出到out.txt中,下次打开可以读取数据的代码...
时间: 2024-02-25 20:54:44 浏览: 73
好的,我会为您解答这个问题。
首先,我们需要设计一个停车位信息的类,属性包括车位号、车主姓名、性别、年龄、车牌号。代码如下:
```java
public class ParkingInfo {
private int parkingNumber;
private String ownerName;
private String gender;
private int age;
private String carNumber;
// 构造函数
public ParkingInfo(int parkingNumber, String ownerName, String gender, int age, String carNumber) {
this.parkingNumber = parkingNumber;
this.ownerName = ownerName;
this.gender = gender;
this.age = age;
this.carNumber = carNumber;
}
// getter和setter方法
public int getParkingNumber() {
return parkingNumber;
}
public void setParkingNumber(int parkingNumber) {
this.parkingNumber = parkingNumber;
}
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 getCarNumber() {
return carNumber;
}
public void setCarNumber(String carNumber) {
this.carNumber = carNumber;
}
}
```
接下来,我们设计一个停车场信息管理系统类,包含增删改查和输出到文件的方法。代码如下:
```java
import java.io.*;
public class ParkingManagementSystem {
private ParkingInfo[] parkingInfoList;
private int size;
// 构造函数
public ParkingManagementSystem() {
parkingInfoList = new ParkingInfo[100];
size = 0;
}
// 增加停车信息
public void addParkingInfo(ParkingInfo parkingInfo) {
parkingInfoList[size] = parkingInfo;
size++;
}
// 删除停车信息
public void deleteParkingInfo(int parkingNumber) {
for (int i = 0; i < size; i++) {
if (parkingInfoList[i].getParkingNumber() == parkingNumber) {
for (int j = i; j < size - 1; j++) {
parkingInfoList[j] = parkingInfoList[j + 1];
}
size--;
break;
}
}
}
// 修改停车信息
public void modifyParkingInfo(int parkingNumber, ParkingInfo newParkingInfo) {
for (int i = 0; i < size; i++) {
if (parkingInfoList[i].getParkingNumber() == parkingNumber) {
parkingInfoList[i] = newParkingInfo;
break;
}
}
}
// 查找停车信息
public ParkingInfo searchParkingInfo(int parkingNumber) {
for (int i = 0; i < size; i++) {
if (parkingInfoList[i].getParkingNumber() == parkingNumber) {
return parkingInfoList[i];
}
}
return null;
}
// 输出到文件
public void outputToFile() {
try {
FileWriter fileWriter = new FileWriter("out.txt");
for (int i = 0; i < size; i++) {
fileWriter.write(parkingInfoList[i].getParkingNumber() + " ");
fileWriter.write(parkingInfoList[i].getOwnerName() + " ");
fileWriter.write(parkingInfoList[i].getGender() + " ");
fileWriter.write(parkingInfoList[i].getAge() + " ");
fileWriter.write(parkingInfoList[i].getCarNumber() + "\n");
}
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 从文件中读取数据
public void readFromFile() {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("out.txt"));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitLine = line.split(" ");
ParkingInfo parkingInfo = new ParkingInfo(Integer.parseInt(splitLine[0]), splitLine[1], splitLine[2], Integer.parseInt(splitLine[3]), splitLine[4]);
addParkingInfo(parkingInfo);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
最后,我们可以在主函数中测试我们的停车场信息管理系统。代码如下:
```java
public class Main {
public static void main(String[] args) {
ParkingManagementSystem parkingManagementSystem = new ParkingManagementSystem();
// 增加停车信息
ParkingInfo parkingInfo1 = new ParkingInfo(1, "张三", "男", 30, "粤A12345");
ParkingInfo parkingInfo2 = new ParkingInfo(2, "李四", "女", 25, "粤B23456");
ParkingInfo parkingInfo3 = new ParkingInfo(3, "王五", "男", 35, "粤C34567");
parkingManagementSystem.addParkingInfo(parkingInfo1);
parkingManagementSystem.addParkingInfo(parkingInfo2);
parkingManagementSystem.addParkingInfo(parkingInfo3);
// 删除停车信息
parkingManagementSystem.deleteParkingInfo(2);
// 修改停车信息
ParkingInfo newParkingInfo = new ParkingInfo(1, "张三", "男", 30, "粤D45678");
parkingManagementSystem.modifyParkingInfo(1, newParkingInfo);
// 查找停车信息
ParkingInfo parkingInfo = parkingManagementSystem.searchParkingInfo(1);
if (parkingInfo != null) {
System.out.println(parkingInfo.getOwnerName() + " " + parkingInfo.getCarNumber());
}
// 输出到文件
parkingManagementSystem.outputToFile();
// 读取文件中的数据
parkingManagementSystem.readFromFile();
// 查看停车信息
for (int i = 0; i < parkingManagementSystem.size; i++) {
System.out.println(parkingManagementSystem.parkingInfoList[i].getOwnerName() + " " + parkingManagementSystem.parkingInfoList[i].getCarNumber());
}
}
}
```
以上就是用Java设计一个停车场信息管理系统的全部内容。
阅读全文