Objective: Min-heap queue with customized comparator Hospital emergency room ass
ID: 3775594 • Letter: O
Question
Objective: Min-heap queue with customized comparator
Hospital emergency room assign patient priority base on symptom of the patient. Each patient receives an identity number which prioritizes the order of emergency for the patient. The lower the number is, the higher the emergency will be.
Use java.util.PriorityQueue to write a java program to create the emergency room registration database. The patient.txt is the input file for patient record. Download patient.txt, Patient.java to perform following specifications:
(a)Load all records from the input file.
(b)At the end, print the list in assigned priority order.
Given Code:
//*******************************************************************
// Patient.java
//*******************************************************************
public class Patient
{
private int id;
private String name;
private String emergencyCase;
//----------------------------------------------------------------
// Creates a customer with the specified id number.
//----------------------------------------------------------------
public Patient (int number, String custName,String er )
{
id = number;
name = custName;
emergencyCase = er;
}
//----------------------------------------------------------------
// Returns a string description of this customer.
//----------------------------------------------------------------
public String toString()
{
return "Patient priority id: " + id+" Patient name: "+name+" Symptom: "+emergencyCase;
}
public String getName()
{
return name;
}
public int getId()
{
return id;
}
public String getCase()
{
return emergencyCase;
}
}
/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/
import java.util.*;
import java.math.*;
/** A bunch of utility functions. */
class DSutil<E> {
/** Swap two Objects in an array
@param A The array
@param p1 Index of one Object in A
@param p2 Index of another Object A
*/
public static <E> void swap(E[] A, int p1, int p2) {
E temp = A[p1];
A[p1] = A[p2];
A[p2] = temp;
}
/** Randomly permute the Objects in an array.
@param A The array
*/
// int version
// Randomly permute the values of array "A"
static void permute(int[] A) {
for (int i = A.length; i > 0; i--) // for each i
swap(A, i-1, DSutil.random(i)); // swap A[i-1] with
} // a random element
public static void swap(int[] A, int p1, int p2) {
int temp = A[p1];
A[p1] = A[p2];
A[p2] = temp;
}
/** Randomly permute the values in array A */
static <E> void permute(E[] A) {
for (int i = A.length; i > 0; i--) // for each i
swap(A, i-1, DSutil.random(i)); // swap A[i-1] with
} // a random element
/** Initialize the random variable */
static private Random value = new Random(); // Hold the Random class object
/** Create a random number function from the standard Java Random
class. Turn it into a uniformly distributed value within the
range 0 to n-1 by taking the value mod n.
@param n The upper bound for the range.
@return A value in the range 0 to n-1.
*/
static int random(int n) {
return Math.abs(value.nextInt()) % n;
}
}
Text FIle:
10,Sam,Bleeding
02,Carla,Stroke
92,Woody,Flu
11,Diane,High-temperature
32,Norm,Stomach
55,Cliff,Broken-bone
06,Tom,Gun-wounds
22,Kristen,Pregnancy
Explanation / Answer
Patient.java program
package blah;
import java.util.Date;
public class Patient
{
protected String name;
protected int category;
protected Date timeArrived;
// accessors and mutators
public String getName()
{
return name;
}
public void setName(String nameIn)
{
this.name = nameIn;
}
public int getCategory()
{
return category;
}
public void setCategory(int categoryIn)
{
this.category = categoryIn;
}
public java.util.Date getTimeArrived()
{
return timeArrived;
}
// default constructor
public Patient()
{
this.name = "Default Name"; this.category = 5;
// unclassified Patients go to the end of the queue
this.timeArrived = new Date();
}
// overloaded constructor
public Patient(String nameIn, int categoryIn)
{
this.name = nameIn;
this.category = categoryIn;
this.timeArrived = new Date();
}
}
// end Patient class
PatientComparator.java
package blah;
import java.util.Comparator;
public class PatientComparator implements Comparator<Patient>
{
public int compare(Patient p1, Patient p2)
{
if (p1.getCategory() < p2.getCategory())
return -1;
if (p1.getCategory() > p2.getCategory())
return 1;
else { if (p1.getTimeArrived().before(p2.getTimeArrived()))
return -1;
if (p1.getTimeArrived().after(p2.getTimeArrived()))
return 1;
}
return 0;
}
} // end PatientComparator class
Patientqueue.java
import java.util.Comparator;
import java.util.PriorityQueue;
public class PatientQueue
{
PriorityQueue pq; // default constructor
public PatientQueue()
{
this.pq = new PriorityQueue<Patient>(1, new PatientComparator());
}
public void registerPatient(Patient p)
{
this.pq.add(p);
} // end registerPatient method
public Patient getNextPatient()
{
return (Patient) this.pq.poll();
} // end
getNextPatient method
} // end PatientQueue class
EmergencyRoomSimulator.java
import.java.util.Random;
public class EmergencyRoomSimulator
{
private static final int WAIT_LIMIT = 3000; // 1000 = 1 second
PatientQueue pq = new PatientQueue();
private void t()
{
try
{
Thread.sleep(new Random().nextInt(WAIT_LIMIT));
} catch (InterruptedException e) {
}
} // end t method
private void patientArrives(Patient p)
{
pq.registerPatient(p);
System.out.println(" ARRIVAL: " + p.getName());
System.out.println(" time arrived: " + p.getTimeArrived());
System.out.println(" category: " + p.getCategory());
System.out.println("------------------------------------"); t();
} // end patientArrives method
private void doctorVisits()
{
Patient p = pq.getNextPatient();
System.out.println(" VISIT: " + p.getName());
System.out.println(" time arrived: " + p.getTimeArrived());
System.out.println(" category: " + p.getCategory());
System.out.println("------------------------------------");
t();
} // end doctorVisits method
private void simulate()
{
System.out.println("------------------------------------");
System.out.println("ER OPEN");
System.out.println("------------------------------------");
patientArrives(new Patient("ABC", 3));
patientArrives(new Patient("XYZ", 1));
patientArrives(new Patient("DFC YUH", 2));
doctorVisits();
patientArrives(new Patient("QWE TYU", 2));
patientArrives(new Patient("Hen Kno", 4));
patientArrives(new Patient("Pat Hen", 2));
doctorVisits();
doctorVisits();
patientArrives(new Patient("Mar DrP", 1));
patientArrives(new Patient("Sam Adam", 3));
doctorVisits();
doctorVisits();
doctorVisits();
doctorVisits();
doctorVisits();
System.out.println("------------------------------------");
System.out.println("ER CLOSED");
System.out.println("------------------------------------");
} // end simulate method
public static void main(String[] args)
{
EmergencyRoomSimulator er = new EmergencyRoomSimulator();
er.simulate();
} // end main
} // end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.