Java Reference
In-Depth Information
public PhoneNumber getNumber(Person key) {
BookEntry entry = getEntry(key);
if(entry != null) {
return entry.getNumber();
} else {
return null;
}
}
private HashMap<Person,BookEntry> phonebook = new HashMap<>();
private static final long serialVersionUID = 1001L;
}
Directory "TryPhoneBook 1"
To store BookEntry objects you use a HashMap<Person,BookEntry> member, phonebook . You use the
Person object corresponding to an entry as the key, so the addEntry() method has to retrieve only the
Person object from the BookEntry object that is passed to it and use that as the first argument to the
put() method for phonebook .
All you need now is a class containing main() to test these classes:
public class TryPhoneBook {
public static void main(String[] args) {
PhoneBook book = new PhoneBook();
// The phone
book
FormattedInput in = new FormattedInput();
// Keyboard
input
Person someone;
while(true) {
System.out.println("Enter 1 to enter a new phone book entry\n" +
"Enter 2 to find the number for a name\n" +
"Enter 9 to quit.");
int what = 0;
// Stores input
selection
try {
what = in.readInt();
} catch(InvalidUserInputException e) {
System.out.println(e.getMessage() + "\nTry again.");
continue;
}
switch(what) {
case 1:
book.addEntry(BookEntry.readEntry());
break;
case 2:
Search WWH ::




Custom Search