取消字数回复限制,用Java设计一个停车场信息管理系统,能够实现对停车位的信息管理(车位号、车主姓名、性别、年龄、 车牌号)。包含增删改查,最后,停车信息输出到out.txt中,下次打开可以读取数据的代码
时间: 2023-09-19 08:06:05 浏览: 133
### 回答1:
Java 停车场信息管理系统
首先,需要创建一个类来表示停车位信息,包括车位号、车主姓名、性别、年龄、车牌号等属性。下面是一个简单的例子:
```
class ParkingSpace {
private int id;
private String ownerName;
private String gender;
private int age;
private String licensePlate;
public ParkingSpace(int id, String ownerName, String gender, int age, String licensePlate) {
this.id = id;
this.ownerName = ownerName;
this.gender = gender;
this.age = age;
this.licensePlate = licensePlate;
}
// getters and setters for each attribute
}
```
然后,需要创建一个类来管理停车位信息,包括增加、删除、修改和查询停车位信息的操作。这个类可以使用一个 List 来存储停车位信息。下面是一个简单的例子:
```
class ParkingSpaceManager {
private List<ParkingSpace> parkingSpaces;
public ParkingSpaceManager() {
this.parkingSpaces = new ArrayList<>();
}
public void addParkingSpace(ParkingSpace parkingSpace) {
parkingSpaces.add(parkingSpace);
}
public void removeParkingSpace(int id) {
for (int i = 0; i < parkingSpaces.size(); i++) {
if (parkingSpaces.get(i).getId() == id) {
parkingSpaces.remove(i);
break;
}
}
}
public void updateParkingSpace(int id, ParkingSpace updatedParkingSpace) {
for (int i = 0; i < parkingSpaces.size(); i++) {
if (parkingSpaces.get(i).getId() == id) {
parkingSpaces.set(i, updatedParkingSpace);
break;
}
}
}
public ParkingSpace getParkingSpace(int id) {
for (ParkingSpace parkingSpace : parkingSpaces) {
if (parkingSpace.getId() == id) {
return parkingSpace;
}
}
return null;
}
}
```
最后,需要创建
### 回答2:
停车场信息管理系统设计如下:
1. 创建一个类`ParkingLot`用于管理停车场信息。其中,每个停车位的信息包括车位号、车主姓名、性别、年龄、车牌号。
```java
public class ParkingLot {
private Map<Integer, String[]> parkingInfo; // 车位号对应的信息
private String filename; // 保存文件名
public ParkingLot(String filename) {
this.filename = filename;
this.parkingInfo = new HashMap<>();
readData(); // 读取保存的数据
}
// 添加停车位信息
public void addParkingInfo(int slot, String[] info) {
parkingInfo.put(slot, info);
}
// 删除停车位信息
public void deleteParkingInfo(int slot) {
parkingInfo.remove(slot);
}
// 修改停车位信息
public void updateParkingInfo(int slot, String[] info) {
parkingInfo.put(slot, info);
}
// 获取停车位信息
public String[] getParkingInfo(int slot) {
return parkingInfo.get(slot);
}
// 保存数据到文件
public void saveData() {
try {
FileWriter writer = new FileWriter(filename);
for (Map.Entry<Integer, String[]> entry : parkingInfo.entrySet()) {
int slot = entry.getKey();
String[] info = entry.getValue();
writer.write(slot + " " + String.join(" ", info) + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 从文件中读取数据
private void readData() {
try {
FileReader reader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(" ");
int slot = Integer.parseInt(parts[0]);
String[] info = Arrays.copyOfRange(parts, 1, parts.length);
parkingInfo.put(slot, info);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
2. 创建一个主类`Main`用于测试停车场信息管理系统。
```java
public class Main {
public static void main(String[] args) {
ParkingLot parkingLot = new ParkingLot("out.txt");
// 添加停车位信息
String[] info1 = {"车主1", "男", "30", "车牌号1"};
parkingLot.addParkingInfo(1, info1);
// 删除停车位信息
parkingLot.deleteParkingInfo(1);
// 修改停车位信息
String[] info2 = {"车主2", "女", "25", "车牌号2"};
parkingLot.updateParkingInfo(2, info2);
// 获取停车位信息
String[] info = parkingLot.getParkingInfo(2);
System.out.println(Arrays.toString(info));
// 保存数据到文件
parkingLot.saveData();
}
}
```
以上代码实现了一个简单的停车场信息管理系统,可以进行增删改查操作,并将停车信息保存到`out.txt`文件中。每次系统启动时,可以读取保存的数据进行操作。
### 回答3:
下面是一个简单的使用Java设计的停车场信息管理系统的示例:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ParkingLotManagementSystem {
private List<ParkingSpot> parkingSpots;
private String filename = "out.txt";
public ParkingLotManagementSystem() {
parkingSpots = new ArrayList<>();
}
public void addParkingSpot(ParkingSpot parkingSpot) {
parkingSpots.add(parkingSpot);
}
public void removeParkingSpot(int spotNumber) {
for (ParkingSpot parkingSpot : parkingSpots) {
if (parkingSpot.getSpotNumber() == spotNumber) {
parkingSpots.remove(parkingSpot);
break;
}
}
}
public void updateParkingSpot(int spotNumber, String ownerName, String gender, int age, String licensePlate) {
for (ParkingSpot parkingSpot : parkingSpots) {
if (parkingSpot.getSpotNumber() == spotNumber) {
parkingSpot.setOwnerName(ownerName);
parkingSpot.setGender(gender);
parkingSpot.setAge(age);
parkingSpot.setLicensePlate(licensePlate);
break;
}
}
}
public ParkingSpot findParkingSpot(int spotNumber) {
for (ParkingSpot parkingSpot : parkingSpots) {
if (parkingSpot.getSpotNumber() == spotNumber) {
return parkingSpot;
}
}
return null;
}
public void saveToFile() {
try {
FileWriter writer = new FileWriter(filename);
for (ParkingSpot parkingSpot : parkingSpots) {
writer.write(parkingSpot.toString());
writer.write("\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadFromFile() {
try {
File file = new File(filename);
if (file.exists()) {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
int spotNumber = Integer.parseInt(parts[0].trim());
String ownerName = parts[1].trim();
String gender = parts[2].trim();
int age = Integer.parseInt(parts[3].trim());
String licensePlate = parts[4].trim();
ParkingSpot parkingSpot = new ParkingSpot(spotNumber, ownerName, gender, age, licensePlate);
parkingSpots.add(parkingSpot);
}
scanner.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ParkingLotManagementSystem parkingLot = new ParkingLotManagementSystem();
parkingLot.loadFromFile();
// 添加示例车位
ParkingSpot parkingSpot1 = new ParkingSpot(1, "张三", "男", 30, "京A12345");
ParkingSpot parkingSpot2 = new ParkingSpot(2, "李四", "女", 28, "京B67890");
parkingLot.addParkingSpot(parkingSpot1);
parkingLot.addParkingSpot(parkingSpot2);
// 查询车位信息
int spotNumber = 1;
ParkingSpot foundSpot = parkingLot.findParkingSpot(spotNumber);
if (foundSpot != null) {
System.out.println("找到了车位 " + spotNumber + ": " + foundSpot.toString());
} else {
System.out.println("未找到车位 " + spotNumber);
}
// 修改车位信息
spotNumber = 1;
String ownerName = "王五";
String gender = "男";
int age = 35;
String licensePlate = "京C54321";
parkingLot.updateParkingSpot(spotNumber, ownerName, gender, age, licensePlate);
// 删除车位信息
spotNumber = 2;
parkingLot.removeParkingSpot(spotNumber);
// 保存到文件
parkingLot.saveToFile();
}
}
class ParkingSpot {
private int spotNumber;
private String ownerName;
private String gender;
private int age;
private String licensePlate;
public ParkingSpot(int spotNumber, String ownerName, String gender, int age, String licensePlate) {
this.spotNumber = spotNumber;
this.ownerName = ownerName;
this.gender = gender;
this.age = age;
this.licensePlate = licensePlate;
}
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 getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
@Override
public String toString() {
return spotNumber + ", " + ownerName + ", " + gender + ", " + age + ", " + licensePlate;
}
}
```
此示例实现了停车场信息管理系统,可以对停车位进行增删改查,并将停车信息保存在文件中。系统初始化时从文件中加载数据,对停车位进行操作后将信息保存到文件中。
阅读全文