/*************************************************************************
 *  Compilation:  javac Student.java StdIn.java
 *  Execution:    java Student N < students.txt
 *
 *  Read in an integer N from command line. Then, read in a list
 *  of N student records from standard input into a Student data type.
 *  Each record consists of four fields, separated by whitespace:
 *      - first name
 *      - last name
 *      - email address
 *      - which section they're in
 *
 *  Enable sorting be any of the 4 fields.
 *
 *************************************************************************/


public class Student implements Comparable {
    public final static int FIRST   = 0;
    public final static int LAST    = 1;
    public final static int EMAIL   = 2;
    public final static int SECTION = 3;

    private static int sortField = 3;      // field to use as the sort key

    private String first;
    private String last;
    private String email;
    private int    section;

    // set the field for sorting
    public static void setSortKey(int field) {
        if (field < 0 || field > 3) throw new RuntimeException("Illegal sort key");
        sortField = field;
    }

    // constructor
    public Student(String first, String last, String email, int section) {
        this.first   = first;
        this.last    = last;
        this.email   = email;
        this.section = section;
    }

    // compare keys - only sign matters in answer returned
    public int compareTo(Object x) {
        Student a = this;
        Student b = (Student) x;
        if      (sortField == FIRST)   return a.first.compareTo(b.first);
        else if (sortField == LAST)    return a.last.compareTo(b.last);
        else if (sortField == EMAIL)   return a.email.compareTo(b.email);
        else                           return a.section - b.section;
    }

    // return a string representation of the invoking object
    public String toString() {
        return section + " " + first + " " + last + " " + email;
    }

}