/*
 * Category: This class stores information for one category in the
 *           triathalon.
 */

import java.util.*;
class Category {

	private String categoryName;	// name of the category
	private Vector athletes;		// Vector of Athlete objects

	// Static variable to keep track of the number of Category objects
	// that are created.  Each time a Category object is created, we
	// add one to this variable.  This variable determines the category
	// number for each Category, which is stored in the 'categoryNumber'
	// instance variable that is used to generate the athlete numbers.
	private static int categoryCounter = 0;

	// These two variables are used to generate the athlete numbers for
	// each new athlete registered in this category.
	private int categoryNumber;	// right most digit in athlete number is
								// the category number (0, 1, 2, etc).
	private int athleteCounter;	// remaining digits in athlete number count
								// each athlete registered (1, 2, 3, etc).

	/*
	 * Category constructor: Create a new category, with name, 'name'.
	 *    The constructor instantiates the Vector and initializes all
	 *    instance variables.
	 */
	public Category (String name) {
		// FILL IN THE BODY.
		this.categoryName = name;
		this.athletes = new Vector();

		// Right most digit of each athlete number is the category
		// number, which comes from the static 'categoryCounter' variable.
		this.categoryNumber = categoryCounter;
		categoryCounter++;

		// Left part of first athlete number starts at 1.
		this.athleteCounter = 1;
	}

	/*
	 * getName: return the name of the category.
	 */
	// FILL IN THE PROTOTYPE AND THE BODY.
	public String getName () {
		return categoryName;
	}

	/*
	 * addAthlete: Add athlete 'newAthlete' to the Vector of Athletes.
	 */
	public void addAthlete (Athlete newAthlete) {
		// FILL IN THE BODY.
		athletes.addElement(newAthlete);
		athleteCounter++;
	}

	/*
	 * deleteAthlete: Delete athlete with athlete number 'athleteNum'
	 *    from this category.  Note: do not worry about adjusting the
	 *    athleteCounter variable when athletes are deleted.
	 */
	public void deleteAthlete (int athleteNum) {
		// Examine each element in the Vector of athletes.  If an
		// element has an athlete number that matches the athlete
		// number passed to this method, delete this element.
		for (int i=0; i<athletes.size(); i++) {
			// FILL IN PART OF THE BODY.
			if (((Athlete)athletes.elementAt(i)).getNumber() == athleteNum) {
				athletes.removeElementAt(i);
				break;
			}
		}
	}

	/*
	 * generateNumber: This method generates an athlete number for the
	 *    next athlete to be registered in this category.  This athlete
	 *    number is returned by this method.  The right most digit is
	 *    the category number ("0" for the first category created, "1"
	 *    for the next category, etc).  The digits on the left count
	 *    each athlete in this category ("1" for the first, "2" for the
	 *    second, etc).
	 */
	public int generateNumber () {
		String newNumber = "" + athleteCounter + categoryNumber;
		return Integer.parseInt(newNumber);
	}

	/*
	 * findAthlete: Searches in the Vector of athletes for the
	 *    athlete with name 'athleteName'.  If found, it returns
	 *    a reference to this Athlete object, otherwise it returns
	 *    null to indicate the athlete was not found (simply
	 *    use the statement "return null" when you determine
	 *    this athlete name does not exist in this category).
	 */
	public Athlete findAthlete (String athleteName) {

		// Check if each athlete element in the Vector contains
		// the name 'athleteName' that was passed to this method.
		// FILL IN PART OF THE BODY.
		for (int i=0; i<athletes.size(); i++) {
			if (((Athlete)athletes.elementAt(i)).getName().
					equals(athleteName)) {
				return (Athlete)athletes.elementAt(i);
			}
		}

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

	/*
	 * findAthlete: Searches in the Vector of athletes for the
	 *    athlete with athlete number 'athleteNum'.  If found, it
	 *    returns a reference to this Athlete object, otherwise it
	 *    returns null to indicate the athlete was not found (simply
	 *    use the statement "return null;" when you determine
	 *    this athlete number does not exist in this category).
	 */
	public Athlete findAthlete (int athleteNum) {

		// Check if each athlete element in the Vector contains the
		// athlete number 'athleteNum' that was passed to this method.
		// FILL IN PART OF THE BODY.
		for (int i=0; i<athletes.size(); i++) {
			if (((Athlete)athletes.elementAt(i)).getNumber() == athleteNum) {
				return (Athlete)athletes.elementAt(i);
			}
		}

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

	/*
	 * hasStarted: This method returns true if at least one athlete
	 *    in this category has started the race.  It returns false
	 *    otherwise.
	 */
	public boolean hasStarted () {
		// Go through each element in the vector.  If any one of these
		// athletes has started the race, then return true.
		for (int i=0; i<athletes.size(); i++) {
			if (((Athlete)athletes.elementAt(i)).hasStarted()) {
				return true;
			}
		}

		return false;	// No athletes have started
	}

	/*
	 * report: Print information for all athletes in this category.  For
	 *    each athlete in this category, print the category name, athlete
	 *    name, athlete number (#), swim time, run time, bike time, and
	 *    total accumulated time.  If an athlete does not have a time
	 *    recorded for an event, NA is printed for the missing times.
	 *    Calls a method in the Athlete class to get some information.
	 *    - Print a tab ("\t") between each value to improve alignment.
	 */
	public void report () {
		// FILL IN THE BODY.
		if (athletes.size() == 0) {
			System.out.println ("\t" + categoryName +
				"\t- no athletes in category.");

		} else {
			// Print a line of output for each athlete in the category.
			for (int i=0; i<athletes.size(); i++) {
				System.out.println ("\t" + categoryName + "\t" +
					((Athlete)athletes.elementAt(i)).report());
			}
		}
	}

	/*
	 * 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 = "Category Name: " + categoryName + "\n" +
		    "   " + athletes.size() + " athletes in category" + "\n" +
			"   Category Number: " + categoryNumber + "\n" +
			"   Athlete Counter: " + athleteCounter + "\n";
		for (int i=0; i<athletes.size(); i++) {
			result += "   Athlete " + i + ":\n";
			result += athletes.elementAt(i).toString();
		}

		return result;
	}
}
