public class TestContact
{
public static void printContacts(List<Contact> a)
{
System.out.println(" Contact List ");
System.out.println();
System.out.println(" Name Relation Birthday Phone Email");
System.out.println("----------------------------------------------------------------------------------");
for(Contact v : a)
{
System.out.println(v);
}
System.out.println();
}
public static void findByName(List<Contact> a, String s)
{
int top = a.size();
int bottom = -1;
int probe;
System.out.println("Find Name - " + s);
while (top - bottom > 1)
{
probe = (top + bottom) / 2;
Contact g = a.get(probe);
if(g.getName().compareTo(s) > 0)
top = probe;
else
bottom = probe;
}
Contact bg = a.get(bottom);
if ((bottom >= 0) && (bg.getName().compareTo(s) == 0))
System.out.println("Found: " + bg);
else
System.out.println("There was no " + s + "found");
}
public static void findByEmail(List<Contact> a, String s)
{
int top = a.size();
int bottom = -1;
int probe;
System.out.println("Find Email - " + s);
while (top - bottom > 1)
{
probe = (top + bottom) / 2;
Contact g = a.get(probe);
if(g.getEmail().compareTo(s) > 0)
top = probe;
else
bottom = probe;
}
Contact bg = a.get(bottom);
if ((bottom >= 0) && (bg.getEmail().compareTo(s) == 0))
System.out.println("Found: " + bg);
else
System.out.println("There was no " + s + " found");
}
public static void main(String [] args)
{
List<Contact> myContacts = new ArrayList<Contact>();
myContacts.add(new Contact("John Carter", "brother", "Mar 3", "(342)555-7069", "jcarter@carter.com"));
myContacts.add(new Contact("Elise Carter", "mom", "Apr 19", "(342)555-7011", "carterMom@carter.com"));
myContacts.add(new Contact("Ellie Carter", "me", "Jun 10", "(342)555-8102", "ecarter@carter.com"));
myContacts.add(new Contact("Sue Ellen", "friend", "Mar 9", "(341)555-9182", "susieE@hotmail.com"));
myContacts.add(new Contact("Frank Carter", "dad", "Dec 1", "(342)555-7011", "carterDad@carter.com"));
myContacts.add(new Contact("Johnnie", "friend", "Jan 21", "(341)555-7789", "jDawg5555@yahoo.com"));
printContacts(myContacts);
findByName(myContacts, "Johnnie");
findByEmail(myContacts, "jcarter@carter.com");
System.out.println();
}
}