Implement Person
- Choose File New in Project to add another class.
- Type Person.java as the name of the class.
- Deselect the Create header option.
- Click OK.
- Declare Person. Give it a single instance variable, personRecord, which contains information about the person. Write a constructor to initialize the variable.
- Write a method that validates the information in personRecord:
- Implement two more methods as shown below:
import next.util.*; import next.wo.*; public class Person extends Object { ImmutableHashtable personRecord; public Person(ImmutableHashtable personDict) { super(); personRecord = personDict; } }
Person initializes the personRecord instance variable using a dictionary returned by the Main component of the Registration application.
public ImmutableHashtable validate() { MutableHashtable valid = new MutableHashtable(); if ((((String)(personRecord.get("address"))).length() == 0) && (((String)(personRecord.get("name"))).length() == 0)) { valid.put("failureReason", "You must supply a name and address"); valid.put("valid", "No"); } else if (((String)(personRecord.get("name"))).length() == 0) { valid.put("failureReason", "You must supply a name."); valid.put("valid", "No"); } else if (((String)(personRecord.get("address"))).length() == 0) { valid.put("failureReason", "You must supply an address"); valid.put("valid", "No"); } else { valid.put("valid", "Yes"); } return valid; }
Person's validate method checks whether the data entered by the user includes values for a name and address. The validate method returns a dictionary. This dictionary contains a status message and a validation flag that indicates whether the registration should be allowed to proceed. If the user failed to enter a name or address, the validation flag value is "No," which disallows the registration. The status message then prompts the user to supply the missing information.
public String name() { return (String)(personRecord.get("name")); } public ImmutableHashtable personAsDictionary() { return personRecord; }
The name method is simply used to return the Person's name, while the method personAsDictionary returns a dictionary representation of the Person. The dictionary representation is used when the Person's data is written out to a file in a property list format (in RegistrationManager's writeRegistrantsToFile method).
Table of Contents Next Section