Pregunta

I'm having a problem with a variable not updating when its supposed to. I also am not sure where to update this variable as it does not fit into any of the if statement tests in my code. YOU MUST UNDERSTAND, that all I need fixed is where the variable peopleCompleted gets updated when the first person to enter is done. In my code you'll see where this first person is arriving, taking his place as the curServed and then being printed without being added to the queue holding everyone else who is in line. You'll notice that curServed gets changed in the serviceComplete() because it handles everyone else IN THE QUEUE. Below is my code and sameple output that is incorrect because of peopleCompleted not being updated for that first person. Basically, I really need help with knowing where to update his completion in the first place. This is a Simulation of a One Line One Queue. I am a beginner/student

CODE

public boolean arrival()
  {
     Customer myCust = new Customer(curTime);
     if(curServed == null) // If no one being served
     {
        curServed = myCust; // myCust is served
        peopleNoWait++;
        return true;
     }
     else if(!q.isFull())
     {
        q.add(myCust);
        peopleThatHadToWait++;
        return true;
     }
     return false;
  }

  public Customer serviceComplete()
  {  
     if(q.isEmpty())
     {
        curServed = null;
     }
     else
     {
        curServed = q.remove(); // Remove next from customer queue
        peopleCompleted++;
        sumOfWaitTime += getWaitTime();
     }
     return curServed;
  }

THESE BELOW HANDLE THE SIMULATION ABOVE

private void doArrival()
   {
      boolean check = sim.arrival(); // Do an arrival
      if(check == false)
         System.out.println("Customer arrived but left immediately"
               + " because the line was full (too long) at time " + 
               sim.getTime() + ".");
      else
         System.out.println("A customer entered system at time " + 
               sim.getTime() + "." + " Number waiting in queue is "
               + sim.getNumWaiting() + ".");
   }

   private void doServiceComplete()
   {
      if(sim.notBeingServed() == true)
      {
         System.out.println("No customer is being served at the present time"
               + " of " + sim.getTime() + ".");
      }
      else
      {
         System.out.print("Customer " + sim.getCurCust().toString() + 
               " finished at time " + sim.getTime() + ". Number waiting" + 
               " is ");
         System.out.println(sim.getNumWaiting() + ".");
      }
      sim.serviceComplete();

Methods that return the vals below

public int getNumWaiting()
  {
     int total = peopleThatHadToWait - peopleCompleted;
     return total;
  }

  public int getTotalServed()
  {
     return peopleCompleted;
  }

Sample output where error is:

  1. Customer C1/T2 finished at time 7. Number waiting is 2. // Should be 1

  2. Customer C2/T6 finished at time 7. Number waiting is 1. // Should be 0

  3. Customer C3/T7 finished at time 13. Number waiting is 5. // 4

  4. Customer C4/T7 finished at time 16. Number waiting is 4. // etc

  5. Customer C5/T8 finished at time 16. Number waiting is 3.

  6. Customer C6/T8 finished at time 17. Number waiting is 2.

  7. Customer C7/T9 finished at time 17. Number waiting is 1.

  8. The number of people served is 7. // Should be 8

  9. The number of people served is 7. // Should be 8

COMMENTS:

These are the lines causing issue. The number waiting and the number of people served are the ones that are incorrect. It is because of the the lack of the peopleCompleted being updated with that first person after he is completed, which according to your advice, is not done in serviceComplete()

¿Fue útil?

Solución

I suspect it's because of this code block here.

private void doServiceComplete()
{
  if(sim.notBeingServed() == true)
  {
     System.out.println("No customer is being served at the present time"
           + " of " + sim.getTime() + ".");
  }
  else
  {
     System.out.print("Customer " + sim.getCurCust().toString() + 
           " finished at time " + sim.getTime() + ". Number waiting" + 
           " is ");
     System.out.println(sim.getNumWaiting() + ".");
  }
  sim.serviceComplete();
}

When you get to sim.getNumWaiting() your people waiting will be one less than you're expecting. sim.serviceComplete() has peopleCompleted++ which is performed AFTER you print your results. You should move this to the beginning of the method, or somewhere else logical, before sim.getNumWaiting() is called.

Example:

private void doServiceComplete()
{
  sim.serviceComplete();

  if(sim.notBeingServed() == true)
  {
     System.out.println("No customer is being served at the present time"
           + " of " + sim.getTime() + ".");
  }
  else
  {
     System.out.print("Customer " + sim.getCurCust().toString() + 
           " finished at time " + sim.getTime() + ". Number waiting" + 
           " is ");
     System.out.println(sim.getNumWaiting() + ".");
  }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top