Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that manages a list of patients for a medical office. Patients s

ID: 3801567 • Letter: W

Question

Write a program

that manages a list of patients for a medical office. Patients should be

represented as objects with the following data members:

name (string)

patient id # (string)

address (string)

height (integer; measured in inches)

weight (double)

date of birth (Date)

date of initial visit (Date)

date of last visit (Date)

The data member “patient id #” is defined to be a

key

. That is, no two patients can have

the same patient id #. In addition to the standard set of accessors for the above data

members, define the fol

lowing methods for class Patient.

standard set of accessors

get_age

method: to compute and returns a patient’s age in years (integer)

get_time_as_patient

method: to compute the number of years (integer) since

the patient’s initial visit. Note that this va

lue can be 0.

get_time_since_last_visit

method:

to compute the number of years (integer)

since the patient’s last visit. This value can be 0, too.

Your program will create a list of patient objects and provide the user with a menu of

choices for accessing

and manipulating the

data on that list. The list must be an object of

the class List that you will define.

Internally, the list object must maintain its list as a

singly linked list with two references, one for head and one for tail.

As usual, your Li

st

class will have the methods “

find,” “

size,” “contains

,” “remove,”

“add,”, “get,”

“getNext,”, “reset,” “toString

,”. At the start, your program should read in patient data

from a text file for an initial set of patients for the list. The name of this file

should be

included on the “command line” when the program is run.

(Don’t hard code

the file name)

Each data item for a patient will appear on a separate line in

the file.

Your program

should be menu-

driven, meaning that it will display a menu of options for the user. The

user will choose one of

these options, and your program will carry out the request. The

program will then display the same menu again and get another

choice from the user.

This interaction will go on until the user chooses QUIT, which should be the last of the

menu’s options. The

menu should look something like the following:

1.

Display list

2.

Add a new patient

3.

Show information for a patient

4.

Delete a patient

5.

Show average patient age

6.

Show information for the youngest patient

7.

Show notification l

ist

8.

Quit

Enter your choice:

Details of each option:

Option 1: Display (on the screen) the names and patient id #’s of all patients in

order starting from the first one. Display the

information for one patient per line;

something like: Susan

Smith, 017629

Option

2: Add a new patient to the

END

of the list.

All

information about the new

patient (including name, patient id #, etc.)

is to be requested (input) from the user

interactively. That is, you will need to ask for 14 pieces of data from the user.

You’ll, of course, need to create a new patient object to hold this data.

NOTE:

As mentioned above, the patient id # field is a

key

. So, if the user types in

a patient id # that happens to be the same as

an already existing patient’s, then

you should display an error message and cancel the operation. Therefore, it is

probably a

good idea to ask for the patient id # first and test it immediately (by

scanning the objects on the list).

Option

3: Display (in a neat format) all the information pertaining to the patien

t

whose patient id # is given by the user. Namely, display the following information:

o

name

o

patient id #

o

address

o

height (shown in feet and inches; for example, 5 ft, 10 in)

o

weight

o

age

o

number of years as a patient (display “less than one year” if 0)

o

number of years since last visit (display “less than one year” if 0)

o

Indication that patient is overdue for a visit

NOTE:

The last item is displayed only if it has been 3 or more years since

the patient’s last visit.

If the user inputs a patient id

# that does

not

exist, then the program should

display an error message and the operation should be canceled (with the menu

immediately being displayed again for another request).

Option

4: Delete the patient whose id # is given by the user. If the patient is not

on the

list, display an error message.

Option 5: Show the average age (to one decimal place) of the patients.

Option

6:

Display (in a neat format) all the information (same as operation 3)

about the youngest patient.

Option

