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 {
/** 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 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 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 boun.
Objective Min-heap queue with customized comparatorHospital emerg.pdf
1. 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;
}
2. 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 {
/** 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 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
3. */
// 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 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
4. 06,Tom,Gun-wounds
22,Kristen,Pregnancy
Solution
Code:
//Include libraries
import java.util.Date;
import java.util.Random;
import java.util.Comparator;
import java.util.PriorityQueue;
//Define class Patient
class Patient
{
//Define variables
protected String name;
protected int lcategory;
protected Date ltimeArrived;
//Define method lgetName()
public String lgetName()
{
return name;
}
//Define method lsetName()
public void lsetName(String lnameIn)
{
this.name = lnameIn;
}
//Define lgetCategory()
public int lgetCategory()
{
return lcategory;
}
//Define lsetCategory()
public void lsetCategory(int lcategoryIn)
{
5. this.lcategory = lcategoryIn;
}
//Define getTimeArrived()
public java.util.Date getTimeArrived()
{
return ltimeArrived;
}
// Define default constructor
public Patient()
{
this.name = "Default Name"; this.lcategory = 5;
// Place to queue end
this.ltimeArrived = new Date();
}
//Define overloaded constructor
public Patient(String lnameIn, int lcategoryIn)
{
this.name = lnameIn;
this.lcategory = lcategoryIn;
this.ltimeArrived = new Date();
}
}
//Define class PatientComparator
class PatientComparator implements Comparator
{
//Define compare()
public int compare(Patient lp1, Patient lp2)
{
//Compare lcategory
if (lp1.lgetCategory() < lp2.lgetCategory())
return -1;
if (lp1.lgetCategory() > lp2.lgetCategory())
return 1;
else
{
6. if (lp1.getTimeArrived().before(lp2.getTimeArrived()))
return -1;
if (lp1.getTimeArrived().after(lp2.getTimeArrived()))
return 1;
}
return 0;
}
}
//Define class lPatientQueue
class lPatientQueue
{
PriorityQueue lpq;
//Define constructor
public lPatientQueue()
{
this.lpq = new PriorityQueue(1, new PatientComparator());
}
//Define lregisterPatient()
public void lregisterPatient(Patient p)
{
this.lpq.add(p);
}
//Define lgetNextPatient()
public Patient lgetNextPatient()
{
return (Patient) this.lpq.poll();
}
}
//Define class lEmergency
public class lEmergency
{
//Declare variables
private static final int lWAIT_LIMIT = 3000;
lPatientQueue lpq = new lPatientQueue();
8. lpatientArrives(new Patient("QWE TYU", 2));
lpatientArrives(new Patient("Hen Kno", 4));
lpatientArrives(new Patient("Pat Hen", 2));
ldoctorVisits();
ldoctorVisits();
lpatientArrives(new Patient("Mar DrP", 1));
lpatientArrives(new Patient("Sam Adam", 3));
ldoctorVisits();
ldoctorVisits();
ldoctorVisits();
ldoctorVisits();
ldoctorVisits();
System.out.println("------------------------------------");
System.out.println("ER CLOSED");
System.out.println("------------------------------------");
} // end lsimulate method
public static void main(String[] args)
{
lEmergency ler = new lEmergency();
ler.lsimulate();
}
}
Sample Output:
E:Javajdk1.7.0_80bin>javac lEmergency.java
E:Javajdk1.7.0_80bin>java lEmergency
------------------------------------
ER OPEN
------------------------------------
ARRIVAL: ABC
time arrived: Wed Nov 23 10:25:59 PST 2016
lcategory: 3
------------------------------------
ARRIVAL: XYZ
time arrived: Wed Nov 23 10:26:00 PST 2016
lcategory: 1
------------------------------------
9. ARRIVAL: DFC YUH
time arrived: Wed Nov 23 10:26:02 PST 2016
lcategory: 2
------------------------------------
VISIT: XYZ
time arrived: Wed Nov 23 10:26:00 PST 2016
lcategory: 1
------------------------------------
ARRIVAL: QWE TYU
time arrived: Wed Nov 23 10:26:06 PST 2016
lcategory: 2
------------------------------------
ARRIVAL: Hen Kno
time arrived: Wed Nov 23 10:26:07 PST 2016
lcategory: 4
------------------------------------
ARRIVAL: Pat Hen
time arrived: Wed Nov 23 10:26:09 PST 2016
lcategory: 2
------------------------------------
VISIT: DFC YUH
time arrived: Wed Nov 23 10:26:02 PST 2016
lcategory: 2
------------------------------------
VISIT: QWE TYU
time arrived: Wed Nov 23 10:26:06 PST 2016
lcategory: 2
------------------------------------
ARRIVAL: Mar DrP
time arrived: Wed Nov 23 10:26:12 PST 2016
lcategory: 1
------------------------------------
ARRIVAL: Sam Adam
time arrived: Wed Nov 23 10:26:14 PST 2016
lcategory: 3
------------------------------------
10. VISIT: Mar DrP
time arrived: Wed Nov 23 10:26:12 PST 2016
lcategory: 1
------------------------------------
VISIT: Pat Hen
time arrived: Wed Nov 23 10:26:09 PST 2016
lcategory: 2
------------------------------------
VISIT: ABC
time arrived: Wed Nov 23 10:25:59 PST 2016
lcategory: 3
------------------------------------
VISIT: Sam Adam
time arrived: Wed Nov 23 10:26:14 PST 2016
lcategory: 3
------------------------------------
VISIT: Hen Kno
time arrived: Wed Nov 23 10:26:07 PST 2016
lcategory: 4
------------------------------------
------------------------------------
ER CLOSED
------------------------------------
E:Javajdk1.7.0_80bin>