用Java设计一个医院床位信息管理系统,能够实现病人信息(床位号、姓名、性别、年龄、病类)的增加、删 除、修改、查询以及更新功能。最后,将病人住院信息保存在out.txt。用GUI窗口输出。
时间: 2023-07-10 20:23:23 浏览: 206
医院信息管理系统(JAVA).
4星 · 用户满意度95%
好的,我来解答你的问题。首先,设计一个医院床位信息管理系统,需要涉及到以下几个方面:
1. 界面设计:使用Java的GUI库,例如Swing或JavaFX,设计一个用户友好的界面,包括增加、删除、修改和查询病人信息的按钮。
2. 数据存储:使用Java的文件读写功能,将病人住院信息保存在out.txt文件中。
3. 数据结构:使用Java的集合类,例如ArrayList,存储病人信息,并实现相应的增加、删除、修改和查询操作。
下面是一个简单的实现代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class HospitalManagementSystem extends JFrame implements ActionListener {
private JLabel bedLabel, nameLabel, genderLabel, ageLabel, diseaseLabel;
private JTextField bedField, nameField, genderField, ageField, diseaseField;
private JButton addButton, deleteButton, updateButton, queryButton;
private ArrayList<Patient> patients = new ArrayList<Patient>();
public HospitalManagementSystem() {
super("医院床位信息管理系统");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bedLabel = new JLabel("床位号:");
nameLabel = new JLabel("姓名:");
genderLabel = new JLabel("性别:");
ageLabel = new JLabel("年龄:");
diseaseLabel = new JLabel("病类:");
bedField = new JTextField(20);
nameField = new JTextField(20);
genderField = new JTextField(20);
ageField = new JTextField(20);
diseaseField = new JTextField(20);
addButton = new JButton("增加");
deleteButton = new JButton("删除");
updateButton = new JButton("修改");
queryButton = new JButton("查询");
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
queryButton.addActionListener(this);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6, 2));
panel.add(bedLabel);
panel.add(bedField);
panel.add(nameLabel);
panel.add(nameField);
panel.add(genderLabel);
panel.add(genderField);
panel.add(ageLabel);
panel.add(ageField);
panel.add(diseaseLabel);
panel.add(diseaseField);
panel.add(addButton);
panel.add(deleteButton);
panel.add(updateButton);
panel.add(queryButton);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
new HospitalManagementSystem();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String bed = bedField.getText();
String name = nameField.getText();
String gender = genderField.getText();
String age = ageField.getText();
String disease = diseaseField.getText();
Patient p = new Patient(bed, name, gender, age, disease);
patients.add(p);
saveToFile();
JOptionPane.showMessageDialog(this, "增加成功!");
} else if (e.getSource() == deleteButton) {
String bed = JOptionPane.showInputDialog(this, "请输入床位号:");
for (int i = 0; i < patients.size(); i++) {
if (patients.get(i).getBed().equals(bed)) {
patients.remove(i);
saveToFile();
JOptionPane.showMessageDialog(this, "删除成功!");
return;
}
}
JOptionPane.showMessageDialog(this, "没有找到该病人!");
} else if (e.getSource() == updateButton) {
String bed = JOptionPane.showInputDialog(this, "请输入床位号:");
for (int i = 0; i < patients.size(); i++) {
if (patients.get(i).getBed().equals(bed)) {
String name = JOptionPane.showInputDialog(this, "请输入姓名:");
String gender = JOptionPane.showInputDialog(this, "请输入性别:");
String age = JOptionPane.showInputDialog(this, "请输入年龄:");
String disease = JOptionPane.showInputDialog(this, "请输入病类:");
patients.set(i, new Patient(bed, name, gender, age, disease));
saveToFile();
JOptionPane.showMessageDialog(this, "修改成功!");
return;
}
}
JOptionPane.showMessageDialog(this, "没有找到该病人!");
} else if (e.getSource() == queryButton) {
String bed = JOptionPane.showInputDialog(this, "请输入床位号:");
for (int i = 0; i < patients.size(); i++) {
if (patients.get(i).getBed().equals(bed)) {
JOptionPane.showMessageDialog(this, patients.get(i));
return;
}
}
JOptionPane.showMessageDialog(this, "没有找到该病人!");
}
}
private void saveToFile() {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"));
for (Patient p : patients) {
bw.write(p.toString());
bw.newLine();
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Patient {
private String bed;
private String name;
private String gender;
private String age;
private String disease;
public Patient(String bed, String name, String gender, String age, String disease) {
this.bed = bed;
this.name = name;
this.gender = gender;
this.age = age;
this.disease = disease;
}
public String getBed() {
return bed;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getAge() {
return age;
}
public String getDisease() {
return disease;
}
public String toString() {
return bed + "," + name + "," + gender + "," + age + "," + disease;
}
}
```
代码中使用了Swing库进行界面设计,并使用了ArrayList存储病人信息。增加、删除、修改和查询操作均可以在GUI窗口中完成,并将病人住院信息保存在out.txt文件中。
阅读全文