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

/** a concrete class that extends abstract class
  ArrOfBosses by providing a constructor and an
  implementation of the sort() method */
public class DemoBoss extends ArrOfBosses {
  /** constructs an arrray of CompareBoss objects by
    reading the records from the keyboard */  
  public DemoBoss() {
    init();
  }

  /** implement the Bubble Sort algorithm which 
    sort the CompareBoss array in lexicographical 
    order */
  public void sort() {
    for (int i = numOfBosses; --i >= 0;) 
      for (int j = 0; j < i; j++) 
        if ((arrOfBosses[j]).compare(arrOfBosses[j+1]) > 0)
          swap(j, j+1);
  }

  /** an internal method that reads the CompareBoss
    records from the keyboard and create a DemoBoss object */
  private void init() {
    String line;  // the next input line
    int size;  // max # of CompareBoss objects
    int count = 0; // actual # of CompareBoss input
    StringTokenizer tokens;  // to tokenize next line
    String token;  // the next token of line being processed
    String first, last;  // boss's first and last names, respectively
    double wage; // boss's wage

    // open a character-based input stream to read keyboard input
    BufferedReader in = new BufferedReader (
                 new InputStreamReader(System.in));

    // catch IOException when doing read, then ignore it
    try {
        // prompt the user for # of Employee records to be processed
        System.out.print("Enter an integer for the # of Employee records: ");
        line = in.readLine();
        size = Integer.parseInt(line);

        // create an array to hold Employee objects
        arrOfBosses = new CompareBoss[size];

        // input Employee records from the keyboard
        for (; count < size; count++) {
          if ((line = in.readLine()) == null) 
            break;  // terminate for loop if no more input

          // create a new StringTokenizer object
          tokens = new StringTokenizer(line);  

          // extract tokens from the current line
          first = tokens.nextToken();  
          last = tokens.nextToken();  
          wage = Double.parseDouble(tokens.nextToken()); 
          // create Boss object, save it in array 
          arrOfBosses[count] = new CompareBoss(first, last, wage);
        }  // end of the input for loop
    } catch (IOException e) {
        System.out.println("IO Exception during readLine()");
        System.exit(-1);  // terminate the program with code -1
    } 
    numOfBosses = count;
  }  // end of init()
  
  /** swap two Boss objects of the array at two specified
    indexes i and j */
  private void swap(int i, int j) {
    CompareBoss temp = arrOfBosses[i];
    arrOfBosses[i] = arrOfBosses[j];
    arrOfBosses[j] = temp;
  }

  /** a main() that tests the reading of an array of CompareBoss 
    objects, sorting, then printing the results */
  public static void main(String[] args) {
    DemoBoss x = new DemoBoss(); // create an array object

    System.out.println("The input array contains:");
    x.print();  
    x.sort();
    System.out.println("The sorted array contains:");
    x.print();
  }
}
