/*
 * University of Utah
 * 2005 High School Programming Contest
 * Record Matching
 *
 * Filename: RecordMatcher.java
 * 
 * Description: Implements a basic record matcher.  Will load a file
 * with a format as specified in the problem description and output indices
 * of matching pairs of records.  This basic implementation will consider
 * two records to be matching if the lowercased values of each field match.
 *
 * Modification History:
 * 2/21/2005 - Initial Release
 */

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

public class RecordMatcher {
  
  // Main
  // The filename of the input file is expected as a single
  // command line parameter.  If it is not provided, a message is
  // output to the screen and the program execution is terminated.
  // Otherwise, record matching is performed and the matching pair
  // indices are output to standard out, one pair per line.
  public static void main(String argv[]) {
    // Check command-line parameters
    if (argv.length != 1) {
      System.out.println("Expecting command line parameter.");
      System.out.println("Proper syntax: ");
      System.out.println("  java RecordMatcher <filename>");
      return;
    }
    
    Vector records = readFile(argv[0]);
    runRecordMatcher(records);
  }
  
  
  // Reads the specified file into a new vector of Record objects.
  //
  // Parameters:
  // filename - name of a file containg the records. Formatted as
  //            specified in problem description.
  public static Vector readFile(String filename) {
    Vector records = new Vector();
    try {
      BufferedReader input = new BufferedReader(new FileReader(filename));
      String uid;
      String fName;
      String lName;
      String address;
      String zip;
      String phone;
      String email;
      // Load each sentence in the story
      while (input.ready()) {
        uid = input.readLine().trim();
        fName = input.readLine().trim();
        lName = input.readLine().trim();
        address = input.readLine().trim();
        zip = input.readLine().trim();
        phone = input.readLine().trim();
        email = input.readLine().trim();
        // Eat empty line.
        input.readLine(); 
        records.addElement(new Record(uid, fName, lName, address,
                                      zip, phone, email));
      }      
      input.close();
    } 
    catch (Exception e) {
      // Note that I should probably do something more to handle the
      // exception, but I wanted to keep things simple for this 
      // contest. 
      System.out.println("Exception in readFile: " + e.toString());
    }
    return records;
  }
  
  
  // Performs record matching and outputs the indices of matching
  // pairs of recrods.
  // 
  // Parameters:
  // records - Vector of Record objects containing the records.  This
  //           function should not modify this data.
  public static void runRecordMatcher(Vector records) {
    for (int i = 0; i < records.size(); i++) {
      Record ri = (Record) records.elementAt(i);
      for (int j = i+1; j < records.size(); j++) {
        Record rj = (Record) records.elementAt(j);
        if (ri.lowerCaseEquals(rj)) {
          System.out.println(ri.getUID() + "," + rj.getUID());
        }
      }
    }
  }
  
  
}

