Java Reference
In-Depth Information
The result will be the message: “Gross Salary is: $413.35.” Notice that the currency formatter rounded the result to
two digits.
The actionPerformed method should look like the following:
public void actionPerformed(ActionEvent e) {
String stringTaxAmt = new String();
String empPayRate = empPRTF.getText();
double doubleEmpPR = -1, doubleGross;
resultLbl.setForeground(Color. black );
try {
doubleEmpPR = Double. parseDouble (empPayRate);
} catch (NumberFormatException Error) {
empPRTF.setText("");
resultLbl.setForeground(Color. red );
resultLbl.setText("Pay rate must be a numeric " +
"value: 1, 2, 3...");
}
if (doubleEmpPR != -1) {
if (dispBtn == e.getSource()) {
Employee emp = new Employee(empNameTF.getText(),
empStreetTF.getText(), cityTF.getText(), stateTF.getText(), zipTF.getText());
EmployeeFrame ef = new EmployeeFrame(emp);
} else {
if (grossBtn == e.getSource()) {
Employee emp = new Employee("", "", "", "", "");
resultLbl.setText("Gross Salary is: " +
emp.grossSalaryCalcFormatted(doubleEmpPR));
} else {
if (taxBtn == e.getSource()) {
Employee emp = new Employee("", "", "", "", "");
String exmp = exmpChoice.getSelectedItem();
int intExmp = Integer. parseInt (exmp);
stringTaxAmt = emp.fedTaxCalcFormatted(doubleEmpPR, intExmp);
resultLbl.setText("The Federal Tax amount is: " + stringTaxAmt);
}
}
}
// System.out.println(empPayRate);
}
}
Tidying Up Our Messy Method
We have incrementally added source code to EnterEmpInfo with very little thought about the eventual scope and
function of the application. Whoa, that's a big statement! Essentially it means that we have been adding on to a house
room by room instead of first designing the house (i.e., flowcharting the class's function) and then building the
needed foundation, walls, plumbing, and so on (i.e., class variables, methods, and method variables). Because of this,
we have redundancy and inefficiency in our class.
Search WWH ::




Custom Search