By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,782 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

Calculate Tax

P: 10
does anyone know where there is an example of how to write code for tax added on to the subtotal to equal price? I looked in the tutorials but didnt see anything like it, and I dont just want people to do my work for me because I want to learn how to write code myself. I am a beginner just to let you know
Dec 22 '06 #1
Share this Question
Share on Google+
6 Replies


Ganon11
Expert 2.5K+
P: 3,652
If you have the tax rate (a.k.a. 5%) and the subtotal (e.g. $10.00), you can multiply the subtotal by the percentage to determine the amount of tax to add - then add that amount to the subtotal to find the total.

The tax rate will either be held as a decimal (a.k.a. 5% would be 0.05) or as an integer (a.k.a. 5% would be 5), and your calculations will have to differ accordingly - if you hold it as a decimal, you can multiply straight away, but if you hold it as an integer, you will have to divide by 100.0 before multiplying.
Dec 22 '06 #2

P: 10
could someone please help, I have been working on this the last couple days trying to figure out why this wont accept the Tax code I put in there,

/*
* Order1.java
*
* Created on December 18, 2006, 6:16 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author test
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.lang.NumberFormatException;

public class Order1
{
public static void main(String[] args)
{
MyOrder1 myFrame = new MyOrder1() ;

myFrame.setVisible(true) ;
}
}

class MyOrder1 extends JFrame implements ActionListener
{
private static final int WIDTH = 200 ;
private static final int HEIGHT = 200 ;
private static final int X_ORIGIN = 400 ;
private static final int Y_ORIGIN = 200 ;

// create new buttons and label them
private JButton Total ;
private JButton Reset ;
private JTextField ChickenField ;
private JTextField PorkField ;
private JTextField CattleField ;
private JTextField TotalField ;
private JTextField TaxField;

private Component Tax;



//creates new label icons for my items
public MyOrder1()
{


String Response = new String() ;
JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
JLabel Pork = new JLabel (" Pork: $2.17 LB ") ;
JLabel Cattle = new JLabel(" Cattle: $3.27 LB ") ;
JLabel tax = new JLabel(" Tax: ") ;
JLabel total = new JLabel(" Total : ");
ChickenField = new JTextField(5) ;
PorkField = new JTextField(5) ;
CattleField = new JTextField(5) ;
TaxField = new JTextField(5) ;
TotalField = new JTextField(5) ;
ChickenField.setText("0") ;
PorkField.setText("0") ;
CattleField.setText("0") ;
TotalField.setText("") ;

Total = new JButton("Total") ;
Total.addActionListener(this) ;

Reset = new JButton("Reset") ;
Reset.addActionListener(this) ;




// get the content pane
Container contentPane = getContentPane() ;

// create a new layout manager for the pane
FlowLayout aLayout = new FlowLayout() ;

// assign the new layout manager to the content pane
contentPane.setLayout(aLayout) ;

// add the "Exit" button to the content pane
contentPane.add(Chicken) ;
contentPane.add(ChickenField) ;
contentPane.add(Pork) ;
contentPane.add(PorkField) ;
contentPane.add(Cattle) ;
contentPane.add(CattleField) ;
contentPane.add(Tax) ;
contentPane.add(TaxField) ;
contentPane.add(total) ;
contentPane.add(TotalField) ;
contentPane.add(Total) ;
contentPane.add(Reset) ;

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
}

public void actionPerformed(ActionEvent e)
{
String chicken, pork, cattle;
float ayam=0, babi=0, sapi=0;
int i;

Object source = e.getSource() ;

if (source == Reset)
{
ChickenField.setText("0") ;
PorkField.setText("0") ;
CattleField.setText("0") ;
TotalField.setText("0") ;
}
else if (source == Total)
{
i = 0;
try
{
chicken = ChickenField.getText();
ayam = Float.valueOf(chicken).floatValue();
}
catch (Exception d)
{
JOptionPane.showMessageDialog(null, "Quantity of Chicken is not valid...", "Error",
JOptionPane.ERROR_MESSAGE);
i=1;
}
try
{
pork = PorkField.getText();
babi = Float.valueOf(pork).floatValue();
}
catch (Exception d)
{
JOptionPane.showMessageDialog(null, "Quantity of Pork is not valid...", "Error",
JOptionPane.ERROR_MESSAGE);
i=1;
}
try
{
cattle = CattleField.getText();
sapi = Float.valueOf(cattle).floatValue();
}
catch (Exception d)
{
JOptionPane.showMessageDialog(null, "Quantity of Cattle is not valid...", "Error",
JOptionPane.ERROR_MESSAGE);
i=1;
}
if (i==0)
{
float total = ayam * 1.19f + babi * 2.17f + sapi * 3.27f ;
String tot= String.valueOf(total);
TotalField.setText(tot) ;


double tax = 0;
tax = (total * 0.08);
String setTax = "" + Tax;
JLabel tax.setText("setTax") ;

total = total + tax;
Dec 24 '06 #3

P: 10
can someone please help
Dec 24 '06 #4

10K+
P: 13,264
can someone please help
Sorry most people had been away.

Is this what you want


Expand|Select|Wrap|Line Numbers
  1. /*
  2. * Order1.java
  3. *
  4. * Created on December 18, 2006, 6:16 PM
  5. *
  6. * To change this template, choose Tools | Template Manager
  7. * and open the template in the editor.
  8. */
  9.  
  10. /**
  11. *
  12. * @author test
  13. */
  14. import java.awt.*;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import javax.swing.*;
  18. import java.lang.NumberFormatException;
  19.  
  20. public class Order1 {
  21.     public static void main(String[] args) {
  22.         MyOrder1 myFrame = new MyOrder1() ;
  23.         myFrame.setVisible(true) ;
  24.     }
  25.  
  26. }
  27.  
  28. class MyOrder1 extends JFrame implements ActionListener {
  29.     private static final int WIDTH = 200 ;
  30.     private static final int HEIGHT = 200 ;
  31.     private static final int X_ORIGIN = 400 ;
  32.     private static final int Y_ORIGIN = 200 ;
  33.  
  34.     // create new buttons and label them
  35.     private JButton Total ;
  36.     private JButton Reset ;
  37.     private JTextField ChickenField ;
  38.     private JTextField PorkField ;
  39.     private JTextField CattleField ;
  40.     private JTextField TotalField ;
  41.     private JTextField TaxField;
  42.  
  43.     private Component Tax;
  44.     JLabel tax;
  45.     //creates new label icons for my items
  46.     public MyOrder1() {
  47.         String Response = new String() ;
  48.         JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
  49.         JLabel Pork = new JLabel (" Pork: $2.17 LB ") ;
  50.         JLabel Cattle = new JLabel(" Cattle: $3.27 LB ") ;
  51.         tax = new JLabel(" Tax: ") ;
  52.         JLabel total = new JLabel(" Total : ");
  53.         ChickenField = new JTextField(5) ;
  54.         PorkField = new JTextField(5) ;
  55.         CattleField = new JTextField(5) ;
  56.         TaxField = new JTextField(5) ;
  57.         TotalField = new JTextField(5) ;
  58.         ChickenField.setText("0") ;
  59.         PorkField.setText("0") ;
  60.         CattleField.setText("0") ;
  61.         TotalField.setText("") ;
  62.  
  63.         Total = new JButton("Total") ;
  64.         Total.addActionListener(this) ;
  65.  
  66.         Reset = new JButton("Reset") ;
  67.         Reset.addActionListener(this) ;
  68.  
  69.  
  70.  
  71.  
  72.         // get the content pane
  73.         Container contentPane = getContentPane() ;
  74.  
  75.         // create a new layout manager for the pane
  76.         FlowLayout aLayout = new FlowLayout() ;
  77.  
  78.         // assign the new layout manager to the content pane
  79.         contentPane.setLayout(aLayout) ;
  80.  
  81.         // add the "Exit" button to the content pane
  82.         contentPane.add(Chicken) ;
  83.         contentPane.add(ChickenField) ;
  84.         contentPane.add(Pork) ;
  85.         contentPane.add(PorkField) ;
  86.         contentPane.add(Cattle) ;
  87.         contentPane.add(CattleField) ;
  88.         contentPane.add(tax) ;
  89.         contentPane.add(TaxField) ;
  90.         contentPane.add(total) ;
  91.         contentPane.add(TotalField) ;
  92.         contentPane.add(Total) ;
  93.         contentPane.add(Reset) ;
  94.  
  95.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  96.         setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
  97.     }
  98.     public void actionPerformed(ActionEvent e) {
  99.         String chicken, pork, cattle;
  100.         float ayam=0, babi=0, sapi=0;
  101.         int i;
  102.  
  103.         Object source = e.getSource() ;
  104.  
  105.         if (source == Reset) {
  106.             ChickenField.setText("0") ;
  107.             PorkField.setText("0") ;
  108.             CattleField.setText("0") ;
  109.             TotalField.setText("0") ;
  110.         }
  111.         else if (source == Total) {
  112.             i = 0;
  113.             try {
  114.                 chicken = ChickenField.getText();
  115.                 ayam = Float.valueOf(chicken).floatValue();
  116.             }
  117.             catch (Exception d) {
  118.                 JOptionPane.showMessageDialog(null, "Quantity of Chicken is not valid...", "Error",
  119.                 JOptionPane.ERROR_MESSAGE);
  120.                 i=1;
  121.             }
  122.             try {
  123.                 pork = PorkField.getText();
  124.                 babi = Float.valueOf(pork).floatValue();
  125.             }
  126.             catch (Exception d) {
  127.                 JOptionPane.showMessageDialog(null, "Quantity of Pork is not valid...", "Error",
  128.                 JOptionPane.ERROR_MESSAGE);
  129.                 i=1;
  130.             }
  131.             try    {
  132.                 cattle = CattleField.getText();
  133.                 sapi = Float.valueOf(cattle).floatValue();
  134.             }
  135.             catch (Exception d)    {
  136.                 JOptionPane.showMessageDialog(null, "Quantity of Cattle is not valid...", "Error",
  137.                 JOptionPane.ERROR_MESSAGE);
  138.                 i=1;
  139.             }
  140.             if (i==0) {
  141.                 double total = ayam * 1.19 + babi * 2.17 + sapi * 3.27 ;
  142.                 double taxAmount = 0.0;
  143.                 taxAmount = (total * Double.parseDouble(TaxField.getText()));
  144.                 String setTax = "" + taxAmount;
  145.                 tax.setText("Tax:"+setTax) ;
  146.                 total = total + taxAmount;
  147.                 String tot= String.valueOf(total);
  148.                 TotalField.setText(tot);
  149.             }
  150.         }
  151.     }
  152. }
Dec 26 '06 #5

P: 10
thanks that was what I was looking for
Dec 26 '06 #6

10K+
P: 13,264
thanks that was what I was looking for
Welcome. Hope to see you around asking or helping others as well.
Dec 27 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.