// Arup Guha
// 7/5/04 
// PhoneBookEntry class for 2004 BHCSI Pre-Test Program.
// This class manages a simple entry for a PhoneBook. It stores a first
// name, last name and a phone number.

// Edited on 2/6/06 for COP 3330 to illustrate implementing Comparable.
// This allows a collection of PhoneBookEntry objects to be sorted using
// the sort method in the Collections class.

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

public class PhoneBookEntry implements Comparable {

  private String firstName;
  private String lastName;
  private int number;

  public PhoneBookEntry(String fn, String ln, int num) {
    firstName = fn;
    lastName = ln;
    number = num;
  }

  // Changes the phone number for the current object to newnum.
  public void changeNumber(int newnum) {
    number = newnum;
  }

  // Changes the last name for the current object to newlastname.
  public void changeLastName(String newlastname) {
    lastName = newlastname;
  }

  // Returns a string representation of the object.
  public String toString() {
    return firstName+"\t"+lastName+"\t"+(number/10000)+"-"+(number%10000);
  }

  // Returns true iff ln is the same as the last name of the current
  // object.
  public boolean sameLastName(String ln) {
    return (lastName.equals(ln));
  }

  // Implements the compareTo method.
  public int compareTo(Object e) {

    // Check if we have an actual PhoneBookEntry object.
    if (e instanceof PhoneBookEntry) {

      // Calculate the difference in last names.
      int temp = (this.lastName).compareTo(((PhoneBookEntry)e).lastName);

      // Return the answer if the names differ.
      if (temp!=0) return temp;

      // Only go to first names if the last names are the same.
      temp = (this.firstName).compareTo(((PhoneBookEntry)e).firstName);

      // Return here if the first names differ (but the last didn't.)
      if (temp!=0) return temp;

      // Break ties by phone number in numerical order.
      return this.number - ((PhoneBookEntry)e).number;
    }

    // Default if the object being compared isn't a PhoneBookEntry object.
    return (e.toString()).compareTo(this.lastName);
  }

  // Returns a clone of the current object.
  public Object clone() {
    return new PhoneBookEntry(this.firstName, this.lastName, this.number);
  }

/*
  // Returns true iff o is a PhoneBookEntry object with the identical
  // components of the current object.
  public boolean equals(Object o) {

    if (o instanceof PhoneBookEntry) {
   
      // Check all three components.
      return( (((PhoneBookEntry)o).lastName).equals(lastName) &&
              (((PhoneBookEntry)o).firstName).equals(firstName) &&
               ((PhoneBookEntry)o).number == number);
      
    }
    return false; // If it's not a PhoneBookEntry, it can't be equal!
  }
*/

  // Prompts the user for information about a PhoneBookEntry object and
  // returns a newly created object based on that information.
  public static PhoneBookEntry getEntry() throws IOException {

    Scanner stdin = new Scanner(System.in);

    // Get all the information.
    System.out.println("What is the first name of the entry?");
    String fn = stdin.next();
    System.out.println("What is the last name of the entry?");
    String ln = stdin.next();
    System.out.println("What is the phone number of this entry?");
    int num = stdin.nextInt();

    // Create and return the new object.
    return new PhoneBookEntry(fn, ln, num);
  }

  public static void main(String[] args) throws IOException {

    Scanner stdin = new Scanner(System.in);

    int ans = 1, i=0;
    ArrayList<PhoneBookEntry> mylist = new ArrayList<PhoneBookEntry>();

    // Read upto 10 entries.
    while (ans==1 && i<10) {
      mylist.add(getEntry());
      System.out.println("Enter another(Y=1,N=0)?");
      ans = stdin.nextInt();
    }

    int num = i;

    // Print them all out.
    System.out.println("Unsorted.");
    for (PhoneBookEntry e: mylist)
      System.out.println(e);

    // Sort them.
    Collections.sort(mylist);

    // Print them out again.
    System.out.println("\n\nSorted!");
    for (PhoneBookEntry e: mylist)
      System.out.println(e);

  }
}
