473,503 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculate Tax

10 New Member
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
6 3007
Ganon11
3,652 Recognized Expert Specialist
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
bputley
10 New Member
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
bputley
10 New Member
can someone please help
Dec 24 '06 #4
r035198x
13,262 MVP
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
bputley
10 New Member
thanks that was what I was looking for
Dec 26 '06 #6
r035198x
13,262 MVP
thanks that was what I was looking for
Welcome. Hope to see you around asking or helping others as well.
Dec 27 '06 #7

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

Similar topics

2
3994
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
1
9971
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
2
324
by: Gálos Zsuzsa | last post by:
Hi all, I need to calculate Formulas. For example: dim strFormula as string="25*(12-6)" I ned a DotNet Function to calculate this Formula (25*(12-6)=150): dim dblValue as double = 150
1
2680
by: Robert de Ridder | last post by:
I use an asp page to display pages, where the dynamic content is in an iframe. To calculate the height of the iframe I use the calcheight function. However when trying to do this with external...
4
9318
by: Jan Szymczuk | last post by:
I'm creating an MS Access 2000 database where I have a number of people entered using simple basic fields, Surname: SMITH Forenames: John DoB: 09/09/1958 Age:...
1
1965
by: Ladislau S. | last post by:
Dear reader, I am an occasional user of MS Access 2000 running on Windows 98. My hobby is ship model building so I made a database for things that I want to buy. After two strokes I bin unable...
6
31768
by: charliewest | last post by:
Can someone pls point me to or recommend the easiest way to calculate someone´s age using the TimeSpan object, in .NET CF? Isn´t there a simple way to use the TimeSpan object to calculate the...
3
5942
by: Libber39 | last post by:
Hi everyone, Have a query on how to calculate the amount of weeks and days contained in a number in an access query. ie: the difference in days between 2 dates amounts to 17 days. I want to now...
0
4623
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
6
4081
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.