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
Calculate Tax
Collapse
X
-
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. -
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.Numbe rFormatExceptio n;
public class Order1
{
public static void main(String[] args)
{
MyOrder1 myFrame = new MyOrder1() ;
myFrame.setVisi ble(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.se tText("0") ;
PorkField.setTe xt("0") ;
CattleField.set Text("0") ;
TotalField.setT ext("") ;
Total = new JButton("Total" ) ;
Total.addAction Listener(this) ;
Reset = new JButton("Reset" ) ;
Reset.addAction Listener(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.set Layout(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) ;
setDefaultClose Operation(JFram e.EXIT_ON_CLOSE );
setBounds(X_ORI GIN, 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.se tText("0") ;
PorkField.setTe xt("0") ;
CattleField.set Text("0") ;
TotalField.setT ext("0") ;
}
else if (source == Total)
{
i = 0;
try
{
chicken = ChickenField.ge tText();
ayam = Float.valueOf(c hicken).floatVa lue();
}
catch (Exception d)
{
JOptionPane.sho wMessageDialog( null, "Quantity of Chicken is not valid...", "Error",
JOptionPane.ERR OR_MESSAGE);
i=1;
}
try
{
pork = PorkField.getTe xt();
babi = Float.valueOf(p ork).floatValue ();
}
catch (Exception d)
{
JOptionPane.sho wMessageDialog( null, "Quantity of Pork is not valid...", "Error",
JOptionPane.ERR OR_MESSAGE);
i=1;
}
try
{
cattle = CattleField.get Text();
sapi = Float.valueOf(c attle).floatVal ue();
}
catch (Exception d)
{
JOptionPane.sho wMessageDialog( null, "Quantity of Cattle is not valid...", "Error",
JOptionPane.ERR OR_MESSAGE);
i=1;
}
if (i==0)
{
float total = ayam * 1.19f + babi * 2.17f + sapi * 3.27f ;
String tot= String.valueOf( total);
TotalField.setT ext(tot) ;
double tax = 0;
tax = (total * 0.08);
String setTax = "" + Tax;
JLabel tax.setText("se tTax") ;
total = total + tax;Comment
-
Originally posted by bputleycan someone please help
Is this what you want
Code:/* * 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; JLabel 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 ") ; 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) { double total = ayam * 1.19 + babi * 2.17 + sapi * 3.27 ; double taxAmount = 0.0; taxAmount = (total * Double.parseDouble(TaxField.getText())); String setTax = "" + taxAmount; tax.setText("Tax:"+setTax) ; total = total + taxAmount; String tot= String.valueOf(total); TotalField.setText(tot); } } } }
Comment
Comment