7: Display the names (and patient id

#’s) of all patients who are overdue

for a visit. As noted above, “overdue” is

defined as 3 or more years since the last

visit.

Option 8: Quit the program.

NOTE:

When the user chooses to quit, you should ask if they would like to save

the patient information to a file. If so, then

you should prompt for the name of an

output (text) file, and then write the data pertaining to

all

patients to that file. The

output for each patient should be in the same format as in the input file. In this

way, your output fil

e can be used as input on

another run of your program. Make

certain to maintain the order of the patients in the output file as they appear on the

list. Be

careful not to overwrite your original input file (or any other file, for that

matter).

Note

:

Try to

implement the various menu options as separate methods (aside

from

“main”)

.

However:

DO NOT DEFINE such “option methods

” as part of the class

List.

Of course, the Java code that implements an option (whether it’s in the “main”

method or not) should def

initely use List’s methods

to help do its job.

Explanation / Answer

/*
* I did the complete designing and also took care of file input and output, all you have to * do is to implement only one method - addPatientInfo() which i have intentionally left

* blank. Just use the PatientList object and do the required operations
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class HospitalManagement {
      
       public static void main(String args[]) throws IOException {
           String inputFileName = "/home/zubair/Desktop/a.txt";
           new HospitalManagement(inputFileName);
       }

       private PatientList patientLogData;
      
       public HospitalManagement(String inputFilePath) throws IOException {
           patientLogData = loadFromFile(inputFilePath);
           start();
       }
      
       public void start() throws IOException {
           Scanner scanner = new Scanner(System.in);          
          
           while (true) {
               displayMenu();
               String choice = scanner.nextLine().toUpperCase();
              
               switch(choice) {
                   case "A" :
                       displayList();
                       break;
                      
                   case "B" :
                       addNewPatient(scanner);
                       break;
                      
                   case "C" :
                       showInfoOfAPatient(scanner);
                       break;
                      
                   case "D" :
                       deleteAPatient(scanner);
                       break;
                      
                   case "E" :
                       showAverageAgeOfPatient();
                       break;
                      
                   case "F" :
                       showInfoOfYoungestPatient();
                       break;
                      
                   case "G" :
                       showNotificationList();
                       break;
                      
                   case "H" :
                       Quit(scanner);
                       scanner.close();
                       return;
                   default :
                       System.out.println(System.lineSeparator());
                       System.out.println("Invalid Choice");  
                       System.out.println(System.lineSeparator());
               }
           }
       }
      
       private void displayList() {
           List<PatientInfo> patientList = patientLogData.getList();
           for (PatientInfo patient : patientList) {
               System.out.println(patient.getName() + " " + patient.getId());
           }
       }

       private void deleteAPatient(Scanner scanner) {
           System.out.println("Enter id of the patient : ");
           String id = scanner.nextLine();
           patientLogData.remove(id);
       }

      
       @SuppressWarnings("deprecation")
       private void showAverageAgeOfPatient() {          
           List<PatientInfo> patientList = patientLogData.getList();
           Date currDate = new Date(System.currentTimeMillis());

           int averageAge = 0;
           for (PatientInfo patient : patientList) {
               averageAge += (currDate.getYear() - patient.getDateOfBirth().getYear());
           }
           if (patientList.size()!=0)
               averageAge /= patientList.size();
          
           System.out.println(System.lineSeparator());
           System.out.println("Average age of the patients is : "+ averageAge + " years");
           System.out.println(System.lineSeparator());
       }

       private void showInfoOfAPatient(Scanner scanner) {
           System.out.println("Enter id of the patient : ");
           String id = scanner.nextLine();
           PatientInfo patient = patientLogData.find(id);
           if (patient != null) {
               System.out.println(patient.toString());
           }
           else {
               System.out.println("paitent not found");
           }
           System.out.println(System.lineSeparator());

       }

       @SuppressWarnings("deprecation")
       private void showInfoOfYoungestPatient() {
           List<PatientInfo> patientList = patientLogData.getList();
           Date currDate = new Date(System.currentTimeMillis());

           if (patientList.size() <= 0) {
               System.out.println(System.lineSeparator());
               System.out.println("No patient data is available");
               return;
           }
           PatientInfo youngPatient = patientList.get(0);
           int smallest = (currDate.getYear() - youngPatient.getDateOfBirth().getYear());

           for (PatientInfo patient : patientList) {
               int age = (currDate.getYear() - patient.getDateOfBirth().getYear());
               if (age < smallest) {
                   smallest = age;
                   youngPatient = patient;
               }
           }
          
           System.out.println(System.lineSeparator());
           System.out.println("Youngest patient is : "+ youngPatient);
           System.out.println(System.lineSeparator());
       }

       @SuppressWarnings("deprecation")
       private void showNotificationList() {
           List<PatientInfo> patientList = patientLogData.getList();
           Date currDate = new Date(System.currentTimeMillis());

           for (PatientInfo patient : patientList) {
               int time = (currDate.getYear() - patient.getLastVisit().getYear());
               if (time >= 3) {
                   System.out.println(patient.getId()+", "+patient.getName()+", is overdue for a visit , been " + time + " years");
               }
           }
       }

       private void addNewPatient(Scanner scanner) {
          
       }

       public void Quit(Scanner sc) throws IOException {
           System.out.println(System.lineSeparator());
           System.out.print("Do you wan to write the output (Y/N)? : ");
          
           while (true) {
               String userChoice = sc.nextLine();
              
               if (userChoice.equalsIgnoreCase("Y")) {
                   System.out.print("Enter output file : ");
                   writeBackToFile(sc.nextLine(), patientLogData);
                   return;
               }
               else if (userChoice.equalsIgnoreCase("N")) {
                   return;
               }
               else {
                   System.out.println("Invalid Option");
                   System.out.println(System.lineSeparator());
               }
           }
       }
      
       /**
       * Reads the patient data from a given file
       * @param filePath
       * @return
       * @throws IOException
       */
       public PatientList loadFromFile(String filePath) throws IOException {
           PatientList patientLogData = new PatientList();
           BufferedReader bufferedInput = new BufferedReader(new FileReader(filePath));
          
           String inputLine;
           while ((inputLine = bufferedInput.readLine()) != null) {
               String[] patientData = inputLine.split(" ");
              
               String id = patientData[0];
               String name = patientData[1];
               Date doB = new Date(Long.valueOf(patientData[2]));
               String address = patientData[3];
               int height = Integer.valueOf(patientData[4]);
               double weight = Double.valueOf(patientData[5]);
               Date lastVisit = new Date(Long.valueOf(patientData[6]));          
               Date initialVisit = new Date(Long.valueOf(patientData[7]));
              
               PatientInfo patient = new PatientInfo(name, id, doB, address, height, weight, initialVisit, lastVisit);
               patientLogData.add(patient);
           }
          
           bufferedInput.close();
           return patientLogData;
       }

      
       /**
       * Writes the output to a file
       * @param filePath
       * @param patientList
       * @throws IOException
       */
       public void writeBackToFile(String filePath, PatientList patientList) throws IOException {
           if (filePath == null || filePath.isEmpty()) {
               return;
           }
           BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
          
           for (PatientInfo patient : patientList.getList()) {
               String id = patient.getId();
               String name = patient.getName();
               int height = patient.getHeight();
               double weight = patient.getWieght();
               String address = patient.getAddress().replaceAll(" ", "-");
               Date doB = patient.getDateOfBirth();
               Date lastVisit = patient.getLastVisit();              
               Date initialVisit = patient.getIntialVisit();
               bufferedWriter.write(id + " " + name + " " + doB.getTime() + " " + address + " " + height + " " + weight
                   + " " + lastVisit.getTime() + " " + initialVisit.getTime() + " ");
           }
           bufferedWriter.close();
       }
      
       public void displayMenu() {  
           System.out.println(System.lineSeparator());
           System.out.println(System.lineSeparator());
           System.out.println("Menu");
           System.out.println("A.Display List");
           System.out.println("B.Add a new patient");
           System.out.println("C.Show info for a patient");          
           System.out.println("D.Delete a patient");          
           System.out.println("E.Show average age of patient");          
           System.out.println("F.Show info of youngest patient");
           System.out.println("G.Show notification list");
           System.out.println("H.Quit");
           System.out.print("Enter your choice : " );
       }
      
}

