Tuesday, 8 February 2011

APQ

My APQ Coding:
import java.util.*;

public class APQ implements Queue
{

   private ArrayList <ArrayList<Message> > messageQueues;
   private int total;//total number of message in the system
  
   public APQ ()//constructor
   {
         messageQueues = new ArrayList <ArrayList<Message>>();
         for(int i=0;i<10;i++)
         {
               messageQueues.add(new ArrayList <Message>());
            }
          total = 0; 
    }
  
   public  boolean isEmpty()//returns true if there are no messages
     {
         for(int i = 9; i>= 0; i--)
         {
             if(messageQueues.get(i) != null)
                 return false;
            }
         return true;
     }
     
       
       public  int size()//returns the total number of messages
        {
           int count = 0;
           for(int i = 0; i<10; i++)
           {
               count += messageQueues.get(i).size();
        }
        return count;
    }

        public void add (Message msg)
        {
            messageQueues.get(msg.getPriority()).add(msg);
        }

     public Message remove()
        {
            int i = 9;
            while(messageQueues.get(i).size() == 0 && i>= 0)
            {
                i--;
            }
            total--;
            return messageQueues.get(i).remove(0);
        }
    }

Credit to Amit Maor for help with the last method.

No comments:

Post a Comment