The state sales tax rate is 4% and the county sales tax rate is 2%. Create a GUI application that allows the user to enter the total sales for the month into a text field. from this figure, the application should calculate and display the following:
- the amount of county sales tax.
- the amount of state sales tax.
- the total sales tax(county plus state).
In the applications, code represent the county tax rate (0.02) and the state tax rate by (0.04) as named constants.
Here is my code:
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class SalesTax extends JFrame
{
private JPanel panel;
private JTextField totalSales;
private JButton calcButton;
private final double COUNTRY_RATE = 0.02;
private final double STATE_RATE = 0.04;
private final int WINDOW_WIDTH = 360;
private final int WINDOW_HEIGHT = 100;
public SalesTax()
{
super("Monthly Sales Tax Reporter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setVisible(true);
}
private void buildPanel()
{
totalSalesMsg = new JLabel("Enter the total sales:");
totalSales = new JTextField(10);
calcButton = new JButton("Calculate Sales Tax");
calcButton.addActionListener(new CalcButtonListener());
panel = new JPanel();
panel.add(totalSalesMsg);
panel.add(totalSales);
panel.add(calcButton);
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double totalSalesAmount,
countyTaxAmount,
stateTaxAmount,
totalTaxAmount;
DecimalFormat dollar = new DecimalFormat("#,##0.00");
totalSalesAmount = Double.ParseDouble(totalSales.getText());
countyTaxAmount = totalSalesAmount * COUNTRY_RATE;
stateTaxAmount = totalSalesAmount * STATE_RATE;
totalSalesAmount = countyTaxAmount + stateTaxAmount;
JOptionPane.showMessageDialog(null, "County Sales Tax: $"
+ dollar.format(countyTaxAmount)
+ "\nState Sales Tax: $"
+ dollar.format(stateTaxAmount)
+ "\nTotal Sales Tax:"
+ dollar.format(totalTaxAmount));
}
}
public static void main(String[] args)
{
SalesTax stw = new SalesTax();
}
}
there is only 3 errors but I don't understand the problem with them.
Aucun commentaire:
Enregistrer un commentaire