300 likes | 1.02k Views
Phone Book. search for a name and will then print out that person's phone number. The list of names and phone numbers is implemented as a list of PhoneEntry objects. PhoneEntry Class. public class PhoneEntry { String name; // name of a person String phone; // their phone number
E N D
Phone Book • search for a name and will then print out that person's phone number. • The list of names and phone numbers is implemented as a list of PhoneEntry objects
PhoneEntry Class public class PhoneEntry { String name; // name of a person String phone; // their phone number // creates a new phoneBookEntry with the specified information public PhoneEntry( String n, String p ) { name = n; phone = p; } //returns a description of this phoneEntry public String toString() { String description = ""; description += name + "\t" + phone +"\n"; return description; }
PhoneBook Class public class PhoneBook { PhoneEntry[] phoneBook; int currentSize; int count; public PhoneBook() // constructor { currentSize=6; phoneBook = new PhoneEntry[ currentSize ] ; count=0; }
PhoneBook Class continued public void addCustomer(String name, String phoneNumber) { System.out.println(count); phoneBook[count] = new PhoneEntry(name, phoneNumber); count++; }
PhoneBook Class continued public PhoneEntry search( String targetName ) {// use a linear search for (int j=0; j<phoneBook.________; j++) { if ( phoneBook[ j ].name.equals( ____________)) return phoneBook[ j ]; } return null; }
public PhoneEntry search( String targetName ) { for (int j=0; j<phoneBook.length; j++) { if ( phoneBook[ j ].name.equals( targetName)) return phoneBook[ j ]; } return null; }
phoneBook[ j ].name.equals( targetName ) • Look at it piece by piece: • phoneBook[ j ] contains a reference to a PhoneEntry object. • The PhoneEntry object contains an instance variable, name. • name is a reference to a String object. • A String object has an equals() method. • The equals() method is used with the String referred to by targetName . • The entire expression evaluates to true or false.
phoneBook[ j ].name.equals( targetName ) Does the expression • targetName.equals( phoneBook[ j ].name ) do the same thing as the above expression? • Yes
PhoneBook Class continued //returns a report describing the phoneBook collection public String toString() { String report = "+=========================\n"; report += "Number of Phone Entries " + count + "\n"; for (int p=0; p<count; p++) report += phoneBook[p].toString() + "\n"; return report; } }
PhoneBook Class public class phoneBookTester { public static void main (String[] args) { PhoneBook pb = new PhoneBook(); pb.addCustomer("Paul Kratides", "(815)439-9271"); pb.addCustomer("James Barclay", "(418)665-1223"); pb.addCustomer("Grace Dunbar", "(860)399-3044"); pb.addCustomer("Violet Smith", "(312)223-1937"); pb.addCustomer("John Wood", "(913)883-2874"); pb.addCustomer("Paul Kratides", "(815)439-9271");
PhoneBook Class // Display the list of Phone Entries System.out.println(pb); PhoneEntry entry = pb.search( "Violet Smith" ); // search for "Violet Smith" if ( entry != null ) System.out.println( entry.name + ": " + entry.phone ); else System.out.println("Name not found"); } }