0
 public class Queue {

  public static void main(String[] args) {

     Queue<Integer> myQueue = new LinkedList<Integer>();
     Random generator  = new Random();

     int totalTime = 60;
     int lineSize=0;
     int serviceTime;
     int currentPatronTimeLeft=50;
     int timeKeeper;
     int newPatron; 
     int totalCheckedOut=0;



     do
     {
        //for new customer 25%of the time
        serviceTime = generator.nextInt(5) + 1;
        newPatron = generator.nextInt(4) + 1;
     //nested if else to control the adding and removing of items based on service time and the 25% stat 
        if(newPatron == 1)
        {
           myQueue.add( new Integer(serviceTime));
           System.out.println("a new customer has been added to line");
           lineSize++;
           System.out.println("The Queue size is now: "+ lineSize);
           currentPatronTimeLeft = myQueue.peek();

        }
        else if(currentPatronTimeLeft == 0 && lineSize != 0)
        {
           myQueue.poll();
           lineSize--;   
           System.out.println("A customer has been removed from the queue");
           totalCheckedOut++;

           System.out.println("The Queue size is now: "+ lineSize);
        }
        else if (lineSize != 0)
        {
           currentPatronTimeLeft =myQueue.peek();
        }


        currentPatronTimeLeft--;
        totalTime--;
        System.out.println();
        System.out.println("---------------------------------------------------");

     }while(totalTime != 0);





     System.out.println();

  }

}

I am having trouble getting my program to remove people from the queue after they have should have completed their serviceTime. any help would be greatly appreciated. the program produces an output somewhat like this

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 1

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 2

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 3

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 4

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 5

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 6

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 7

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 8

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 9

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 10

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 11

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 12

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 13

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 14

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
4
  • 1
    do you have a debugger? Commented Feb 21, 2013 at 8:48
  • @RADAI i do not have one Commented Feb 21, 2013 at 8:52
  • We have to scroll a lot because your code has many empty lines. Please consider formatting it nicely before posting it here ;) Commented Feb 21, 2013 at 9:17
  • Try reducing your code to the absolute minimum that shows the problem - see SSCCE Commented Feb 21, 2013 at 9:20

3 Answers 3

2

Remove the following condition from your code:

else if(lineSize != 0)
 {
           currentPatronTimeLeft =myQueue.peek();
 }

It should work fine

Sign up to request clarification or add additional context in comments.

Comments

0

Your problem lies in this part of the code:

else if (lineSize != 0)
{
    currentPatronTimeLeft =myQueue.peek();
}
currentPatronTimeLeft--;

If no one gets added to the queue, and the current one is not finished, you update currentPatronTimeLeft to the start value of the current client. After that you decrement it. If in the next iteration, no new client is added to the queue, then the current one won't be finished, because his time left is his time from the beginning - 1. Then you update currentPatronTimeLeft to it's start value again before decrementing it. This way it will never become 0.

Solution:

else if(currentPatronTimeLeft == 0 && lineSize != 0)
{
    myQueue.poll();
    lineSize--;
    // insert following lines:
    if (lineSize > 0) {
        currentPatronTimeLeft =myQueue.peek() + 1; // +1 <--> following decrement
    }
    // end of my insert
    System.out.println("A customer has been removed from the queue");
    totalCheckedOut++;
    System.out.println("The Queue size is now: "+ lineSize);
}

and you have to get rid of the whole else if (lineSize != 0) block.

4 Comments

okay i changed: else if(currentPatronTimeLeft == 0 || lineSize != 0) and now i have items being removed prematurely
@user2094629: using || lineSize != 0 will always remove, once it's in. I posted a solution above.
okay it seems to be running much better and seems to be in a pattern that seems correct. the output is a new customer has been added to line. Their service time is :1 The Queue size is now: 1 1 --------------------------------------------------- a new customer has been added to line. Their service time is :3 The Queue size is now: 2 1 --------------------------------------------------- A customer has been removed from the queue The Queue size is now: 1 --------------------------------------------------- the dashes represent 1 iteration but i feel like that output is one iteration off
Yes, because now it's right. Please don't forget to accept the best answer, see how it works here: How does accepting an answer work? and upvote helpful answers.
0

You have a problem with the if-else you did, currentPatronTimeLeft will always be the first element, thus never reaching zero. a quick fix would be using another variable for one of these purposes.

2 Comments

how would you suggest using another variable to replace currentPatronTimeLeft? or are you suggesting i add another variable altogether
since i cannot yet comment on others answers, @user2094270 you should as well add in the first else if currentPatronTimeLeft = myQueue.poll();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.