/*
 * Triathalon: This class stores information for the triathalon.
 *             It contains information about each category.
 */

class Triathalon {

	private String triathalonName;		// Name of the triathalon
	private Category[] categoryList;	// Array of Categories
	private int categoryCount;			// Current number of categories
										//   added to array

	/*
	 * Triathalon constructor: Create a new triathalon, that is named 'name'
	 * and can accommodate a maximum of 'numCategories' number of categories.
	 */
	public Triathalon (String name, int numCategories) {
		// FILL IN THE BODY.
		this.triathalonName = name;
		this.categoryList = new Category[numCategories];
		this.categoryCount = 0;
	}

	/*
	 * getCategoryCount: Returns the number of categories that have
	 *    been added to the triathalon so far.
	 */
	// FILL IN THE PROTOTYPE AND THE BODY.
	public int getCategoryCount () {
		return categoryCount;
	}

	/*
	 * addCategory: Add 'newCategory' to the array of categories.  If
	 *    the array is already full, returns false.
	 */
	public boolean addCategory (Category newCategory) {
		// FILL IN THE PART OF THE BODY.
		if (categoryCount < categoryList.length) {
			categoryList[categoryCount] = newCategory;
			categoryCount++;
			return true;
		}

		return false;
	}

	/*
	 * findCategory: This method is passed an integer athlete number
	 *    'athleteNum'.  It returns a reference to the category that this
	 *    athlete is registered in.  If the athlete number is invalid,
	 *    this method returns null.
	 */
	public Category findCategory (int athleteNum) {

		// Convert athlete number to a String
		String athleteNumStr = (new Integer(athleteNum)).toString();

		// Last character in string is the category number.
		int categoryNum = Integer.parseInt
			("" + athleteNumStr.charAt(athleteNumStr.length()-1));

		// As long as the category number is valid, return this category.
		if (categoryNum >=0 && categoryNum < categoryList.length) {
			return categoryList[categoryNum];
		}

		// Athlete number was invalid, so return null.  This is a
		// way to put an empty value into an object reference.
		return null;
	}

	/*
	 * findCategory: Searches in the array of categories for the
	 *    category with name 'categoryName'.  If found, it returns
	 *    a reference to this category, otherwise it returns
	 *    null to indicate the category was not found.
	 */
	public Category findCategory (String categoryName) {

		// Go through each categoryList element and check if it contains
		// the 'categoryName' that was passed to this method.
		// FILL IN PART OF THE BODY.
		for (int i=0; i<categoryCount; i++) {
			if (categoryList[i].getName().equals(categoryName)) {
				return categoryList[i];
			}
		}

		// Category was not found, so return null.  This is a way
		// to put an empty value into an object reference.
		return null;
	}

	/*
	 * findAthlete: Searches the entire triathalon for an athlete
	 *    with name 'athleteName'.  If found, it returns a reference
	 *    to this athlete, otherwise it returns null to indicate the
	 *    athlete name was not found.
	 */
	public Athlete findAthlete (String athleteName) {

		// Go through each categoryList element and check if it contains
		// the 'athleteName' that was passed to this method.
		// FILL IN PART OF THE BODY.
		for (int i=0; i<categoryCount; i++) {
			Athlete athlete = categoryList[i].findAthlete(athleteName);
			if (athlete != null) {
				return athlete;
			}
		}

		// Category was not found, so return null.  This is a way
		// to put an empty value into an object reference.
		return null;
	}

	/*
	 * report: Print information for all categories in the triathalon.
	 *    First print the name of the triathalon.  Then for each category,
	 *    call a method to print the category name, and the results for each
	 *    athlete.  Reports an error message if there are no categories
	 *    currently in the triathalon.  Use tabs ("\t") to separate each
	 *    column of values.
	 */
	public void report () {
		// FILL IN THE BODY.
		System.out.println ("Triathalon: " + triathalonName);

		if (categoryCount == 0) {
			System.out.println ("No categories currently in triathalon.");

		} else {
			System.out.println
				("\tCateg.\tAthlete\t #\tSwim\tBike\tRun\tOverall");
			for (int i=0; i<categoryCount; i++) {
				categoryList[i].report();
				System.out.println();
			}
		}
	}

	/*
	 * 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.
		return "Triathalon Name: " + triathalonName + "\n" +
			   "   " + categoryList.length + " categories in triathalon.";
	}
}
