Question
Download the complete emergency room program and add the following features to it: 1.The size of the patient list is calculated when it is printed.
Download the complete emergency room program and add the following features to it:
1.The size of the patient list is calculated when it is printed. But there may be occasions where you need the size for something else, such as to track how many people are waiting throughout a day. Add a size instance variable that is updated each time the size of the list changes, and a size() getter method that returns its value (do not recalculate the size each time the method is called). Replace the calculation in print() with this instance variable to test your work.
2.The patient list is ordered in reverse order of arrival. It would be nice if it were stored in a more meaningful order. Modify the add method so that new patients are stored in the list first in order of severity (highest to lowest), and for those with the same severity, in increasing order of arrival time. You will need to add a new method to Patient.
Submit your complete revised PatientList and Patient classes by the due date specified in the course schedule.
public class Activity9A {
public static void main(String[] args) {
PatientList emergency = new PatientList();
emergency.add("Zelma", 1);
emergency.add("Clayton", 2);
emergency.add("Casper", 3);
emergency.add("Ihor", 1);
emergency.add("Edwina", 3);
emergency.print();
System.out.println(" End of processing.");
}
}
class Patient {
private String name;
private int arrival;
private int severity;
public Patient(String name, int arrival, int severity) {
this.name = name;
this.arrival = arrival;
this.severity = severity;
}
public String toString() {
return name + " arrived at " + arrival + " with severity " + severity;
}
}
class PatientList {
private PatientNode head;
private int lastArrival;
public PatientList() {
head = null;
lastArrival = 1;
}
public void add(String name, int severity) {
Patient patient;
patient = new Patient(name, lastArrival, severity);
lastArrival++;
head = new PatientNode(patient, head);
}
public void print() {
PatientNode current;
int size = 0;
current = head;
while (current != null) {
System.out.println(current.data);
size++;
current = current.next;
}
System.out.println("Size = " + size);
System.out.println("Last arrival = " + size);
}
}
class PatientNode {
public Patient data;
public PatientNode next;
public PatientNode(Patient data, PatientNode next) {
this.data = data;
this.next = next;
}
}
public class Activity9B {
public static void main(String[] args) {
PatientList emergency = new PatientList();
emergency.add("Zelma", 1);
System.out.println("Next admission: " + emergency.nextAdmission());
emergency.add("Clayton", 2);
System.out.println("Next admission: " + emergency.nextAdmission());
emergency.add("Casper", 3);
System.out.println("Next admission: " + emergency.nextAdmission());
emergency.add("Ihor", 1);
System.out.println("Next admission: " + emergency.nextAdmission());
emergency.add("Edwina", 3);
System.out.println("Next admission: " + emergency.nextAdmission());
emergency.print();
System.out.println(" End of processing.");
}
}
class Patient {
private String name;
private int arrival;
private int severity;
public Patient(String name, int arrival, int severity) {
this.name = name;
this.arrival = arrival;
this.severity = severity;
}
public boolean isAdmittedBefore(Patient other, int lastArrival) {
boolean before;
int priority, otherPriority;
if (severity == 3) {
// admitted before, unless the other's severity is 3 and arrived earlier
before = (other.severity != 3) || (other.arrival > arrival);
} else if (other.severity == 3) {
before = false;
} else {
priority = (lastArrival - arrival) * severity;
otherPriority = (lastArrival - other.arrival) * other.severity;
before = (priority > otherPriority) ||
((priority == otherPriority) && other.arrival > arrival);
}
return before;
}
public String toString() {
return name + " arrived at " + arrival + " with severity " + severity;
}
}
class PatientList {
private PatientNode head;
private int lastArrival;
public PatientList() {
head = null;
lastArrival = 1;
}
public void add(String name, int severity) {
Patient patient;
patient = new Patient(name, lastArrival, severity);
lastArrival++;
head = new PatientNode(patient, head);
}
public Patient nextAdmission() {
PatientNode current;
Patient toAdmit = null;
current = head;
while (current != null) {
if (toAdmit == null) {
toAdmit = current.data;
} else {
if (current.data.isAdmittedBefore(toAdmit, lastArrival)) {
toAdmit = current.data;
}
}
current = current.next;
}
return toAdmit;
}
public void print() {
PatientNode current;
int size = 0;
current = head;
while (current != null) {
System.out.println(current.data);
size++;
current = current.next;
}
System.out.println("Size = " + size);
System.out.println("Last arrival = " + size);
}
}
class PatientNode {
public Patient data;
public PatientNode next;
public PatientNode(Patient data, PatientNode next) {
this.data = data;
this.next = next;
}
}
public class Activity9C {
public static void main(String[] args) {
String[] names = { "Zelma", "Clayton", "Casper", "Ihor", "Edwina" };
int[] severities = { 1, 2, 3, 1, 3 };
PatientList list;
list = new PatientList();
testPatientList(list, names, severities, 0);
System.out.println(" End of processing.");
}
public static void testPatientList(PatientList list, String[] names, int[] severities, int pos) {
PatientList copy;
if (pos < names.length) {
list.add(names[pos], severities[pos]);
copy = list.clone();
copy.print();
System.out.println("Admitting: " + copy.nextAdmission());
System.out.println();
testPatientList(list, names, severities, pos + 1);
testPatientList(copy, names, severities, pos + 1);
}
}
}
class Patient {
private String name;
private int arrival;
private int severity;
public Patient(String name, int arrival, int severity) {
this.name = name;
this.arrival = arrival;
this.severity = severity;
}
public boolean isAdmittedBefore(Patient other, int lastArrival) {
boolean before;
int priority, otherPriority;
if (severity == 3) {
// admitted before, unless the other's severity is 3 and arrived earlier
before = (other.severity != 3) || (other.arrival > arrival);
} else if (other.severity == 3) {
before = false;
} else {
priority = (lastArrival - arrival) * severity;
otherPriority = (lastArrival - other.arrival) * other.severity;
before = (priority > otherPriority) ||
((priority == otherPriority) && other.arrival > arrival);
}
return before;
}
public String toString() {
return name + " arrived at " + arrival + " with severity " + severity;
}
}
class PatientList {
private PatientNode head;
private int lastArrival;
public PatientList() {
head = null;
lastArrival = 0;
}
public void add(String name, int severity) {
Patient patient;
lastArrival++;
patient = new Patient(name, lastArrival, severity);
head = new PatientNode(patient, head);
}
public Patient nextAdmission() {
PatientNode current;
PatientNode previous;
PatientNode toAdmitCurrent = null;
PatientNode toAdmitPrevious = null;
current = head;
previous = null;
while (current != null) {
if (toAdmitCurrent == null) {
toAdmitCurrent = current;
} else {
if (current.data.isAdmittedBefore(toAdmitCurrent.data, lastArrival)) {
toAdmitCurrent = current;
toAdmitPrevious = previous;
}
}
previous = current;
current = current.next;
}
if (toAdmitCurrent != null) {
if (toAdmitPrevious == null) {
head = toAdmitCurrent.next;
} else {
toAdmitPrevious.next = toAdmitCurrent.next;
}
return toAdmitCurrent.data;
} else {
return null;
}
}
public void print() {
PatientNode current;
int size = 0;
current = head;
while (current != null) {
System.out.println(current.data);
size++;
current = current.next;
}
System.out.println("Size = " + size);
System.out.println("Last arrival = " + lastArrival);
}
public PatientList clone() {
PatientList copy;
PatientNode current;
PatientNode copyCurrent;
PatientNode newNode;
copy = new PatientList();
current = head;
copyCurrent = null;
while (current != null) {
newNode = new PatientNode(current.data, null);
if (copyCurrent == null) {
copy.head = newNode;
} else {
// last node in copy points to the new node
copyCurrent.next = newNode;
}
// move to the next node in both lists
copyCurrent = newNode;
current = current.next;
}
copy.lastArrival = lastArrival;
return copy;
}
}
class PatientNode {
public Patient data;
public PatientNode next;
public PatientNode(Patient data, PatientNode next) {
this.data = data;
this.next = next;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started