Java设计一个停车场信息管理系统,包括类的定义、GUI界面设计、文件IO、集合,能够实现对停车位的信息管理(车位号、车主姓名、性别、年龄、车牌号)。最后,停车信息输出到out.txt中。
时间: 2023-07-04 18:14:16 浏览: 73
好的,我可以为您提供一个基本的Java停车场信息管理系统的实现。以下是代码示例:
ParkingLot.java
```
public class ParkingLot {
private String parkingNumber;
private String ownerName;
private String gender;
private int age;
private String licensePlateNumber;
public ParkingLot(String parkingNumber, String ownerName, String gender, int age, String licensePlateNumber) {
this.parkingNumber = parkingNumber;
this.ownerName = ownerName;
this.gender = gender;
this.age = age;
this.licensePlateNumber = licensePlateNumber;
}
public String getParkingNumber() {
return parkingNumber;
}
public void setParkingNumber(String 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 getLicensePlateNumber() {
return licensePlateNumber;
}
public void setLicensePlateNumber(String licensePlateNumber) {
this.licensePlateNumber = licensePlateNumber;
}
}
```
ParkingLotManager.java
```
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ParkingLotManager {
private List<ParkingLot> parkingLots = new ArrayList<>();
public void addParkingLot(ParkingLot parkingLot) {
parkingLots.add(parkingLot);
}
public void deleteParkingLot(int index) {
parkingLots.remove(index);
}
public List<ParkingLot> getParkingLots() {
return parkingLots;
}
public void saveToFile(String fileName) {
try (FileWriter writer = new FileWriter(fileName)) {
for (ParkingLot parkingLot : parkingLots) {
writer.write(parkingLot.getParkingNumber() + "," + parkingLot.getOwnerName() + ","
+ parkingLot.getGender() + "," + parkingLot.getAge() + "," + parkingLot.getLicensePlateNumber() + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadFromFile(String fileName) {
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
String parkingNumber = fields[0];
String ownerName = fields[1];
String gender = fields[2];
int age = Integer.parseInt(fields[3]);
String licensePlateNumber = fields[4];
ParkingLot parkingLot = new ParkingLot(parkingNumber, ownerName, gender, age, licensePlateNumber);
parkingLots.add(parkingLot);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
ParkingLotGui.java
```
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ParkingLotGui {
private JFrame frame;
private JTable table;
private DefaultTableModel tableModel;
private ParkingLotManager parkingLotManager;
public ParkingLotGui() {
parkingLotManager = new ParkingLotManager();
parkingLotManager.loadFromFile("parkingLot.txt");
frame = new JFrame("Parking Lot Manager");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
// create table
tableModel = new DefaultTableModel(new Object[]{"Parking Number", "Owner Name", "Gender", "Age", "License Plate Number"}, 0);
for (ParkingLot parkingLot : parkingLotManager.getParkingLots()) {
tableModel.addRow(new Object[]{parkingLot.getParkingNumber(), parkingLot.getOwnerName(),
parkingLot.getGender(), parkingLot.getAge(), parkingLot.getLicensePlateNumber()});
}
table = new JTable(tableModel);
// create buttons
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ParkingLotDialog dialog = new ParkingLotDialog(frame, true);
dialog.setVisible(true);
if (dialog.isOk()) {
ParkingLot parkingLot = dialog.getParkingLot();
parkingLotManager.addParkingLot(parkingLot);
tableModel.addRow(new Object[]{parkingLot.getParkingNumber(), parkingLot.getOwnerName(),
parkingLot.getGender(), parkingLot.getAge(), parkingLot.getLicensePlateNumber()});
}
}
});
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
parkingLotManager.deleteParkingLot(selectedRow);
tableModel.removeRow(selectedRow);
}
}
});
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
parkingLotManager.saveToFile("out.txt");
}
});
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(saveButton);
// add components to panel
panel.add(new JScrollPane(table), BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
// add panel to frame
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
new ParkingLotGui();
}
}
```
ParkingLotDialog.java
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ParkingLotDialog extends JDialog {
private JTextField parkingNumberField;
private JTextField ownerNameField;
private JTextField genderField;
private JTextField ageField;
private JTextField licensePlateNumberField;
private boolean isOk;
private ParkingLot parkingLot;
public ParkingLotDialog(Frame parent, boolean modal) {
super(parent, modal);
setTitle("Add Parking Lot");
setSize(300, 200);
setLocationRelativeTo(parent);
JPanel panel = new JPanel(new GridLayout(5, 2));
JLabel parkingNumberLabel = new JLabel("Parking Number:");
parkingNumberField = new JTextField();
panel.add(parkingNumberLabel);
panel.add(parkingNumberField);
JLabel ownerNameLabel = new JLabel("Owner Name:");
ownerNameField = new JTextField();
panel.add(ownerNameLabel);
panel.add(ownerNameField);
JLabel genderLabel = new JLabel("Gender:");
genderField = new JTextField();
panel.add(genderLabel);
panel.add(genderField);
JLabel ageLabel = new JLabel("Age:");
ageField = new JTextField();
panel.add(ageLabel);
panel.add(ageField);
JLabel licensePlateNumberLabel = new JLabel("License Plate Number:");
licensePlateNumberField = new JTextField();
panel.add(licensePlateNumberLabel);
panel.add(licensePlateNumberField);
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String parkingNumber = parkingNumberField.getText();
String ownerName = ownerNameField.getText();
String gender = genderField.getText();
int age = Integer.parseInt(ageField.getText());
String licensePlateNumber = licensePlateNumberField.getText();
parkingLot = new ParkingLot(parkingNumber, ownerName, gender, age, licensePlateNumber);
isOk = true;
dispose();
}
});
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
panel.add(buttonPanel);
add(panel);
}
public boolean isOk() {
return isOk;
}
public ParkingLot getParkingLot() {
return parkingLot;
}
}
```
以上代码实现了一个简单的Java停车场信息管理系统,包括类的定义、GUI界面设计、文件IO和集合。当添加、删除或修改停车位信息时,这些信息会被保存到文本文件中。最后,停车信息会输出到out.txt文件中。
阅读全文