将患者信息存储到文本文件中并在之后加载
Store patient information into text file and load it afterwards
我们接到了编写一个简单的医院管理系统的任务。用户能够输入患者信息,例如姓名、性别、出生日期和疾病。
数据存储在 ArrayList 中并保存到文本文件中。
现在我的问题是:我需要显示输入系统的所有患者。
但是我无法以正确的方式取回数据并将其显示在控制台中 window。
输出应该类似于:患者姓名、性别、疾病、出生日期。
这是我的代码。这不是全部代码,只是其中的一部分。
这是主要的class..
import java.io.*;
import java.util.*;
public class Main {
public static void main (String args []) {
int choice = 0;
List<Patient> PatientList = new ArrayList<Patient>();
Patient patient = new Patient();
List<Doctor> DoctorList = new ArrayList<Doctor>();
Doctor doctor = new Doctor();
Scanner readInput = new Scanner(System.in);
Scanner readChoice = new Scanner(System.in);
do {
System.out.println("Press 1 to enter a new patient \nPress 2 to enter a new doctor \nPress 3 to show all patients \nPress 4 to show all doctors \nPress 0 to quit.");
choice = readChoice.nextInt();
switch (choice) {
//case 0:
case 1: System.out.println("Please enter the name of the new patient: ");
patient.setPatientName(readInput.nextLine());
System.out.println("Please enter the gender of the new patient: ");
patient.setPatientGender(readInput.nextLine());
System.out.println("Please enter the disease of the new patient: ");
patient.setDisease(readInput.nextLine());
System.out.println("Please enter the age of the new patient: ");
patient.setPatientDateOfBirth(readInput.nextLine());
PatientList.add(patient);
try {
FileOutputStream fos = new FileOutputStream("patients.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(PatientList);
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
/////////////////////////////////////////////////////////////////
//case 2 is not relevant to my question therefore I did not put it in here
case 3: if(PatientList.size() == 0) {
System.out.println("\nNo patients were found...\nReturning to main menu...\n");
}
else {
try {
FileInputStream fis = new FileInputStream("patients.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
PatientList =(ArrayList<Patient>) ois.readObject();
ois.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < PatientList.size(); i++) {
System.out.println(PatientList.get(i).getPatientName()+", "+PatientList.get(i).getPatientDateOfBirth()+", "
+PatientList.get(i).getPatientGender()+", "+PatientList.get(i).getDisease());
}
}
break;
还有患者class
import java.io.Serializable;
public class Patient implements Serializable {
private String patientName;
private String patientGender;
private String disease;
private String patientDateOfBirth;
public Patient (String patientName, String patientGender, String disease, String patientDateOfBirth) {
this.patientName = patientName;
this.patientGender = patientGender;
this.disease = disease;
this.patientDateOfBirth = patientDateOfBirth;
}
public Patient() {
}
public String getPatientName () {
return patientName;
}
public void setPatientName (String patientName) {
this.patientName = patientName;
}
public String getPatientGender () {
return patientGender;
}
public void setPatientGender (String patientGender) {
this.patientGender = patientGender;
}
public String getDisease () {
return disease;
}
public void setDisease (String disease) {
this.disease = disease;
}
public String getPatientDateOfBirth () {
return patientDateOfBirth;
}
public void setPatientDateOfBirth (String patientDateOfBirth) {
this.patientDateOfBirth = patientDateOfBirth;
}
}
我猜主要有一些错误 class (情况 3),但我自己无法解决。
非常感谢您的帮助!
它从不在第一次启动时从磁盘加载数据的原因是因为案例 3 中的第一个 if 语句检查 PatientList.size 是否为空,如果是则不从磁盘加载数据( else 语句仅在列表中有内容时运行)
case 3:
//this if statment is checking if PatientList.size() is 0 or in other words check if it is empty
if(PatientList.size() == 0)
{
System.out.println("\nNo patients were found...\nReturning to main menu...\n");
}
//this else code block will only run if the above if statment is false
else
{
try
{
//load data from the hard drive
} catch (FileNotFoundException e) {
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}
}
break;
要解决这个问题,您可能需要检查 PatientList.isEmpty() 是否存在,如果存在,则尝试从磁盘加载数据,如果没有数据,则打印出未找到的记录。
case 3:
if(PatientList.isEmpty())
{
try
{
//load data from the hard drive
} catch (FileNotFoundException e) {
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}
//populate PatientList if data was loaded from disk here
//if PatientList is still empty then print out no patients found
if(PatientList.isEmpty())
{
System.out.println("\nNo patients were found...\nReturning to main menu...\n");
}
}
break;
我们接到了编写一个简单的医院管理系统的任务。用户能够输入患者信息,例如姓名、性别、出生日期和疾病。
数据存储在 ArrayList 中并保存到文本文件中。
现在我的问题是:我需要显示输入系统的所有患者。
但是我无法以正确的方式取回数据并将其显示在控制台中 window。
输出应该类似于:患者姓名、性别、疾病、出生日期。
这是我的代码。这不是全部代码,只是其中的一部分。
这是主要的class..
import java.io.*;
import java.util.*;
public class Main {
public static void main (String args []) {
int choice = 0;
List<Patient> PatientList = new ArrayList<Patient>();
Patient patient = new Patient();
List<Doctor> DoctorList = new ArrayList<Doctor>();
Doctor doctor = new Doctor();
Scanner readInput = new Scanner(System.in);
Scanner readChoice = new Scanner(System.in);
do {
System.out.println("Press 1 to enter a new patient \nPress 2 to enter a new doctor \nPress 3 to show all patients \nPress 4 to show all doctors \nPress 0 to quit.");
choice = readChoice.nextInt();
switch (choice) {
//case 0:
case 1: System.out.println("Please enter the name of the new patient: ");
patient.setPatientName(readInput.nextLine());
System.out.println("Please enter the gender of the new patient: ");
patient.setPatientGender(readInput.nextLine());
System.out.println("Please enter the disease of the new patient: ");
patient.setDisease(readInput.nextLine());
System.out.println("Please enter the age of the new patient: ");
patient.setPatientDateOfBirth(readInput.nextLine());
PatientList.add(patient);
try {
FileOutputStream fos = new FileOutputStream("patients.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(PatientList);
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
/////////////////////////////////////////////////////////////////
//case 2 is not relevant to my question therefore I did not put it in here
case 3: if(PatientList.size() == 0) {
System.out.println("\nNo patients were found...\nReturning to main menu...\n");
}
else {
try {
FileInputStream fis = new FileInputStream("patients.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
PatientList =(ArrayList<Patient>) ois.readObject();
ois.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < PatientList.size(); i++) {
System.out.println(PatientList.get(i).getPatientName()+", "+PatientList.get(i).getPatientDateOfBirth()+", "
+PatientList.get(i).getPatientGender()+", "+PatientList.get(i).getDisease());
}
}
break;
还有患者class
import java.io.Serializable;
public class Patient implements Serializable {
private String patientName;
private String patientGender;
private String disease;
private String patientDateOfBirth;
public Patient (String patientName, String patientGender, String disease, String patientDateOfBirth) {
this.patientName = patientName;
this.patientGender = patientGender;
this.disease = disease;
this.patientDateOfBirth = patientDateOfBirth;
}
public Patient() {
}
public String getPatientName () {
return patientName;
}
public void setPatientName (String patientName) {
this.patientName = patientName;
}
public String getPatientGender () {
return patientGender;
}
public void setPatientGender (String patientGender) {
this.patientGender = patientGender;
}
public String getDisease () {
return disease;
}
public void setDisease (String disease) {
this.disease = disease;
}
public String getPatientDateOfBirth () {
return patientDateOfBirth;
}
public void setPatientDateOfBirth (String patientDateOfBirth) {
this.patientDateOfBirth = patientDateOfBirth;
}
}
我猜主要有一些错误 class (情况 3),但我自己无法解决。
非常感谢您的帮助!
它从不在第一次启动时从磁盘加载数据的原因是因为案例 3 中的第一个 if 语句检查 PatientList.size 是否为空,如果是则不从磁盘加载数据( else 语句仅在列表中有内容时运行)
case 3:
//this if statment is checking if PatientList.size() is 0 or in other words check if it is empty
if(PatientList.size() == 0)
{
System.out.println("\nNo patients were found...\nReturning to main menu...\n");
}
//this else code block will only run if the above if statment is false
else
{
try
{
//load data from the hard drive
} catch (FileNotFoundException e) {
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}
}
break;
要解决这个问题,您可能需要检查 PatientList.isEmpty() 是否存在,如果存在,则尝试从磁盘加载数据,如果没有数据,则打印出未找到的记录。
case 3:
if(PatientList.isEmpty())
{
try
{
//load data from the hard drive
} catch (FileNotFoundException e) {
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}
//populate PatientList if data was loaded from disk here
//if PatientList is still empty then print out no patients found
if(PatientList.isEmpty())
{
System.out.println("\nNo patients were found...\nReturning to main menu...\n");
}
}
break;