Write a program that manages a list of patients for a medical office. Patients s
ID: 3805504 • 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
Patient.java:
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Patient {
private String name, patientId, address;
private int height;
private double weight;
private LocalDate dob, doInitial, doLast;
public Patient(String name, String patientId, String address, int height,
double weight, LocalDate dob, LocalDate doInitial, LocalDate doLast) {
this.name = name;
this.patientId = patientId;
this.address = address;
this.height = height;
this.weight = weight;
this.dob = dob;
this.doInitial = doInitial;
this.doLast = doLast;
}
public String getName() {
return name;
}
public String getPatientId() {
return patientId;
}
public String getAddress() {
return address;
}
public int getHeight() {
return height;
}
public double getWeight() {
return weight;
}
public LocalDate getDob() {
return dob;
}
public LocalDate getDoInitial() {
return doInitial;
}
public LocalDate getDoLast() {
return doLast;
}
public int get_age() {
return (int) ChronoUnit.YEARS.between(LocalDate.now(), dob);
}
public int get_time_as_patient() {
return (int) ChronoUnit.YEARS.between(LocalDate.now(), doInitial);
}
public int get_time_since_last_visit() {
return (int) ChronoUnit.YEARS.between(LocalDate.now(), doLast);
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Name: " + name);
sb.append(",Id: " + patientId);
sb.append(",Address: " + address);
sb.append(",H: " + height);
sb.append(",W: " + weight);
sb.append(",last Visit: " + doLast);
return sb.toString();
}
public boolean equals(Patient patient) {
return patient.patientId.equals(patientId);
}
}
MyList.java:
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class MyList<Item extends Patient> {
private Node<Item> first; // top of stack
private Node<Item> last; // top of stack
private int n; // size of the stack
// helper linked list class
private static class Node<Item extends Patient> {
private Item item;
private Node<Item> next;
}
public MyList() {
first = last = null;
n = 0;
}
public int size() {
return n;
}
public boolean empty() {
return first == null;
}
public Item add(Item item) {
Node<Item> oldLast = last;
last = new Node<Item>();
last.item = item;
last.next = null;
if (oldLast == null) {
first = last;
} else {
oldLast.next = last;
}
n++;
return item;
}
public Item get(int index) {
if(n==0 || index < 0 || index >= n) {
throw new IllegalArgumentException("Invalid index");
}
Node<Item> p = first;
while(p != null) {
if(--index == 0) {
return p.item;
}
p = p.next;
}
return null;
}
public void reset() {
last = first = null;
n = 0;
}
public String toString() {
StringBuilder sb = new StringBuilder();
Node<Item> p = first;
while(p != null) {
sb.append(p.item + " ");
p = p.next;
}
return sb.toString();
}
public boolean contains(Item item) {
return (find(item) != -1);
}
public boolean remove(Item item) {
Node<Item> p = first;
if(n==0) {
return false;
}
if(n==1){
if(p.item.equals(item)) {
reset();
} else {
return false;
}
}
Node<Item> prev = null;
while(p != null) {
if(p.item.equals(item)) {
if(prev != null) {
prev.next = p.next;
} else {
first = p.next;
}
if(p == last) {
last = prev;
}
return true;
}
prev = p;
p = p.next;
}
return false;
}
public int find(Item item) {
int i = 1;
Node<Item> p = first;
while (p != null) {
if (p.item.equals(item)) {
return i;
}
i++;
p = p.next;
}
return -1;
}
public int find(String patientId) {
int i = 1;
Node<Item> p = first;
while (p != null) {
if (p.item.getPatientId().equals(patientId)) {
return i;
}
i++;
p = p.next;
}
return -1;
}
public double findAverageAge() {
int ageSum = 0;
int totalPatients = 0;
Node<Item> p = first;
while (p != null) {
ageSum = p.item.get_age();
totalPatients++;
p = p.next;
}
return ageSum/Double.valueOf(totalPatients);
}
public Item youngestPatient() {
Node<Item> p = first;
Node<Item> youngest = p;
while (p != null) {
if(youngest.item.get_age() > p.item.get_age())
youngest = p;
p = p.next;
}
return youngest.item;
}
public void displayNotifications() {
Node<Item> p = first;
while (p != null) {
if(ChronoUnit.YEARS.between(LocalDate.now(), p.item.getDoLast()) > 3)
System.out.println(p.item);
p = p.next;
}
}
}
PatientDemo.java:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Scanner;
public class PatientDemo {
static Scanner s = new Scanner(System.in);
static int showMenu() {
boolean valid = false;
int choice = 0;
while(!valid) {
System.out.println("Select:");
System.out.println("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 list 8. Quit");
choice = Integer.parseInt(s.next());
if(choice >= 1 && choice <= 8) {
valid = true;
}
}
return choice;
}
public static void main(String[] args) {
MyList<Patient> patientList = new MyList<>();
int menu = showMenu();
while(menu != 8) {
if(menu == 1) {
System.out.println("Patient List");
System.out.println(patientList);
} else if(menu == 2) {
System.out.println("Add Patient");
addNewPatient(patientList);
} else if(menu == 3) {
System.out.print("Enter patient Id: ");
String id = s.next();
try {
Patient p = patientList.get(patientList.find(id));
System.out.println(p);
} catch (Exception e) {
System.out.println("patient Doesn't exist");
}
} else if(menu == 4) {
System.out.print("Enter patient Id: ");
String id = s.next();
try {
Patient p = patientList.get(patientList.find(id));
patientList.remove(p);
} catch (Exception e) {
System.out.println("patient Doesn't exist");
}
} else if(menu == 5) {
System.out.print("Average Patient Age: " + patientList.findAverageAge());
} else if(menu == 6) {
System.out.print("Youngest Patient: " + patientList.youngestPatient());
} else if(menu == 7) {
System.out.println("Notifications:");
patientList.displayNotifications();
}
System.out.println();
menu = showMenu();
}
// please implement the logic for writing to file your self using toString() method of List
}
private static void addNewPatient(MyList<Patient> patientList) {
SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");
System.out.println("Enter PatientId");
String id = s.next();
while(doesPatiendIdExists(patientList, id)) {
System.out.println("patient Id exists. Enter again.");
id = s.next();
}
System.out.println("Enter Name:");
String name = s.next();
System.out.println("Enter Address:");
String address = s.next();
System.out.println("Enter Height:");
int h = Integer.parseInt(s.next());
System.out.println("Enter Weight:");
int w = Integer.parseInt(s.next());
System.out.println("Enter Dob(dd-mm-yyyy):");
boolean valid = false;
LocalDate dob= null;
while(!valid) {
try {
dob = Instant.ofEpochMilli(df.parse(s.next()).getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
valid = true;
} catch (ParseException e) {
System.out.println("inavlid date");
}
}
Patient p = new Patient(name, id, address, h, w, dob, LocalDate.now(), LocalDate.now());
patientList.add(p);
}
static boolean doesPatiendIdExists(MyList<Patient> patientList, String patientId) {
return (patientList.find(patientId) != -1);
}
}
As your question is very very long, and we get just 2 hours to complete the question, its not possible to implement everything which you asked for. I have tried to give you most of the code, however i could not test it due to time constraint. It misses the logic for adding the patients from file and writing them to file. Please write them by yourself.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.