import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class DatingAgency
{
  /**
     Constructor -- instantiation of our object
  */
  public DatingAgency()
  {
    // this handles our collection class -- an array list in this case.
    datingagency = new ArrayList();
  }

  public void addClient(Client clientIn, String hobbyOne, String hobbyTwo, String hobbyThree)
  {
    if( ( clientIn.addHobby(new Hobby(hobbyOne,"")) && !clientIn.addHobby(new Hobby(hobbyTwo, ""))
	   && !clientIn.addHobby(new Hobby(hobbyThree, ""))) )
      {
	System.out.println("ERROR adding hobby");
	System.exit(0);
      }

     datingagency.add(clientIn);
     this.matchNewClientHobbies(clientIn);
  }
  
  public static void main(String args[])
  {
    // instantiate a new object
    DatingAgency da = new DatingAgency();
    da.addClient( new Client("Freddie",21,'m',"(055)223344"), "Walking", "Running", "Cycling" );
    da.addClient( new Client("Jason",29,'m',"(023)765453"), "Eating", "Reading", "Fishing");
    da.addClient( new Client("John",38,'m',"(011)776872"), "Eating", "Fishing", "Cycling");

    // display
    //da.display();

    Client mytemp = da.findClientByName("Freddie");

    if( mytemp != null)
    {
      mytemp.display();
    } else {
      System.out.println("Client was not found.");
    }
  }

  public Client findClientByName(String nameIn)
  {
    // set up an iterator
    Iterator i = datingagency.iterator();
    boolean found = false;
    Client tempClient = null;
    
    // Look for the name
    while ( i.hasNext() && !found )
    {
      tempClient = (Client)i.next();
      if ( tempClient.getName().equals(nameIn) )
      {
	found = true;
      } else {
	// Hmm, nothing found.
	tempClient = null;
	found = false;
      }
    }
    return tempClient;
  }

  private void matchNewClientHobbies(Client clientIn)
  {
    Iterator i = datingagency.iterator();
    while( i.hasNext() )
    {
      Client nextClient = (Client)i.next();
      //System.out.println( ((Hobby)(nextClient.getHobbies())).size());
       
      if ( (nextClient.getHobbies()).equals(clientIn.getHobbies()) )
      {
	// match
	System.out.print("\n--------------------------");
      	nextClient.display();
      } else {
	System.out.println("** NO MATCH **");
      }
    }
  }

  public void display()
  {
    // should we instantiate the Iterator here?
    Iterator i = datingagency.iterator();

    while ( i.hasNext() )
    {
      ( (Client)i.next() ).display();
    }
  }
	
  // attributes
  private ArrayList datingagency;
  private Hobby hobby;
}