/*
 * Athlete: This class stores information for one athlete, who
 *          competes in one category in the triathalon.
 */

class Athlete {

	private String athleteName;		// name of the athlete (unique)
	private int athleteNumber;		// assigned athlete number
	private int[] times;			// stores times for each event

	private final int NUM_EVENTS = 3;	// number of events

	// Elements in the array that still have a zero value, indicate
	// that this event time has not yet been entered.
	public final static int NOT_ENTERED = 0;

	/*
	 * Athlete constructor: Create a new athlete, that is named
	 * 'name' and has athlete number 'athleteNumber'.
	 */
	public Athlete (String name, int athleteNumber) {
		this.athleteName = name;
		this.athleteNumber = athleteNumber;
		this.times = new int[NUM_EVENTS];
	}

	/*
	 * getName: return the athlete's name.
	 */
	public String getName () {
		return athleteName;
	}

	/*
	 * getNumber: return the athlete's athlete number.
	 */
	public int getNumber () {
		return athleteNumber;
	}

	/*
	 * addEventTime: record 'nextTime', which is the time for this
	 *    athlete's next event that does not yet have a time recorded.
	 *    This method returns false if all events have already been
	 *    recorded for this athlete.  It returns true otherwise.
	 *
	 *    Note: If the user enters four event times for an athlete, an
	 *    error would be reported, since there are only three events.
	 *    If less than three event times are recorded, we assume the
	 *    athlete did not finish the remaining events.
	 */
	public boolean addEventTime (int nextTime) {
		// FILL IN THE BODY.
		if (times[times.length-1] != NOT_ENTERED) {
			System.out.println ("All event times have already been " +
				"added for this athlete.");
			return false;
		}

		// We store the times that each event took to complete, rather
		// than the cumulative times.  This simplifies the report method.
		int overallTime = 0;		// cumulative time so far

		// Start at the beginning of the array, cumulating the time,
		// until we find the first time that hasn't been entered.
		for (int i=0; i<times.length; i++) {

			// First time that hasn't been entered is encountered.
			if (times[i] == NOT_ENTERED) {

				// Make sure the next time for this athlete is at least
				// as big as the time that has accumulated so far.
				// NOTE: THIS CHECK WAS NOT REQUIRED.
				if (nextTime <= overallTime) {
					System.out.println ("Invalid time value.  Must be " +
						"larger than last time entered for this athlete.");
					return false;
				} else {
					times[i] = nextTime - overallTime;
					break;
				}

			// This time value was already entered, so add it onto
			// the cumulative total time.
			} else {
				overallTime += times[i];
			}
		}
		return true;
	}

	/*
	 * hasStarted: return 'true' if this athlete has at least
	 *    one event time recorded, indicating that this athlete
	 *    has started the race.
	 */
	public boolean hasStarted () {
		for (int i=0; i<times.length; i++) {
			if (times[i] != NOT_ENTERED) {
				return true;
			}
		}
		return false;
	}

	/*
	 * report: return a single line of information for one athlete
	 *    in the triathalon.  This information should include the
	 *    athlete's name, athlete number, swim time, bike time, run
	 *    time, and overall total time.  If any of the times were
	 *    not entered for this athlete report "NA" instead of an
	 *    actual time.  Use a tab ("\t") to separate each field.
	 */
	public String report () {
		// FILL IN THE BODY.
		String result = athleteName + "\t" + athleteNumber + "\t";

		// If at least one time has not been entered, we'll print
		// "NA" instead of the cumulative time for this athlete.
		boolean inComplete = false;

		int sum = 0;	// Sum up total time for this athlete.

		// Include swim, bike, and run times, or include "NA"
		// for any of these that have not been entered.
		for (int i=0; i<times.length; i++) {
			if (times[i] == NOT_ENTERED) {
				result += "NA" + "\t";
				inComplete = true;
			} else {
				result += times[i] + "\t";
				sum += times[i];
			}
		}

		// Include the overall time.
		if (inComplete) {
			result += "NA";
		} else {
			result += sum;
		}

		return result;
	}

	/*
	 * toString: return all information stored in this class.
	 *    This method is used for testing only.
	 */
	public String toString () {
		// THIS METHOD IS OPTIONAL - COMPLETE IF DESIRED.
		String result = "     Athlete Name: " + athleteName + "\n" +
			    "     Athlete Number:    " + athleteNumber + "\n" +
				"     Number of Events:  " + times.length + "\n";

		for (int i=0; i<times.length; i++) {
			result += "       Event " + i + ": " + times[i] + "\n";
		}

		return result;
	}
}
