What if we want to restrict the age of library users ?
Take a look at this class:
public class AgeValidator implements Validator {
String errorMessage = ""; public boolean validate(Field f) { int field = 0; try {
field = Integer.parseInt(f.getValue());
} catch (NumberFormatException e) { return false;
} if (field < 5) {
errorMessage = "Age should be bigger than 5"; return false;
} if (field > 150) {
errorMessage = "Age should be smaller than 150"; return false;
} return true;
} public String getErrorMessage() { return errorMessage;
}
}
And what are we supposed to do with it ?
Here's the modified define() method of persistent object LibraryUser:
public void define() {
addField("FirstName", new VarcharField(50));
addField("LastName", new VarcharField(50));
addField("Age", new IntField());
getField("Age").addValidator(new AgeValidator());
addField("Address", new VarcharField(100));
}
Now look how the validation will work:
Now you may add another validators by yourself, if you need those.
Our task is done. The virtual library is ready now. If you want to improve it - you now have enough knowledge about persistent objects and their usage. You will be able now to add new features by yourself.