import SimpleOutput;
import Entry;
import Student;
import Faculty;
import java.util.NoSuchElementException;

public class SchoolDirectoryAlt {

    private int maxSize;
    private int currentSize;
    private Entry [] entryArray;

    SchoolDirectoryAlt () {
        maxSize = 4;  // initial maximum
        currentSize = 0;
        entryArray = new Entry[maxSize];
    }

    public void add (Entry person) {
        // check if room in array, and double array size if needed
        if (currentSize == maxSize) {
            maxSize = 2*maxSize; 
            Entry [] temp = new Entry[maxSize];
            for (int i = 0; i < currentSize; i++)
                temp[i] = entryArray[i];
            entryArray = temp;  // update entryArray to the new, larger array
        }

        // add person to array, maintaining alphabetical order
        int i = currentSize;
        while (i > 0 && person.comesBefore(entryArray[i-1])) {
            entryArray[i] = entryArray[i-1];
            i--;
        }
        entryArray[i] = person;
        currentSize++;
    }

    public void print (SimpleOutput out) {
        out.println ("Directory Listing");
        for (int i = 0; i < currentSize; i++) 
            entryArray[i].print(out);
        out.println ("End of Listing");
    }

    public Entry lookup (String first, String second) {
        int lo = 0;
        int hi = currentSize;
        int mid = (hi + lo)/2;

        while (lo < hi) {
            if (entryArray[mid].equals(first, second))
                return entryArray[mid];
            if (entryArray[mid].comesBefore(first, second))
                lo = mid + 1;
            else hi = mid;
            mid = (hi + lo)/2;
        }
        // if person not found, generate an exception
        throw new NoSuchElementException(); 
    }

    public static void main (String [] argv) {
        // test of various methods
        // constructors
        SchoolDirectoryAlt dir = new SchoolDirectoryAlt();
        SimpleOutput out = new SimpleOutput ();

        Student stu1 = new Student("Terry", "Walker", "walkert@math.grin.edu",
                                   1970, "off-campus");
        Student stu2 = new Student("Barbara", "Ellen", "barbara@cs.grin.edu",
                                   2002, "12-34-56");
        Student stu3 = new Student("Donna", "Marie", "donna@math.grin.edu",
                                   1998, "3.1415926535");
        Student stu4 = new Student("Shamrock", "The Cat", "none",
                                   3000, "varies");
        Student stu5 = new Student("Muffin", "The Cat", "none",
                                   3000, "varies");

        Faculty fac1 = new Faculty("John", "Stone", "stone@math.grin.edu",
                                 "Science 2418", 3181,
                                 "Mathematics and Computer Science", 1983);
        Faculty fac2 = new Faculty("Henry", "Walker", "walker@cs.grinnell.edu",
                                 "Science 2420", 4208, 
                                 "Mathematics and Computer Science", 1973);
        Faculty fac3 = new Faculty("Janet", "Gibson", "gibson@grinnell.edu",
                                 "Science 0420", 3168,
                                 "Psychology", 1989);
        Faculty fac4 = new Faculty("Samuel", "Rebelsky","rebelsky@cs.grin.edu",
                                 "Science 2427", 4410,
                                 "Mathematics and Computer Science", 1997);

        // add entries to directory
        dir.add(stu1);
        dir.add(fac1);
        dir.add(stu2);
        dir.add(fac2);
        dir.add(stu3);
        dir.add(fac3);
        dir.add(stu4);
        dir.add(fac4);
        dir.add(stu5);

        // print directory
        dir.print(out);

        // check lookup
        Entry ent;

        try {
            out.println ("Searching for Barbara Ellen -- first entry");
            ent = dir.lookup("Barbara", "Ellen");
            ent.print(out);

            out.println ("Searching for Terry Walker -- last entry");
            ent = dir.lookup("Terry", "Walker");
            ent.print(out);

            out.println ("Searching for Muffin, The Cat");
            ent = dir.lookup("Muffin", "The Cat");
            ent.print(out);

            out.println ("Searching for Muffin, The Dog");
            ent = dir.lookup("Muffin", "The Dog");
            ent.print(out);
        } catch 
            (NoSuchElementException e) {
                out.println("Directory Entry not found:  ");
                out.println("   Exception caught:  " + e);
        }
    }
}
