// pump.cc: the Pump class.

#include <iostream.h>
#include "main.h"
#include "car.h"
#include "event.h"
#include "evlist.h"
#include "random.h"
#include "stats.h"
#include "pump.h"
#include "depart.h"

double Pump::serviceTime () {
   if (carInService == 0) {
      cout << "Error! no car in service when expected\n";
      return -1.0;
   }

   // Make a random sample from a normal distribution by repeatedly
   // sampling the uniform distribution. (It can be shown that the sum
   // of 12 random numbers from the uniform distribution on (0,1) has
   // a near Normal distribution with mean 6 and standard deviation 1.)

   double howLong = -6;
   for (int i = 1; i <= 12; i++)
       howLong += sim->serviceStream->nextDouble();
   
   return sim->serviceTimeBase
         + sim->serviceTimePerLitre * carInService->getLitresNeeded()
         + sim->serviceTimeSpread * howLong;
}   

void Pump::startService (Car *car) {
   // precondition: sim->pumpStand->aPumpIsAvailable().
   
   // Match the auto to an available pump.
   carInService = car;
   const double pumpTime = serviceTime();
   
   // Collect statistics.
   sim->stats->accumWaitingTime (sim->simulationTime
         - carInService->getArrivalTime());
   sim->stats->accumServiceTime (pumpTime);
   
   // Schedule departure of car from this pump.
   Departure *dep = new Departure (sim->simulationTime + pumpTime);
   dep->setPump (this);
   sim->eventList->insert (dep);
}

