将患者信息存储到文本文件中,然后加载

时间:2015-06-16 18:26:36

标签: java arrays arraylist text-files

我们的任务是编写一个简单的医院管理系统。用户能够输入患者信息,例如姓名,性别,出生日期和疾病。
数据存储在ArrayList中并保存到文本文件中。

现在我的问题:我需要显示所有输入系统的患者。
但是我无法以正确的方式恢复数据并将其显示在控制台窗口中。
输出应该是:患者姓名,性别,疾病,出生日期。

这就是我所拥有的......这不是整个代码,只是它的某些部分 这是主要的课程..

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;

患者班

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;

}
}

我猜主要课程中存在一些错误(案例3),但我自己无法解决。

我真的很感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

它在第一次启动时从不从磁盘加载数据的原因是因为案例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;