Java Reference
In-Depth Information
5.
Delete the following statement (contained in the try block):
doubleEmpPR = Double.parseDouble(empPayRate);
This statement is no longer needed because setEmployeeProperties retrieves and parses the pay rate.
In the if statement, after the catch block, replace doubleEmpPR with emp.getPayRate() as
follows:
6.
if (emp.getPayRate() != -1) {
Originally, doubleEmpPRwas initialized to -1 so that we could ensure that a valid numeric pay rate value had
been retrieved from the frame. However, we really should use a separate and more descriptive variable to indicate an
error. In addition, we should add further validation checks on the pay rate value. For instance, do you think that a pay
rate of 99 cents is valid?
To summarize, we need to do two important things:
A.
Check for the number format exception when the text is parsed
B.
Validate that the pay rate value is valid
One of these functions should be included in the setEmployeeProperties method, and the other should be in the
Employee class. Can you identify which function should go where?
The number format exception check should be in EnterEmpInfo's setEmployeeProperties method. Because
setEmployeeProperties retrieves text data and parses the text to a primitive, it is the logical place to perform the check.
Data validation is one of the key functions of a setter. Therefore, the validation that the pay rate is within a certain
range should be in the setPayRate method of Employee class. We will code the setter data validation in the next section.
7.
Cut and paste the try and catch from actionPerformed to setEmployeeProperties and
change the code it so that the setPayRate and setExemptions appears as follows:
try {emp.setPayRate(Double.parseDouble(empPRTF.getText()));
emp.setExemptions(Integer.parseInt(exmpChoice.getSelectedItem()));
}
catch (NumberFormatException error) {
resultLbl.setForeground(Color.red);
resultLbl.setText("Pay rate must be a" +
"numeric value: 1, 2, 3...");
}
8.
After the catch header but before setting the foreground color to red, add the following
statements to erase the incorrect pay rate from the textfield and set pay rate to -1:
empPRTF.setText("");
emp.setPayRate(-1);
The beginning of the actionPerformed method should look like the following:
public void actionPerformed(ActionEvent e) {
this .setEmployeeProperties();
resultLbl.setForeground(Color.black);
if (emp.getPayRate() != -1) {
Search WWH ::




Custom Search