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

public class CandidatesArray
{
  private int recordCount;
  private Candidate[] theArray;

/** Construct a new candidate list with the given information.
**/
  public CandidatesArray(int howMany)
  {
    this.theArray = new Candidate[howMany];
    this.setRecordCount(0);
  }

/******************************************************************************/
/* Accessors                                                                  */
/******************************************************************************/
  public int getRecordCount()
  {
    return this.recordCount;
  }

/******************************************************************************/
/* Mutators                                                                   */
/******************************************************************************/
  public void setRecordCount(int value)
  {
    this.recordCount = value;
  }
/******************************************************************************/
  public void incrementRecordCount()
  {
    ++(this.recordCount);
  }

/******************************************************************************/
/* Utilities                                                                  */
/******************************************************************************/
/******************************************************************************/
  public int getArrayLength()
  {
    return this.theArray.length;
  }

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

      theCandidate.getData(inDataFile);
      this.theArray[this.getRecordCount()] = theCandidate;

      this.incrementRecordCount();
    } // while(inDataFile.hasNext())

    System.out.println("Method 'getData' ending");
  } // public void getTheArray(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.theArray[0].getPrintHeaderString());
    for(int i = 0; i < this.getRecordCount(); ++i)
    {
      outDataFile.printf("%2d %s%n",i,this.theArray[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.theArray[0].getPrintHeaderString());
    for(int i = 0; i < this.getRecordCount(); ++i)
    {
      if(this.theArray[i].getField(which).equals(matchString))
      {
        outDataFile.printf("%2d %s%n",i,this.theArray[i].toString());
        outDataFile.flush();
      }
    }
  } // public void printByMatch2(PrintWriter outDataFile,Fields.which,String matchString)

/******************************************************************************/
/** Simple sorting routine to sort the list data lexicographically on the
 *  given field.
**/
  public void sortData(Fields which)
  {
    String s1 = "", s2 = "";
    Candidate tempCandidate = null;

    for(int i = 0; i < this.getRecordCount()-1; ++i)
    {
      for(int j = i+1; j < this.getRecordCount(); ++j)
      {
        s1 = this.theArray[i].getField(which);
        s2 = this.theArray[j].getField(which);
        System.out.printf("(i %2d %s) (j %2d %s) ",i,s1,j,s2);
        if(s1.compareTo(s2) > 0)
        {
          System.out.printf(" SWAP");
          tempCandidate = this.theArray[i];
          this.theArray[i] = this.theArray[j];
          this.theArray[j] = tempCandidate;
        }
        System.out.printf("%n");
      }
    }
  } // public void sortData(Fields which)

} // public class CandidatesArray

