/**
 * The BattingAverge class is used to store and calculate a player's
 * batting average.
 *
 * @author Kevin Chu
 */

public class BattingAverage
{

	private int hits;   // The number of hits a player has.
	private int atBats; // The number of "at bat" attempts a player has.


	/**
	 * Constructor
	 */
	public BattingAverage()
	{
		super();
	}

	/**
	 * Clone Constructor
	 *
	 * @param obj clone this object.
	 */
	public BattingAverage(BattingAverage obj)
	{
		this();
		setHits(obj.hits);
		setAtBats(obj.atBats);
	}

	/**
	 * Full Constructor
	 *
	 * @param hits - The number of hits a player has.
	 * @param atBats - The number of "at bat" attempts a player has.
	 */
	public BattingAverage(
			int hits,
			int atBats)
	{
		setHits(hits);
		setAtBats(atBats);
	}


	/**
	 * Set the value of hits
	 *
	 * @param hits The number of hits a player has.
	 */
	public void setHits(int hits)
	{
		this.hits = hits;
	}

	/**
	 * Get the value of hits.
	 *
	 * @return The number of hits a player has.
	 */
	public int getHits()
	{
		return hits;
	}


	/**
	 * Set the value of atBats
	 *
	 * @param atBats The number of "at bat" attempts a player has.
	 */
	public void setAtBats(int atBats)
	{
		this.atBats = atBats;
	}

	/**
	 * Get the value of atBats.
	 *
	 * @return The number of "at bat" attempts a player has.
	 */
	public int getAtBats()
	{
		return atBats;
	}


	/**
	 * Convert object to String
	 *
	 * @return a String representation of a BattingAverage object.
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("BattingAverage={");
		sb.append("hits=" + hits + ",");
		sb.append("atBats=" + atBats);
		sb.append("}");
		return sb.toString();
	}


	/**
	 * Determine if an object is equal to this object.
	 *
	 * @param obj the object to test.
	 * @return true if obj is equal to this object.
	 */
	public boolean equals(Object obj)
	{
		if (obj == null || !(obj instanceof BattingAverage))
			return false;

		BattingAverage tobj = (BattingAverage) obj;
		if (this.hits != tobj.hits)
			return false;
		if (this.atBats != tobj.atBats)
			return false;

		// Must be equal!
		return true;
	}


	/**
	 * Calculate and return a player's batting average.
	 * @return the player's batting average, or zero if there
	 *	are no stats.
	 */
	public double getBattingAverage()
	{
		if (atBats != 0)
		    return (double) hits / (double) atBats;
		else
		    return 0;
	}

}
