3 Replies - 215 Views - Last Post: 18 April 2013 - 04:13 AM Rate Topic: -----

#1 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

How do you enqueue an Element to a queue based on an attribute?

Posted 17 April 2013 - 11:41 PM

Sorry about the wording, but I did not know how else to say it. I have a queue called ERQueueue that implements the basic queue interface. I wrote my own code already that works on a basic queue, but this one is different. All of the methods need to stay the same except for the enqueue method. Instead of just inserting an element at the end I need it to look at one of the variables to determine where to enqueue it. I created a Patient class that has an attribute called emeregencyLevel, with an accessor called getEmeregencyLevel(), and need that to determine how the Patients are enqueued. The lower the number, the higher up on the queue they should go. For example if I had to add three patients in this order Joe level 2, Jim level 5, and Bob level 1, the queue should put them in the order: Bob, Joe, Jim based on that level.

This is the Constructor, and the enqueue method that works on a basic queue.

public class ERQueue<E> implements Queue<E>
{
    private final int CAPACITY = 50;
    private E[] eAry;
    private int numElements;
    private int front;
    private int rear;
   
   public ERQueue(E Element)
   {
       this.eAry = (E []) new Object[CAPACITY];
       this.numElements = 0;
       this.front = 0;
       this.rear = 0;
   }

 public void enqueue (E element)
  {
      this.eAry[this.rear] = element;
      this.numElements++;
      this.rear = (this.rear + 1) % CAPACITY;
  }




Is This A Good Question/Topic? 0
  • +

Replies To: How do you enqueue an Element to a queue based on an attribute?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: How do you enqueue an Element to a queue based on an attribute?

Posted 18 April 2013 - 01:13 AM

I would call the attribute you're describing the object's priority. So you need to add an insert method to your class that takes into account the Patient object's emergency level or priority.

You could accomplish the same thing two ways (and maybe more):

1. Insert the Patients into the queue according to their priority, or
2. Remove the Patients from the queue according to their priority.

For the first approach (the one you apparently favor), you could define an insertion rule, something like: "Patients with a priority other than zero are inserted ahead of the first Patient with a larger value for an emergency level. Patients with zero priority are added to the end of the queue." Once you're happy with the insertion rule, code it. Since what you're describing is not typical queue behavior, you may want to develop some helper methods to simplify insertion.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How do you enqueue an Element to a queue based on an attribute?

Posted 18 April 2013 - 03:56 AM

Another solutions is to have as many queue as you have priority level
A central class can hold those N queues. When you insert it will insert on the good queue. When you remove, it will remove from the first non empty queue.

A timer can even, after certain amount of time promote patient from one queue to another.
So you can have a field queuedTime in the Patient class that register the time a patient is queued. Every certain time a method can scan the queues, if a patient has been in a queue more than a certain time, hist priority is increment by one, it is requeue on the next queue and its queuedTime is reset to now.

Happy codind
Was This Post Helpful? 1
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: How do you enqueue an Element to a queue based on an attribute?

Posted 18 April 2013 - 04:13 AM

Quote

Another solutions is to have as many queue as you have priority level

I thought of that basic idea, but I didn't continue it in my head to include a timer that promoted patients among queues. I didn't mention it because it seemed too complicated, and the number of priorities wasn't specified. For a reasonable number of priorities (whatever that is), I think it could be a great idea, or at least one that would be interesting to code. Thanks for adding it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1