Is this program working? Please help.
Hello everybody :-)
If you have a free minute, could you please check and see if this program works? This supposed to be a tax calculator and it runs as a desktop application, not an applet.
This is really important for me.
I am looking forward for your feedback. If there is an error please post the error message.
I appreciate all your help and thanks in advance.
The program consists of 2 classes:
StateTaxComputationTeamB.java This file carries main() method
And
StateTaxComputationUITeamB.java - this files carries user interface and makes the calculation.
Thanks.
/**
*Week 2 TEAM B Programming Exercise
*Filename: StateTaxComputationTeamB.java
*Purpose: Calculate States Tax
*
*@author TEAM B
*@since June 04 2007
*
*/
import javax.swing.*;
publicclass StateTaxComputationTeamB{
publicstaticvoid main(String [] args){
// Create User Inteface Object
StateTaxComputationUITeamB UI =new StateTaxComputationUITeamB("State Tax Calculator", 275, 210,false);
// Setup default behaviour when user closes the application
UI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Show user interface frame
UI.show();
}
}
// END of file StateTaxComputationTeamB.java
/**
*Week 2 TEAM B Programming Exercise
*Filename: StateTaxComputationUITeamB.java
*Purpose: This class creates Tax Calculator User Interface and Calculates the tax
*
*@author TEAM B
*@since June 04 2007
*
*/
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
publicclass StateTaxComputationUITeamBextends JFrameimplements ActionListener{
privatestaticfinaldouble INDEX = 0.03;
privatestaticfinalint MIN_INCOME = 600;
private JLabel resultLabel =new JLabel();
private JButton computeBtn =new JButton("Compute");
private JTextField incomeTxt =new JTextField(10);
private JTextField dependentTxt =new JTextField(10);
/**
* Default constructor
*/
public StateTaxComputationUITeamB(String title,int width,int height,boolean isResizable){
// Set frame size
setTitle(title);
setSize(width, height);
// Is frame resizable?
this.setResizable(isResizable);
Container cont = getContentPane();
GridLayout layout =new GridLayout(0,1);
cont.setLayout(layout);
// Create elements, set up layouts and add to the main frame
// Header message label
JPanel headerPanel =new JPanel(new FlowLayout());
JLabel headerLabel =new JLabel("State Tax Computation Program");
headerPanel.add(headerLabel, BorderLayout.CENTER);
// Income text field and label.
SpringLayout incomeLayout =new SpringLayout();
JPanel incomePanel =new JPanel(incomeLayout);
JLabel incomeLabel =new JLabel("Income: ");
incomePanel.add(incomeLabel);
incomePanel.add(incomeTxt);
incomeLayout.putConstraint(SpringLayout.WEST, incomeLabel, 95, SpringLayout.WEST, incomePanel);
incomeLayout.putConstraint(SpringLayout.NORTH, incomeLabel, 5, SpringLayout.NORTH, incomePanel);
incomeLayout.putConstraint(SpringLayout.WEST, incomeTxt, 5, SpringLayout.EAST, incomeLabel);
incomeLayout.putConstraint(SpringLayout.NORTH, incomeTxt, 5, SpringLayout.NORTH, incomePanel);
// Number of Dependents text field and label
SpringLayout dependentLayout =new SpringLayout();
JPanel dependentPanel =new JPanel(dependentLayout);
JLabel dependentLabel =new JLabel("Number of Dependents: ");
dependentPanel.add(dependentLabel);
dependentPanel.add(dependentTxt);
dependentLayout.putConstraint(SpringLayout.WEST, dependentLabel, 8, SpringLayout.WEST, dependentPanel);
dependentLayout.putConstraint(SpringLayout.NORTH, dependentLabel, 5, SpringLayout.NORTH, dependentPanel);
dependentLayout.putConstraint(SpringLayout.WEST, dependentTxt, 5, SpringLayout.EAST, dependentLabel);
dependentLayout.putConstraint(SpringLayout.NORTH, dependentTxt, 5, SpringLayout.NORTH, dependentPanel);
// Compute button
SpringLayout buttonLayout =new SpringLayout();
JPanel buttonPanel =new JPanel(buttonLayout);
computeBtn.addActionListener(this);
buttonPanel.add(computeBtn);
buttonLayout.putConstraint(SpringLayout.WEST, computeBtn, 177, SpringLayout.WEST, buttonPanel);
buttonLayout.putConstraint(SpringLayout.NORTH, computeBtn, 5, SpringLayout.NORTH, buttonPanel);
// Result label
JPanel resultPanel =new JPanel();
resultPanel.add(resultLabel);
// Add all components to the main container
cont.add(headerPanel);
cont.add(new JSeparator(JSeparator.HORIZONTAL));
cont.add(incomePanel);
cont.add(dependentPanel);
cont.add(buttonPanel);
cont.add(resultPanel);
}
/**
* Calculate result. Incldes error checking.
*
* @return void
*/
private String computeResult(){
double income;
long numDependents;
String msg;
Locale locale = Locale.US;
// Catch number format exceptions.
try{
income = Double.parseDouble(incomeTxt.getText());
}
catch(NumberFormatException e){
msg ="ERROR: Please correct value of Income.";
return msg;
}
try{
numDependents = Integer.parseInt(dependentTxt.getText());
}
catch(NumberFormatException e){
msg ="ERROR: Please correct value of Dependents";
return msg;
}
// Income cant't be negative
if(income < 0){
msg ="ERROR: Please correct value of Income.";
return msg;
}
// Number of Dependents can't be negative
elseif(numDependents < 0){
msg ="ERROR: Please correct value of Dependents.";
return msg;
}
// Income must be greater then Number of Dependents * Minimum Income
elseif(income < (MIN_INCOME * numDependents)){
msg ="State Tax is " + NumberFormat.getCurrencyInstance(locale).format(0);
return msg;
}
// If we are here then calculate the tax, format it as US currency and return the string.
msg ="State Tax is " + NumberFormat.getCurrencyInstance(locale).format(INDEX * (income - (MIN_INCOME * numDependents)));
return msg;
}
/**
* Implements actionPerformed method of ActionListener interface
*
* @param ActionEvent e
* @return void
*/
publicvoid actionPerformed(ActionEvent e){
resultLabel.setText(computeResult());
}
}
//END of file StateTaxComputationUITeamB.java

