/**************************************************************
 * This class implements a mutable class for keeping car race
 * statistics.  Each object represents one driver and his/her 
 * stats.  
 *************************************************************/
public class CarStat
{
	private static double trackSize = 0;  // Size of the track (one lap)
	private static int raceCount = 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 races;			 // Number of races for this driver
	private double milesLed;	 // Number of miles led 
	private int finishPosTot;   // Sum of finish positions

	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.  This method does not 
	 * affect the race statistics
	 * @param name Driver name
	 * @param number Car number
	 *************************************************************/
	public CarStat(String name, int number)
	{
		this.driver = name;
		this.carNumber = number;
		this.races = 0;
		this.milesLed = 0;
		this.finishPosTot = 0;
	}

	/**************************************************************
	 * This mutator method sets the overall race statistics 
	 * and should be used before entering any driver statistics.
	 * Lap length will give the length for this race;
	 * miles will be added to total miles.  raceCount should be
	 * incremented;
	 * @param lapLength Size of the track
	 * @param miles     Length of race in miles 
	 *************************************************************/
	public static void setupRace(double lapLength, double miles)
	{
		CarStat.trackSize = lapLength;
		CarStat.totalMiles = CarStat.totalMiles + miles;
		CarStat.raceCount++;
	}

	/**************************************************************
	 * This mutator method updates the individual driver
	 * statistics.  Note, the miles led must be calculated based on
	 * the lapsLed and the size of the track.  The position will
	 * be added to the position total.  The raceCount for this driver
	 * must be incremented.
	 * @param lapsLed  Number of laps that this driver led the race
	 * @param position Position number at end of the race 
	 *************************************************************/
	public void updateRaceStats(int lapsLed, int position)
	{
		this.races++;
		this.milesLed = this.milesLed + (lapsLed * CarStat.trackSize);
		this.finishPosTot = this.finishPosTot + position;
	}

	/**************************************************************
	 * This accessor method calculates the average position of finish
	 * for this driver based on the total positions and the number
	 * of races for this driver.
	 * @return average finish position of this driver
	 *************************************************************/
	public double avgFinish()
	{
		if (this.races == 0)
			return 0;
		return (double)this.finishPosTot / this.races;
		
	}

	/**************************************************************
	 * This accessor method returns the percent of miles led for 
	 * this driver as a percent of the total miles of all races.
	 * Note: 80% would be returned as 80.0
	 * @return The percent of this driver's led miles
	 *************************************************************/
	public double percentMilesLed()
	{
		double percent;	
		if (CarStat.totalMiles == 0) return 0;
		percent = (this.milesLed / CarStat.totalMiles) * 100;
		return percent;
	}

	/**************************************************************
	 * This accessor method returns the total miles led for 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 this.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 str;
		str = String.format("Driver:%s, Car:%d, Races:%d, AvgFin:%f, MilesLed:%f", this.driver, 
			this.carNumber, this.races,  this.avgFinish(),this.milesLed);
		return str;
	}
}
