/**
 *
 * The Name class is used to store a person's name.
 * Currently it only supports first and last names.
 *
 * <p> This is proably overkill for such a simple object
 * but its purpose is to demonstrate a functional object.
 *
 * @author Kevin Chu
 */

public class Name
{

	private String first;   // First name
	private String last;    // Last name


	/**
	 * Constructor
	 */
	public Name()
	{
		// This isn't needed for this example,
		// but it doesn't hurt either.
		super();
	}

	/**
	 * Clone Constructor
	 *
	 * @param obj clone this object.
	 */
	public Name(Name obj)
	{
		this();
		setFirst(obj.first);
		setLast(obj.last);
	}

	/**
	 * Full Constructor
	 *
	 * @param first - First name
	 * @param last - Last name
	 */
	public Name(
			String first,
			String last)
	{
		setFirst(first);
		setLast(last);
	}


	/**
	 * Set the value of first
	 *
	 * @param first 
	 */
	public void setFirst(String first)
	{
		this.first = first;
	}

	/**
	 * Get the value of first.
	 *
	 * @return First name
	 */
	public String getFirst()
	{
		return first;
	}


	/**
	 * Set the value of last
	 *
	 * @param last 
	 */
	public void setLast(String last)
	{
		this.last = last;
	}

	/**
	 * Get the value of last.
	 *
	 * @return Last name
	 */
	public String getLast()
	{
		return last;
	}


	/**
	 * Convert object to String
	 *
	 * @return Name as "First Last".
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append(first);
		sb.append(" ");
		sb.append(last);
		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)
	{
		// Test to make sure that obj is a Name object
		// If not, then it can't be equal to this object.
		// The "instanceof" operator makes this test.
		// The ! says NOT, so we are testing for "not a Name".
		// We also make sure obj is not null.
		if (obj == null || !(obj instanceof Name))
			return false;

		// Since obj is a Name object, lets cast it to one.
		Name tobj = (Name) obj;

		// Now we compare each individual field.
		// We also need to check if fields are null.
		if (first == null)
		{
			// Return false if the other first isn't also null.
			if (tobj.first != null)
				return false;
		}
		else
		{
			// Compare the two first fields.  Return false
			// if they don't match.
			if (!first.equals(tobj.first))
				return false;
		}

		// Now repeat all that for the "last" fields.
		if (last == null)
		{
			// Return false if the other last isn't also null.
			if (tobj.last != null)
				return false;
		}
		else
		{
			// Compare the two last fields.  Return false
			// if they don't match.
			if (!last.equals(tobj.last))
				return false;
		}

		// If we got this far, then the two object have equal values!
		// Let's return true and get on with our lives!
		return true;
	}

	/**
	 * Override the default hashCode() method.
	 * This allows classes like Vector and Hashtable to quickly
	 * identify an object.
	 *
	 * @return a "unique" hashCode for this object.
	 */
	public int hashCode()
	{
		int code = -100;	// Just a random base.
		if (first !=null)
			code += first.hashCode();
		if (last !=null)
			code += last.hashCode();
		return code;
	}

}
