/* statistics.C
 *
 * Keep track of statistics.  Currently, we track:
 *
 *    average trip time
 */


#include <iostream.h>
#include "statistics.h"
#include "passenger.h"
#include "main.h"


/* When a passenger arrives at the passenger's destination, the
 * arrival is reported by calling this function.  Here, we just
 * accumulate the number of trips and the total trip time.
 */

void
Statistics::reportArrival( Passenger *p )

{
  numTrips++;
  totalTripTime += currentTime - p->startTime();
}


/* Print a report at fixed intervals.
 */


void
Statistics::periodicReport()

{
  if (textOutput)
    if (numTrips == 0) {
      cout << "\rTime: " << currentTime
	   << "  No trips reported";
    } else {
      cout << "\rTime: " << currentTime
	   << "  Completed trips: " << numTrips
	   << "  Average trip time: " << totalTripTime / (float) numTrips;
    }
#if 0
  for (int i=0; i<allTrains.size(); i++)
    cout << "  " << *allTrains[i] << " @ " << allTrains[i]->speed;
#endif
  cout.flush();
}


/* Print a final report.
 */


void
Statistics::finalReport()

{
  if (textOutput)
    cout << endl;

  if (testing) {

    /* DON'T MODIFY THIS PART!  The tutors rely on this output to
     * compare running times of programs.  If you modify this, your
     * program won't be considered in the competition. */

    if (numTrips == 0) {
      cout << "Time: " << currentTime
	   << "  No trips reported\n";
    } else {
      cout << "Time: " << currentTime
	   << "  Completed trips: " << numTrips
	   << "  Average trip time: " << totalTripTime / (float) numTrips << "\n";
    }

  } else {

    /* MODIFY THIS PART to report additional statistics as required by
     * the assignment. */

    if (numTrips == 0) {
      cout << "Time: " << currentTime
	   << "  No trips reported\n";
    } else {
      cout << "Time: " << currentTime
	   << "  Completed trips: " << numTrips
	   << "  Average trip time: " << totalTripTime / (float) numTrips << "\n";
    }
  }
}
