.Java停车场信息管理 设计一个停车场信息管理系统,能够实现对停车位的信息管理(车位号、车主姓名、性别、年龄、车牌 号)。最后,停车信息输出到out.txt中。
时间: 2023-12-14 14:37:37 浏览: 56
基于java的停车场管理系统,应用于车辆的出入管理,优秀毕业设计系统!
这里是一个简单的Java停车场信息管理系统的设计:
```
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class ParkingLotManagementSystem {
public static void main(String[] args) {
ArrayList<ParkingSpace> parkingSpaces = new ArrayList<>(); // 创建停车位列表
// 添加停车位信息(示例数据)
parkingSpaces.add(new ParkingSpace(1, "张三", "男", 30, "粤A12345"));
parkingSpaces.add(new ParkingSpace(2, "李四", "女", 25, "粤B67890"));
parkingSpaces.add(new ParkingSpace(3, "王五", "男", 40, "粤C24680"));
Scanner scanner = new Scanner(System.in);
// 循环显示菜单
while (true) {
System.out.println("停车场信息管理系统");
System.out.println("1. 添加停车位信息");
System.out.println("2. 删除停车位信息");
System.out.println("3. 修改停车位信息");
System.out.println("4. 显示所有停车位信息");
System.out.println("5. 退出");
int choice = scanner.nextInt(); // 读取用户选择
switch (choice) {
case 1:
addParkingSpace(parkingSpaces, scanner);
break;
case 2:
deleteParkingSpace(parkingSpaces, scanner);
break;
case 3:
updateParkingSpace(parkingSpaces, scanner);
break;
case 4:
showAllParkingSpaces(parkingSpaces);
break;
case 5:
saveParkingSpacesToFile(parkingSpaces);
System.exit(0); // 退出程序
default:
System.out.println("无效的选择,请重新输入。");
}
}
}
// 添加停车位信息
private static void addParkingSpace(ArrayList<ParkingSpace> parkingSpaces, Scanner scanner) {
System.out.println("请输入车位号:");
int id = scanner.nextInt();
System.out.println("请输入车主姓名:");
String name = scanner.next();
System.out.println("请输入性别:");
String gender = scanner.next();
System.out.println("请输入年龄:");
int age = scanner.nextInt();
System.out.println("请输入车牌号:");
String licensePlateNumber = scanner.next();
parkingSpaces.add(new ParkingSpace(id, name, gender, age, licensePlateNumber));
System.out.println("添加成功!");
}
// 删除停车位信息
private static void deleteParkingSpace(ArrayList<ParkingSpace> parkingSpaces, Scanner scanner) {
System.out.println("请输入要删除的车位号:");
int id = scanner.nextInt();
for (ParkingSpace parkingSpace : parkingSpaces) {
if (parkingSpace.getId() == id) {
parkingSpaces.remove(parkingSpace);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到车位号为" + id + "的停车位。");
}
// 修改停车位信息
private static void updateParkingSpace(ArrayList<ParkingSpace> parkingSpaces, Scanner scanner) {
System.out.println("请输入要修改的车位号:");
int id = scanner.nextInt();
for (ParkingSpace parkingSpace : parkingSpaces) {
if (parkingSpace.getId() == id) {
System.out.println("请输入新的车主姓名(原值:" + parkingSpace.getName() + "):");
String name = scanner.next();
System.out.println("请输入新的性别(原值:" + parkingSpace.getGender() + "):");
String gender = scanner.next();
System.out.println("请输入新的年龄(原值:" + parkingSpace.getAge() + "):");
int age = scanner.nextInt();
System.out.println("请输入新的车牌号(原值:" + parkingSpace.getLicensePlateNumber() + "):");
String licensePlateNumber = scanner.next();
parkingSpace.setName(name);
parkingSpace.setGender(gender);
parkingSpace.setAge(age);
parkingSpace.setLicensePlateNumber(licensePlateNumber);
System.out.println("修改成功!");
return;
}
}
System.out.println("未找到车位号为" + id + "的停车位。");
}
// 显示所有停车位信息
private static void showAllParkingSpaces(ArrayList<ParkingSpace> parkingSpaces) {
System.out.println("车位号\t车主姓名\t性别\t年龄\t车牌号");
for (ParkingSpace parkingSpace : parkingSpaces) {
System.out.println(parkingSpace.getId() + "\t" + parkingSpace.getName() + "\t" +
parkingSpace.getGender() + "\t" + parkingSpace.getAge() + "\t" +
parkingSpace.getLicensePlateNumber());
}
}
// 将停车位信息保存到文件中
private static void saveParkingSpacesToFile(ArrayList<ParkingSpace> parkingSpaces) {
try {
FileWriter writer = new FileWriter("out.txt");
writer.write("车位号\t车主姓名\t性别\t年龄\t车牌号\n");
for (ParkingSpace parkingSpace : parkingSpaces) {
writer.write(parkingSpace.getId() + "\t" + parkingSpace.getName() + "\t" +
parkingSpace.getGender() + "\t" + parkingSpace.getAge() + "\t" +
parkingSpace.getLicensePlateNumber() + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println("保存文件时出错:" + e.getMessage());
}
}
}
// 停车位类
class ParkingSpace {
private int id; // 车位号
private String name; // 车主姓名
private String gender; // 性别
private int age; // 年龄
private String licensePlateNumber; // 车牌号
public ParkingSpace(int id, String name, String gender, int age, String licensePlateNumber) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.licensePlateNumber = licensePlateNumber;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
}
```
以上代码创建了一个停车场信息管理系统,可以添加、删除、修改和显示停车位信息,并将停车位信息保存到文件中(out.txt)。用户可以通过控制台输入选择操作。
阅读全文