Store patient information into text file and load it afterwards

2k views Asked by At

We got the task to write a simple Hospital Management System. The user is able to enter the patient information such as name, gender, date of birth and disease.
The data is stored in an ArrayList and saved into a textfile.

Now to my problem: I need to display all patients which were entered into the system.
But I can't get the data back in a correct way and display it in the console window.
Output should be someting like: Patient Name, Gender, Disease, Date of birth.

Here's what I have.. It's not the whole code, just some parts of it.
This is the main 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;

And the patient 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;

}
}

I guess there's some mistakes in the main class (in case 3) but I can not solve it myself.

I would really appreciate your help!

1

There are 1 answers

2
jordan dap On BEST ANSWER

The reason that it never loads the data from disk on first startup is because the first if statment in case 3 checks to see if PatientList.size is empty and if it is do not load data from disk (the else statment only runs if there is somthing in the list)

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;

To solve this you probably want to check if PatientList.isEmpty() and if it is then try and load data from disk and if there is no data then print out no records found.

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;