Assignment #16
Maintain a team roster
Due Date: Thursday of Week 6
30 points
The logic of this project is somewhat complicated because it involves changing the text fields and buttons that are available depending on the actions the user takes. It also requires a number of event listeners. However, it is similar to the Product Maintenance application in the book, so it should be manageable.
Console
Operation
- This application accepts user entries and validates them according to the specifications below. If the data is valid, a dialog box with the message "Data accepted" is displayed and the text fields are cleared. If an entry isn't valid, a dialog box is displayed with an appropriate error message and the focus is moved to the text field with the invalid data.
Specifications
- Create a class named Validator that holds a collection of validation requirements for one or more text fields. The constructors and methods for the Validator class follow:
- To use the Validator class, you first call the methods that add validation requirements to the Validator object. The Validator object maintains these requirements in an array list. Then, you call the validate method to validate the text fields against the requirements that have been established.
- Create a class with a frame that includes three text fields: name, which is a required field; age, which must be an integer from 0 to 120; and sales, which must be a valid double value.
- The frame must also define a button labeled "OK" that performs the validation. If the data is validated successfully, a dialog box with the message "Data accepted" should be displayed, and the contents of the five text fields should be cleared.
Constructor | Description |
---|---|
Validator(JPanel panel) | Creates a Validator object. The JPanel is used to position any error messages that might be displayed when data is validated. |
Method | Description |
void addRequiredField(JTextField t, String name) | Adds a required text field. |
void addIntegerField(JTextField t, String name) | Adds a text field into which the user must enter a valid integer. |
void addDoubleField(JTextField t, String name) | Adds a text field into which the user must enter a valid double value. |
void addIntMinField(JTextField t, int min, String name) | Adds a text field into which the user must enter an integer value that is greater than or equal to the specified value. The addInteger method must be called for the text field prior to calling this method. |
void addIntMaxField(JTextField t, int max, String name) | Adds a text field into which the user must enter a value that is less than or equal to the specified value. The addInteger method must be called for the text field prior to calling this method. |
boolean validate() | Performs the validations that have been created by the various add methods, in the order those methods were called. If any text field fails validation, a message box is displayed to indicate the error, focus is moved to the field with the invalid data, and the validate method returns false. Additional fields are not validated. |
Hints
- Don't hesitate to define one or more private classes or interfaces nested within the Validator class to simplify the solution.
- One benefit of defining the Validator class this way is so that you can place the code that sets up the validation for each text field along with the code that creates the fields. Thus, you should place the code that calls the various add methods of the Validator class near the code that sets up the panel layout, not in the code that handles the action event. The event handler code should simply call the validate method to validate the data.
Enhancements
- Add additional validation options, such as min and max for double values or a range checking validation that combines the min and max functions.
- Modify the Validator class so that instead of displaying error messages in a dialog box, it displays error messages in labels on the panel. The add methods will have to be modified so they accept a JLabel parameter, and the validate method will have to be modified so that instead of displaying a dialog box, it places the error message in the specified label. In addition, the validate method should continue validating fields even after it encounters an invalid field. However, focus should be given to the first field in error.
- Modify the Product Maintenance application that's presented in the chapter (figures 17-17 through 17-19) to use the Validator class.
- Name of program
- Programmer's name
- Current Date
- Computer system and compiler you are using
- Brief description of the program
Compile and run the Java program.
Take a screen capture by hitting simultaneously the keys Alt and Print Screen (Prt Sc) of the output results (console) and paste it in MS Paint. Save it as a gif file.
When you are ready, upload the java program and the gif file by clicking the link below:I cannot grade if I don't get the files and receive your email.