import java.io.*;
import java.util.*;

public class CandidateList3
{
  private ArrayList<Candidate> theList;

/** Construct a new candidate list with the given information.
**/
  public CandidateList3()
  {
    theList = new ArrayList<Candidate>();
  }

/******************************************************************************/
/* Accessors                                                                  */
/******************************************************************************/

/******************************************************************************/
/* Mutators                                                                   */
/******************************************************************************/

/******************************************************************************/
/* Utilities                                                                  */
/******************************************************************************/
/******************************************************************************/
  public int getListLength()
  {
    return this.theList.size();
  }

/******************************************************************************/
  public void getData(Scanner inDataFile)
  {
    Candidate theCandidate;
    System.out.println("Method 'getData' beginning");
    while(inDataFile.hasNext())
    {
      theCandidate = new Candidate();

      theCandidate.getData(inDataFile);
      this.theList.add(theCandidate);

    } // while(inDataFile.hasNext())

    System.out.println("Method 'getData' ending");
  } // public void getTheList(Scanner inDataFile)

/******************************************************************************/
  public void echoData(PrintWriter outDataFile)
  {
    outDataFile.printf("%nThe list of candidates is%n");
// note that this is a bit of a kluge to get the header string formatting
    outDataFile.printf("   %s%n",this.theList.get(0).getPrintHeaderString());
    for(int i = 0; i < this.getListLength(); ++i)
    {
      outDataFile.printf("%2d %s%n",i,this.theList.get(i).toString());
      outDataFile.flush();
    }
  } // public void echoData(PrintWriter outDataFile)

/******************************************************************************/
/** Outer layer of printing by match to print a header message and then
 *  invoke the inner layer for the loop and search.
**/
  public void printWithMatch(PrintWriter outDataFile,Fields which,String matchString)
  {
    if(Fields.FIRSTNAME == which)
    {
      outDataFile.printf("%nCandidates with FIRSTNAME matching %s are%n",matchString);
    }
    else if(Fields.LASTNAME == which)
    {
      outDataFile.printf("%nCandidates with LASTNAME matching %s are%n",matchString);
    }
    else if(Fields.PARTY == which)
    {
      outDataFile.printf("%nCandidates with PARTY matching %s are%n",matchString);
    }
    else if(Fields.STATE == which)
    {
      outDataFile.printf("%nCandidates with STATE matching %s are%n",matchString);
    }
    else if(Fields.BIRTHPLACE == which)
    {
      outDataFile.printf("%nCandidates with BIRTHPLACE matching %s are%n",matchString);
    }

    this.printByMatch2(outDataFile,which,matchString);
  } // public void printWithMatch(PrintWriter outDataFile,Fields which,String matchString)

/******************************************************************************/
/** Inner layer of printing by match to iterate through the arraylist and print
 *  if there is a match.
**/
  public void printByMatch2(PrintWriter outDataFile,Fields which,String matchString)
  {
// note that this is a bit of a kluge to get the header string formatting
    outDataFile.printf("   %s%n",this.theList.get(0).getPrintHeaderString());
    for(int i = 0; i < this.getListLength(); ++i)
    {
      if(this.theList.get(i).getField(which).equals(matchString))
      {
        outDataFile.printf("%2d %s%n",i,this.theList.get(i).toString());
        outDataFile.flush();
      }
    }
  } // public void printByMatch2(PrintWriter outDataFile,Fields.which,String matchString)

} // public class CandidateList3

