Java Reference
In-Depth Information
public BookEntry(Person person, PhoneNumber number) {
this.person = person;
this.number = number;
}
public Person getPerson() {
return person;
}
public PhoneNumber getNumber() {
return number;
}
@Override
public String toString() {
return person.toString() + '\n' + number.toString();
}
// Read an entry from the keyboard
public static BookEntry readEntry() {
return new BookEntry(Person.readPerson(),
PhoneNumber.readNumber());
}
private Person person;
private PhoneNumber number;
private static final long serialVersionUID = 1001L;
}
Directory "TryPhoneBook 1"
This is all pretty standard stuff. In the static readEntry() method, you just make use of the methods
that create Person and PhoneNumber objects using input from the keyboard, so this becomes very simple.
The class that implements the phone book is next — called the PhoneBook class, of course:
import java.io.Serializable;
import java.util.HashMap;
class PhoneBook implements Serializable {
public void addEntry(BookEntry entry) {
phonebook.put(entry.getPerson(), entry);
}
public BookEntry getEntry(Person key) {
return phonebook.get(key);
}
Search WWH ::




Custom Search