skynyrd
skynyrd

Reputation: 982

Addition operation for PriorityQueue class in Java

I'm trying to implement a PriorityQueue in Java. I have PrinterQueue class that includes the PriorityQueue and TestPrinterQueue class for testing that. PrintJob is the type that is in this queue. The constructor is private, so I used reflection for implementing the instance of the object.

The problem occurs in runtime that I cannot add more than one objects to the queue. The size of the queue is always same. There is no compilation error. All codes are below. Thank you...

PrinterQueue class:

import java.util.Comparator;
import java.util.PriorityQueue;

public class PrinterQueue{
   private static PrinterQueue queue;
   PriorityQueue<PrintJob> pqExample;
   Comparator<PrintJob> Comparator;
   int iQueueSize = 50;

   private PrinterQueue(){
      Comparator = new Priority();
      pqExample = new PriorityQueue<PrintJob>(iQueueSize,Comparator);
  }
   public static PrinterQueue getQueue(){
      if (queue==null) queue = new PrinterQueue();
     return queue;} 

   public void addJob (PrintJob job) {
       queue = new PrinterQueue();
       queue.pqExample.add(job);
     // Use the job’s getPriority method to check its priority. PRIORITY CLASSINDA KULLANILDI.
     System.out.println("Job " + job.getName() + " is added to the printer queue" + " Size: " + queue.pqExample.size());
     Print();
  }
  public boolean isEmpty(){
      if(queue.pqExample.isEmpty())
          return true;
      else
          return false;
  }
  public PrintJob removeJob(){
      //TODO –remove the highest priority job from the queue
      PrintJob job = queue.pqExample.remove();
      System.out.println("Job "+ job.getName()+ "has been printed");
      return job;

  }
  public boolean isFull(){
      if (queue.pqExample.size() == iQueueSize)
          return true;
      else
          return false;
  }
  private void Print() {

      PriorityQueue<PrintJob> pqTemp;
      pqTemp = queue.pqExample;

       while (pqTemp.size() != 0)
       {
           System.out.println(queue.pqExample.element());
           System.out.println("//");
           pqTemp.remove();
       }
  }
}

TestPrinterQueue class:

import java.lang.reflect.Constructor;
import java.util.Scanner;

public class TestPrinterQueue {

    public static void main (String[] args) throws Exception {

        Constructor<PrinterQueue> constructor = PrinterQueue.class.getDeclaredConstructor(new Class[0]);
        constructor.setAccessible(true);

        PrinterQueue pqTest = constructor.newInstance(new Object[0]);

        @SuppressWarnings("resource")
        Scanner scUserInput = new Scanner(System.in);

        while (true)
        {
             PrintJob pjWillbeAdded = new PrintJob();

             System.out.println("Enter the priority:");
             pjWillbeAdded.setPriority(scUserInput.nextInt());
             System.out.println("Enter the JobName:");
             pjWillbeAdded.setName(scUserInput.next());
             System.out.println("Enter the Number of Pages in PrintJob:");
             pjWillbeAdded.setiNoPJ(scUserInput.nextInt());

             pqTest.addJob(pjWillbeAdded);

             System.out.println("**** TEST SIZE : " + pqTest.pqExample.size() + "*****");
        }
    }
}

I hope this information is enough to explain my problem.

Upvotes: 0

Views: 644

Answers (1)

awolfe91
awolfe91

Reputation: 1647

In your addJob() method, you're assigning queue to a new PrinterQueue object each time. Changing this to "queue = getQueue()" should fix that.

Upvotes: 1

Related Questions