Search code examples
javapriority-queue

Priority Que returning null in java and how to use comparable for it


I am using priorityQue for the first time and am confused why it is returning null when I try to print it. This is an assignment where I'm making a simple menu to store display and remove (just the head value). Also if anyone can explain how the comparable class sorts the priorityQue because I'm pretty sure I'm doing it wrong. Any help is appreciated

Main Class

import java.util.PriorityQueue;
import java.util.Scanner;

public class  PatientManager{
    Patient p =new Patient(0, null, 0);

    PriorityQueue<Patient> waitingList= new PriorityQueue<Patient>();
    int order=0; 

    public static void main(String[] args){
        PatientManager pm=new PatientManager();
        pm.start();
    }
    void start(){
        boolean go=true;
        try{
            while(go){

                System.out.println("------------------");
                System.out.println("(1) New Patient.");
                System.out.println("(2) Next Patient.");
                System.out.println("(3) Waiting List.");
                System.out.println("(4) Exit.");
                System.out.println("------------------");
                System.out.print("Chose an item from the menu: ");
                Scanner input=new Scanner(System.in);
                int ask= input.nextInt();
                switch (ask) {
                case 1:  optionOne();
                         break;
                case 2:  optionTwo();
                         break;
                case 3:  optionThree();
                         break;
                case 4:  go=false;
                         break;
                default: System.out.print("(x) Wrong choice: ");
                         break;
                }
            }
        }catch(Exception e){
            System.out.print("(x) Wrong choice");
            e.printStackTrace();
            start();
        }
    }
    void optionOne(){
        System.out.print("Enter Patients name ");
        Scanner reader=new Scanner(System.in);
        String name= reader.next();
        p.setName(name);
        System.out.print("Enter Emergency[1 (low) to 5 (life-and-death)] ");
        boolean a=true;
        while(a){
            try{
                int b=reader.nextInt();
                if(1<=b && b<=5){
                    p.setEmergency(b);
                    a=false;
                }else System.out.print("(x) Wrong choice: ");
            }catch(Exception e){
                System.out.print("(x) Wrong choice");
                a=false;
            }
        }
        System.out.println(waitingList.add(new Patient(order,p.getName(),p.getEmergency())));
        System.out.println(waitingList.poll());
        order++;
    }
    void optionTwo(){
        System.out.println(waitingList.peek());
        waitingList.remove();
    }
    void optionThree(){
        System.out.println(waitingList.peek());
    }
}

getter setter class

import java.util.Comparator;

public class Patient implements Comparable<Patient>{
    //attributes
    private String name;
    private int order;      //order of arrival
    private int emergency; //1 is normal, 5 is life-and-death situation

    //constructor
    public Patient(int order, String name, int priority) {
        this.order = order;
        this.name = name;
        this.emergency = priority;
    }

    //getters and setters
    public int getOrder() {
        return order;
    }
    public void setOrder(int order) {
        this.order = order;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getEmergency() {
        return emergency;
    }

    public void setEmergency(int emergency) {
        this.emergency = emergency;
    }

    public String toString() {
        return name;
    }
    public int compareTo(Patient a){
        if(emergency!=a.emergency && emergency>a.emergency) return emergency;
        else if(emergency!=a.emergency && emergency<a.emergency) return a.emergency;
        else if(emergency==a.emergency){
            if(order<a.order)return order;
            else return a.order;
        }
        return emergency=0;
    }
}

Solution

  • for Comparable you need to do like:

    if same then 0 => same emergency

    if current > second = 1 current patient has higher priority/emergency

    if current < second = -1 current patient has lower priority/emergency

    in code:

     public int compareTo(Patient a){
        if(emergency==a.emergency)
            return 0;       
        else if(emergency>a.emergency)
            return 1;
        else
            return -1;          
    }
    

    also do you really want to do below in optionOne(). The reason you get null is because as soon as you add it to the queue you remove it in the next line.

      System.out.println(waitingList.add(new Patient(order,p.getName(),p.getEmergency())));
      System.out.println(waitingList.poll()); //poll() Retrieves and removes the head of this queue, or returns null if this queue is empty.
    

    Read again what poll is doing cause i think you misunderstood.