/**************************************************************
 * This class implements a mutable class for keeping car race
 * statistics on miles led, percent of miles led, and average
 * finish.  Each object represents one driver and his/her 
 * stats.  
 *************************************************************/
public class CarStat
{
   private static double trackSize = 0;  // Size of the track (one lap) in miles
   private static int totalRaceCnt = 0;  // Total number of races 
   private static double totalMiles = 0; // Total miles for all races
   
   private String driver;      // Name of driver
   private int carNumber;      // Car number for driver
   private int driverRaceCnt;  // Number of races for this driver
   private double milesLed;    // Number of miles led for all races
   private int finishPosTotal; // Sum of finish positions
   //***************************************************************
   // Example for finishPosTotal: for a driver with finishes of
   // 2, 1, 4, 3, 1, and 1, finishPosTotal is the sum of those or
   // 12 which would generate an average finish over the driver's
   //  6 races of 12/6 == 2.0.
   //***************************************************************

   private final boolean DEBUG = false;

   /**************************************************************
    * This constructor creates a CarStat object with
    * a driver name and car number.  All other 
    * driver statistics are set to zero.
    * @param name Driver name
    * @param number Car number
    *************************************************************/
    // Note: This method does not affect any class variables.
    //***********************************************************
   public CarStat(String name, int number)
   {
      Util139.printDebug(DEBUG, "");
      this.driver = name;
      this.carNumber = number;
      Util139.printDebug(DEBUG, "this=%s\n", this.toString());
   }

   /**************************************************************
    * This mutator method sets the track size and distance data for a
    * race and must be called before calling 
    * updateRaceStats to add statistics for that race to any object.
    * Lap length will give the length of a single lap in miles (track size);
    * miles indicates the total length of the race.  This method
    * must be called exactly once for each race run and increases the
    * count of the number of total races.
    * @param lapLength Size of the track in miles
    * @param miles     Length of race in miles 
    *************************************************************/
   // Think about all of the class variables and make sure you are 
   // updating them all appropriately.
   //************************************************************
   public static void setupRace(double lapLength, double miles)
   {
      CarStat.trackSize = lapLength;
      CarStat.totalMiles = CarStat.totalMiles + miles;
      CarStat.totalRaceCnt++;
   }

   /**************************************************************
    * This mutator method updates the individual driver
    * statistics on laps led, average finish position. 
    * @param lapsLed  Number of laps that this driver led the race
    * @param position Position number at end of the race 
    *************************************************************/
    // Think about how to use the formal parameters and the class
    // variables so you can adjust the instance variables appropriately.
    // Consider all of the instance variables and which might be affected
    // by the driver running a race.
    //************************************************************
   public void updateRaceStats(int lapsLed, int position)
   {
      Util139.printDebug(DEBUG, "this=%s\n", this.toString());

      this.milesLed = lapsLed * this.trackSize;
      this.finishPosTotal = this.finishPosTotal + position;
      this.driverRaceCnt++;
      
      Util139.printDebug(DEBUG, "this=%s\n", this.toString());
   }

   /**************************************************************
    * This accessor method calculates the average position of finish
    * for this driver over the races in which the driver competed.
    * @return average finish position of this driver
    *************************************************************/
   public double avgFinish()
   {
      double finishPos;
      finishPos = 0;
      if(this.driverRaceCnt > 0)
         finishPos = this.finishPosTotal / this.driverRaceCnt;
         
	   return finishPos;
   }

   /**************************************************************
    * This accessor method returns the percent of miles led by
    * this driver as a percentage of the total miles of all races.
    * @return The percent of this driver's led miles
    *************************************************************/
   public double percentMilesLed()
   {
      double perLed;
      perLed = 0;
      
      if (this.totalMiles > 0)
         perLed = (this.milesLed / totalMiles) * 100;
         
      return perLed;
   }

   /**************************************************************
    * This accessor method returns the total miles led by this driver.
    * @return The miles led by this driver.
    *************************************************************/
   public double getMilesLed()
   {
      return this.milesLed;
   }
   /**************************************************************
    * This accessor method returns the name of this driver.
    * @return This driver's name.
    *************************************************************/
   public String getDriver()
   {
      return driver;
   }
   /*********************************************************
    * This method returns a string representation of this object.
    * The format of the string is:
    * "Driver:%s, Car:%d, Races:%d, AvgFin:%f, MilesLed:%f" 
    * @return A string representation of the object
    *********************************************************/
   public String toString()
   {
      String builder;
      builder = String.format("Driver:%s, Car:%d, Races:%d, AvgFin:%f, MilesLed:%f",
       this.driver, this.carNumber, this.driverRaceCnt, this.avgFinish(), this.milesLed);
      return builder;
   }
}
