import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;

class Word {
  private String word;
  private int count;
  
  public Word( String word ) {
    this.word = word;
    this.count = 1;
  }
  
  public String toString() {
    return word + " has " + count + " instances.";
  }
  
  public void increment() {
    ++count;
  }
  
  public String getWord() {
    return word;
  }
}

class HW17 {
  private ArrayList<Word> list;
  
  public HW17( String filename ) {
    list = new ArrayList<Word>();
    doReadFromFile( filename );
  }
  
  private void doReadFromFile( String filename ) {
    // make a Scanner tied to the file
    Scanner scanner = null;
    try {
      scanner = new Scanner( new File( filename ) );
    } catch ( java.io.FileNotFoundException e ) {
      System.out.println( "Couldn't open " + filename );
      System.exit(-1);  // terminate the program
    }
    
    // build the word list
    while ( scanner.hasNext() ) {
      String term = scanner.next();
      
      int position = locationInList( term );
      if ( position < 0 ) {
	list.add( new Word( term ) );
      } else {
	list.get( position ).increment();
      }
    }
  }

  private int locationInList( String term ) {
    int position = 0;
    
    for ( Word word : list ) {
      if ( word.getWord().equalsIgnoreCase( term ) )
	return position;
      ++position;
    }
    
    return -1;  // not in the list of known words
  }
  
  public int uniqueWordCount() {
    return list.size();
  }
  
  
  public void printWordStats() {
    for ( Word word : list )
      System.out.println( word );
  }

  public static void main( String[] arg ) {
    new HW17( "HW17.java" ).printWordStats();
  }
} // end class HW17

