Java Reference
In-Depth Information
Download email_07/src/stripesbook/model/PhoneNumber.java
package stripesbook.model;
public class PhoneNumber {
private String areaCode;
private String prefix;
private String suffix;
public PhoneNumber() {
}
public PhoneNumber(String areaCode, String prefix, String suffix) {
this .areaCode = areaCode;
this .prefix = prefix;
this .suffix = suffix;
}
/ * Getters and setters... * /
}
This makes it a lot easier to work with phone numbers, such as find-
ing all contacts that are in a given area code. We can just look at the
areaCode property and not worry about the format in which the user
entered the phone number.
The first thing to do is to change the phone number property in the
Contact class from String to PhoneNumber :
Download email_07/src/stripesbook/model/Contact.java
private PhoneNumber phoneNumber;
public PhoneNumber getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(PhoneNumber phoneNumber) {
this .phoneNumber = phoneNumber;
}
A type converter must create a PhoneNumber object from an input String .
This calls for an implementation of TypeConverter<PhoneNumber> :
Download email_07/src/stripesbook/ext/PhoneNumberTypeConverter.java
package stripesbook.ext;
public class PhoneNumberTypeConverter
implements TypeConverter<PhoneNumber>
{
private static final Pattern pattern = Pattern.compile(
"\\(?(\\d{3})\\)?[-. ]?(\\d{3})[-. ]?(\\d{4})");
public PhoneNumber convert(String input,
Class<? extends PhoneNumber> type,
Collection<ValidationError> errors)
{
PhoneNumber result = null ;
 
 
Search WWH ::




Custom Search