Java Reference
In-Depth Information
fieldName = "phone";
fieldValue = "1-800-555-1234"; // valid
field = lookup(fieldName);
printValid(field, fieldValue,
field.validate(fieldValue));
fieldName = "email";
fieldValue = "john@doe"; // invalid - missing
.<tld>
field = lookup(fieldName);
printValid(field, fieldValue,
field.validate(fieldValue));
fieldName = "ssn";
fieldValue = "111-11-1111"; // valid
field = lookup(fieldName);
printValid(field, fieldValue,
field.validate(fieldValue));
}
}
Running the above code results in the following output:
password("1Cxy9") valid: false
phone("1-800-555-1234") valid: true
email("john@doe") valid: false
ssn("111-11-1111") valid: true
How It Works
Notice that the enhanced FieldType enum now defines a fieldName instance
variable and a constructor with a fieldName String argument for initializing the
instance variable. Each enum constant (again, each constant being an instance of
FieldType ) must be instantiated with a fieldName. FieldType also defines
an abstract validate(String) method that each enum constant must imple-
ment to perform the field validation. Here, each FieldType 's validate() method
Search WWH ::




Custom Search