// custqueue.C: the CustQueue class. (modified from carqueue.cc)

#include <cstdio>
#include <iostream.h>
#include "supermarket.h"
#include "custqueue.h"

double CustQueue::getEmptyTime () {
   if (queueSize > 0)
      return totalEmptyQueueTime;
   else
      return totalEmptyQueueTime + sim->getTime();
}

void CustQueue::insert (Cust *newestCust) {
   QueueItem *item = new QueueItem();
   item->data = newestCust;
   item->next = 0;
   
   if (lastWaitingCust == 0) {
      // the queue is empty
      firstWaitingCust = item;
      totalEmptyQueueTime += sim->getTime();
   }
   else {
      // the queue already had at least one customer in it
      lastWaitingCust->next = item;   
   }
   
   lastWaitingCust = item;
   queueSize += 1;   
}

Cust *CustQueue::takeFirstCust () {
   // precondition: queueSize > 0 && firstWaitingCust != 0
   if (queueSize <= 0 || firstWaitingCust == 0) {
      cerr << "Error! cust queue unexpectedly empty\n";
      return 0;
   }
   
   Cust *custToReturn = firstWaitingCust->data;
   queueSize--;
   QueueItem *restOfList = firstWaitingCust->next;
   delete firstWaitingCust;
   firstWaitingCust = restOfList;
   
   if (firstWaitingCust == 0) {
      // empty queue: update the end of the queue, and start
      // counting empty queue time
      lastWaitingCust = 0;
      totalEmptyQueueTime -= sim->getTime();
   }
   
   return custToReturn;
}
