In this step you create a new Java Class.
You are in the J2EE Development perspective in the J2EE DC Explorer view.
...
1. Open the Web Module project node (...appl~tax~servlet...) and select the entry Source.
2. From the context menu, choose New → Java Class.
A dialog window Java Class appears.
3. Enter the following:
a. Package: org.example.tax.web
b. Name: TaxUIController
4. Choose Finish.
The dialog window Select Activity appears.
5. Select New J2EE Application and choose OK.
The Java editor is opened automatically.
6. Enter the following code:
package org.example.tax.web;
import java.text.NumberFormat; import java.util.Locale; import org.example.tax.ITaxCalculator; import org.example.tax.TaxFactory;
public class TaxUIController { private static NumberFormat usCurrencyFormatter = NumberFormat .getCurrencyInstance(Locale.US);
private double income = 0;
private double tax = 0;
public void setIncome(String s) { if (s == null) { income = 0; } else { try { if ((s.charAt(0) == '$') && (s.length() > 1)) { s = s.substring(1); } s = s.trim(); income = Double.parseDouble(s); } catch (Exception e) { income = 0; } } ITaxCalculator calculator = TaxFactory.createInstance(); tax = calculator.calculateTax(income); }
public double getIncome() { return income; }
public double getTax() { return tax; }
public String formatIncome() { return usCurrencyFormatter.format(income); }
public String formatTax() { return usCurrencyFormatter.format(tax); } }
|
7. Save your changes.
The class TaxUIController imports ITaxCalculator und TaxFactory from component appl/tax/calc.
You see compilation errors in the Tasks view: They will be repaired in the next step.
You have created the required Java class.