Objective Minheap queue with customized comparator Hospital
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
Solution
import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Comparator;
 import java.util.PriorityQueue;
 import java.util.Scanner;
 import java.util.StringTokenizer;
public class EmergencyRegistrationDatabase implements Comparator<Patient> {
private static Scanner sc;
   @Override
    public int compare(Patient o1, Patient o2) {
        // TODO Auto-generated method stub
        if (o1.getEmergencyCase().equalsIgnoreCase(\"Stroke\")) {
            switch (o2.getEmergencyCase()) {
            case \"Pregnancy\":
                return 0;
            case \"Gun-wound\":
                return 0;
            case \"Flu\":
                return 1;
            case \"Broken-bone\":
                return 1;
            default:
                return 0;
           }
        } else if (o1.getEmergencyCase().equalsIgnoreCase(\"Pregnancy\")) {
           switch (o2.getEmergencyCase()) {
            case \"Stroke\":
                return 0;
            case \"Gun-wound\":
                return 0;
           case \"Flu\":
                return 1;
            case \"Broken-bone\":
                return 1;
            default:
                return 0;
           }
        }
        return 0;
    }
   public static void main(String[] args) {
        // Patients
        PriorityQueue<Patient> queue = new PriorityQueue<Patient>(100, new EmergencyRegistrationDatabase());
        try {
            sc = new Scanner(new File(\"patients.txt\"));
            while (sc.hasNextLine()) {
                String str = sc.nextLine();
                Patient pat = new Patient(str);
                queue.add(pat);
                // System.out.println(sc.nextLine());
           }
            System.out.println(\"q is \" + queue.toArray());
       } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
}
class Patient {
    public int getId() {
        return id;
    }
   public void setId(int id) {
        this.id = id;
    }
   public String getName() {
        return name;
    }
   public void setName(String name) {
        this.name = name;
    }
   public String getEmergencyCase() {
        return emergencyCase;
    }
   public void setEmergencyCase(String emergencyCase) {
        this.emergencyCase = emergencyCase;
    }
   private int id;
    private String name;
    private String emergencyCase;
   public Patient(int id, String name, String emergencyCase) {
        this.id = id;
        this.name = name;
        this.emergencyCase = emergencyCase;
    }
   public Patient(String csvStr) {
        StringTokenizer str = new StringTokenizer(csvStr, \",\");
        this.id = Integer.parseInt(str.nextToken());
        this.name = str.nextToken();
        this.emergencyCase = str.nextToken();
    }
 }
//////////////////////////////////////
patients.txt
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





