Java Reference
In-Depth Information
try {
System.out.print("Enter first name: ");
firstName = keyboard.readLine().trim();
System.out.print("Enter surname: ");
surname = keyboard.readLine().trim();
} catch(IOException e) {
System.err.println("Error reading a name.");
e.printStackTrace();
System.exit(1);
}
return new Person(firstName,surname);
}
// Rest of the class as before...
private static BufferedReader keyboard = new BufferedReader(
new
InputStreamReader(System.in));
}
Directory "TryPhoneBook 1"
You should have no trouble seeing how this works because it's almost identical to the
readPerson()
method that you used previously in this chapter.
You can make the
PhoneNumber
class very simple:
import java.io.*;
class PhoneNumber implements Serializable {
public PhoneNumber(String areacode, String number) {
this.areacode = areacode;
this.number = number;
}
@Override
public String toString() {
return areacode + " " + number;
}
private String areacode;
private String number;
private static final long serialVersionUID = 1001L;
}
Directory "TryPhoneBook 1"
You could do a whole lot of validity checking of the number here, but it's not important for the example.