AP Computer Science --- Haas --- ArrayListAddressBook

Objective: Use the ArrayList class



/** <<< THIS CODE IS COMPLETE >>>
 *  a contact on an email list has a first name, last name,
 *  and email address.
 */
public class Contact
{
  private String firstName;
  private String lastName;
  private String email;

  public Contact(String firstName, String lastName, String email) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.email = email;
  }

  public String getFirst() { return firstName; }
  public String getLast() { return lastName; }
  public String getEmail()  { return email; }
} 


import java.util.*;

/** <<< Code Not Complete >>>
 * An AddressBook is a list of contacts in alphabetical 
 * order by last name, then first name.
 */
public class AddressBook {
 
  private ArrayList< Contact > list;

  public AddressBook() {
    list = new ArrayList< Contact >();
  }
  
  /**  <<< Code Not Complete >>>
   * 1) checks to see if first and last name is on list
   * 2) if not already on the list the new contact is added
   * 3) maintains the alphabetical order of the list
   */
  public void addContact(String first, String last, String email) {
       
            <<< COMPLETE THE CODE >>>

  }
  
  /** <<< Code Not Complete  >>>
   * looks for first and last name match and removes it if found
   */
  public void removeContact(String first, String last) {
             
            <<< COMPLETE THE CODE >>>


  }
  
  /** <<< Code Not Complete >>>
   * returns a new list of address by combining 
   * this list with other list 
   * new list is in alphabetical order
   * duplicates are removed
   */
  public AddressBook mergeLists(AddressBook otherList) {
       
            <<< COMPLETE THE CODE >>>


  }
  
  /** 
   * <<< Code Not Complete >>>
   * returns list of contacts: first name, last name, email address
   */
  public String toString() {
          
            <<< COMPLETE THE CODE >>>

  }

}   
  


/** <<< CODE COMPLETE --- output of tester >>>
 
 <<< mylist >>> 
Ann Bell belly456@aol.com
Gladys Bell gbell23@aol.com
Sandy Brown brwnsgr@aol.com
Mary Jones sushigirl@hvc.rr.com
Jim Smith jimbo7687@aol.com

<<< mylist >>> 
Gladys Bell gbell23@aol.com
Sandy Brown brwnsgr@aol.com
Mary Jones sushigirl@hvc.rr.com

<<< otherlist >>> 
Ann Bell belly456@aol.com
Sandy Brown brwnsgr@aol.com
Chip Larr pizzaman@yahoo.com
Lucy Morris icollectstuff@aol.com

<<< mergedList >>> 
Ann Bell belly456@aol.com
Gladys Bell gbell23@aol.com
Sandy Brown brwnsgr@aol.com
Mary Jones sushigirl@hvc.rr.com
Chip Larr pizzaman@yahoo.com
Lucy Morris icollectstuff@aol.com

*/

import java.util.*;

public class Tester {

  public static void main(String args[]) {

    AddressBook myList = new AddressBook();
    myList.addContact("Sandy","Brown","brwnsgr@aol.com");
    myList.addContact("Jim","Smith","jimbo7687@aol.com");
    myList.addContact("Mary","Jones","sushigirl@hvc.rr.com");
    myList.addContact("Jim","Smith","jimbo7687@aol.com");    
    myList.addContact("Gladys","Bell","gbell23@aol.com");
    myList.addContact("Jim","Smith","jimbo7687@aol.com");
    myList.addContact("Ann","Bell","belly456@aol.com");
    
    System.out.println("<<< mylist >>> \n" + myList);
    
    myList.removeContact("Jim","Smith");
    myList.removeContact("Pam","Harris");
    myList.removeContact("Ann","Bell");
    
    System.out.println("<<< mylist >>> \n" + myList);

    AddressBook otherList = new AddressBook();
    otherList.addContact("Sandy","Brown","brwnsgr@aol.com");
    otherList.addContact("Chip","Larr","pizzaman@yahoo.com");
    otherList.addContact("Lucy","Morris","icollectstuff@aol.com");
    otherList.addContact("Ann","Bell","belly456@aol.com");
    
    System.out.println("<<< otherlist >>> \n" + otherList);
    
    AddressBook newList = myList.mergeLists(otherList);
    System.out.println("<<< mergedList >>> \n" + newList);
  }
}