private void saveToFile() { try { BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt")); for (Patient p : p) { bw.write(p.toString()); bw.newLine(); } bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
时间: 2024-02-10 09:20:14 浏览: 76
这是一段Java代码,其中定义了一个名为saveToFile的方法,该方法的作用是将数据保存到文件。该方法通过创建一个BufferedWriter对象,将Patient对象中的数据写入到一个名为out.txt的文件中。这个方法使用了Java 7中引入的try-with-resources结构,因此在文件操作完成后,将自动关闭BufferedWriter对象。如果在写文件时发生IOException异常,将打印异常信息。这段代码的作用是将Patient对象中的数据保存到文件中。
相关问题
根据类图,实现java代码:@startuml class BankAccount { -accountNumber : String -balance : double -ownerName : String +BankAccount(accountNumber:String, ownerName:String) +getAccountNumber():String +getBalance():double +getOwnerName():String +deposit(amount:double):void +withdraw(amount:double):void } class BankAccountDAO { -accounts : Map<String, BankAccount> -fileName : String +BankAccountDAO(fileName:String) +getAccount(accountNumber:String):BankAccount +getAllAccounts():List<BankAccount> +addAccount(account:BankAccount):void +updateAccount(account:BankAccount):void +deleteAccount(accountNumber:String):void -saveToFile():void } BankAccount -> BankAccountDAO @enduml
```
public class BankAccount {
private String accountNumber;
private double balance;
private String ownerName;
public BankAccount(String accountNumber, String ownerName) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance = 0.0;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public String getOwnerName() {
return ownerName;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
}
import java.io.*;
import java.util.*;
public class BankAccountDAO {
private Map<String, BankAccount> accounts;
private String fileName;
public BankAccountDAO(String fileName) {
this.fileName = fileName;
accounts = new HashMap<String, BankAccount>();
loadFromFile();
}
public BankAccount getAccount(String accountNumber) {
return accounts.get(accountNumber);
}
public List<BankAccount> getAllAccounts() {
return new ArrayList<BankAccount>(accounts.values());
}
public void addAccount(BankAccount account) {
accounts.put(account.getAccountNumber(), account);
saveToFile();
}
public void updateAccount(BankAccount account) {
accounts.put(account.getAccountNumber(), account);
saveToFile();
}
public void deleteAccount(String accountNumber) {
accounts.remove(accountNumber);
saveToFile();
}
private void loadFromFile() {
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
String accountNumber = parts[0];
String ownerName = parts[1];
double balance = Double.parseDouble(parts[2]);
BankAccount account = new BankAccount(accountNumber, ownerName);
account.deposit(balance);
accounts.put(accountNumber, account);
}
reader.close();
} catch (IOException e) {
System.out.println("Failed to load accounts from file: " + e.getMessage());
}
}
private void saveToFile() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for (BankAccount account : accounts.values()) {
String line = account.getAccountNumber() + "," + account.getOwnerName() + "," + account.getBalance();
writer.write(line);
writer.newLine();
}
writer.close();
} catch (IOException e) {
System.out.println("Failed to save accounts to file: " + e.getMessage());
}
}
}
```
用Java设计一个医院床位信息管理系统,能够实现病人信息(床位号、姓名、性别、年龄、病类)的增加、删 除、修改、查询以及更新功能。最后,将病人住院信息保存在out.txt。用GUI窗口输出。
好的,我来解答你的问题。首先,设计一个医院床位信息管理系统,需要涉及到以下几个方面:
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文件中。
阅读全文