(courtesy of Yvonne Cheng)
This Java program calculates Goods and Services Tax (GST at 7%), Provincial Sales Tax (PST 7.5% on the subtotal), and the total amount. Two cases are implemented: GST only or GST+PST. Implementation details:
import java.awt.*;
import java.awt.event.*;
class ChoiceExample extends Frame implements ActionListener
{
Button calculateButton = new Button("Calculate");
Button cancelButton = new Button("Cancel");
Choice prefix = new Choice();
TextField PriceText = new TextField(9);
TextField taxText = new TextField(9);
TextField totalText = new TextField(9);
//constructor
public ChoiceExample()
{
super("Calculate Your Taxes");
setLayout(new GridLayout(7,2));
//Add choices to the choice component
prefix.add("GST");
prefix.add("GST AND PST");
//Put components on frame
add(new Label("Choose your tax(es): "));
add(prefix);
add(new Label("Amount "));
add(PriceText);
add(new Label(" "));
add(new Label(" "));
add(new Label("Tax Amount: "));
add(taxText);
add(new Label("Total Amount: "));
add(totalText);
add(new Label(" "));
add(new Label(" "));
add(calculateButton);
add(cancelButton);
calculateButton.addActionListener(this);
cancelButton.addActionListener(this);
//set the size for the frame and display it.
setSize(300, 400);
pack();
show();
}
public double Round(double A){
return ((int)(A*100+0.5))/100.00;
}
public void actionPerformed(ActionEvent event)
{
String Price = PriceText.getText();
double amount = Double.parseDouble(Price);
double fedrate = 0.07;
double provrate = 0.075;
double GST = amount * fedrate;
double PST = provrate * (amount + GST);
double GSTPST = GST + PST;
if(prefix.getSelectedItem().equals("GST"))
{
double totalamount = amount + GST;
taxText.setText("" + Round(GST));
totalText.setText("" + Round(totalamount));
}
else if(prefix.getSelectedItem().equals("GST AND PST"))
{
double totalamount = amount + GSTPST;
taxText.setText("" + Round(GSTPST));
totalText.setText("" + Round(totalamount));
}
}
}
public class tax1
{
public static void main(String[] args)
{
new ChoiceExample();
}
}