class PatientList {

   private List<PatientInfo> patientList;
  
   public PatientList() {
       patientList = new ArrayList<>();
   }
  
   public List<PatientInfo> getList() {
       return patientList;
   }
  
   public PatientInfo find(String patientID) {
       for (PatientInfo patient : patientList) {
           if (patient.getId().equalsIgnoreCase(patientID)) {
               return patient;
           }
       }
       return null;
   }
  
   public int find(PatientInfo inputPatient) {
       int i=0;
       for (PatientInfo patient : patientList) {
           if (patient.getId().equalsIgnoreCase(inputPatient.getId())) {
               return i;
           }
           i++;
       }
       return -1;
   }
  
   public void remove(String patientID) {
       PatientInfo element = null;
       for (PatientInfo patient : patientList) {
           if (patient.getId().equalsIgnoreCase(patientID)) {
               element = patient;
               break;
           }
       }
       if (element != null) {
           patientList.remove(element);
       }
   }

   public boolean contains(PatientInfo patient) {
       if (find(patient.getId()) == null) {
           return false;
       }
       return true;
   }

   public void remove(PatientInfo patient) {
       patientList.remove(patient);
   }

   public void add(PatientInfo patient) {
       patientList.add(patient);
   }

   public int size() {
       return patientList.size();
   }
  
   @Override
   public String toString() {
       StringBuffer buffer = new StringBuffer();
       for (PatientInfo patient : patientList) {
           buffer.append(patient.toString() + System.lineSeparator());
       }
       return buffer.toString();
   }
}

class PatientInfo {
  
   private String name;
   private String id;
   private Date dateOfBirth;
   private String address;
   private int height;
   private double wieght;
   private Date intialVisit;
   private Date lastVisit;
  
   public PatientInfo(String name, String id, Date dateOfBirth, String address, int height, double wieght,
           Date intialVisit, Date lastVisit) {
       super();
       this.name = name;
       this.id = id;
       this.dateOfBirth = dateOfBirth;
       this.address = address;
       this.height = height;
       this.wieght = wieght;
       this.intialVisit = intialVisit;
       this.lastVisit = lastVisit;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getId() {
       return id;
   }

   public void setId(String id) {
       this.id = id;
   }

   public String getAddress() {
       return address;
   }

   public void setAddress(String address) {
       this.address = address;
   }

   public int getHeight() {
       return height;
   }

   public void setHeight(int height) {
       this.height = height;
   }

   public double getWieght() {
       return wieght;
   }

   public void setWieght(double wieght) {
       this.wieght = wieght;
   }

   public Date getDateOfBirth() {
       return dateOfBirth;
   }

   public void setDateOfBirth(Date dateOfBirth) {
       this.dateOfBirth = dateOfBirth;
   }

   public Date getIntialVisit() {
       return intialVisit;
   }

   public void setIntialVisit(Date intialVisit) {
       this.intialVisit = intialVisit;
   }

   public Date getLastVisit() {
       return lastVisit;
   }

   public void setLastVisit(Date lastVisit) {
       this.lastVisit = lastVisit;
   }

   @Override
   public String toString() {
       return "PatientInfo [name=" + name + ", id=" + id + ", address=" + address + ", height=" + height + ", wieght="
               + wieght + ", dateOfBirth=" + dateOfBirth + ", intialVisit=" + intialVisit + ", lastVisit=" + lastVisit+ "]";
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote