Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-14-2008, 06:14 PM
Member
 
Join Date: May 2008
Posts: 21
crazydeo is on a distinguished road
[SOLVED] Please Help!!
I've been working on this project all night and have been unsuccessful in getting the values to display in output. All I get once I push the calculate button are 0's for each of the values, but the total is summed up. Please point me in the right direction.

Thank you in advance!!!


Code:
/* Project : 8 * Class : CISP21 * Date : 05/14/08 * Programmer : Adeel Mujtaba * Description : An application that computes the total charges for * Order INC. and displays the output in JFrame * */ import javax.swing.*; import java.awt.event.*; import java.text.*; import java.awt.*; public class Presentation extends JFrame implements ActionListener, ItemListener { // All the GUI objects JPanel mainPanel = new JPanel(); JPanel discountPanel = new JPanel(); JLabel storeLabel = new JLabel(" OrderBook INC. "); JTextField bookNameTextField = new JTextField(20); JTextField quantityTextField = new JTextField(20); JRadioButton discountARadioButton = new JRadioButton(" A "); JRadioButton discountBRadioButton = new JRadioButton(" B "); JRadioButton discountCRadioButton = new JRadioButton(" C "); JRadioButton discountInvisibleRadioButton = new JRadioButton(""); ButtonGroup discountButtonGroup = new ButtonGroup(); JTextField priceTextField = new JTextField(20); JButton calculateButton = new JButton(" Calculate "); JTextArea outputTextArea = new JTextArea("Books Ordered", 10, 25); JScrollPane outputScrollPane = new JScrollPane(outputTextArea); //object of the font Font storeFont = new Font("Arial",Font.BOLD,14); public static void main(String[] args) { //create an object of the class and code the close button Presentation myBooks = new Presentation(); myBooks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public Presentation() { designFrame(); add(mainPanel); setTitle("OrderBook INC."); setSize(304,600); setVisible(true); } public void designFrame() { //set the font and the color storeLabel.setFont(storeFont); storeLabel.setForeground(Color.GRAY); //add the components to the mainPanel mainPanel.add(storeLabel); mainPanel.add(new JLabel(" Book Name ")); mainPanel.add(bookNameTextField); mainPanel.add(new JLabel(" Quantity Ordered ")); mainPanel.add(quantityTextField); //adding discount radio buttons to car radio button group mainPanel.add(new JLabel(" Discount Type ")); discountButtonGroup.add(discountARadioButton); discountButtonGroup.add(discountBRadioButton); discountButtonGroup.add(discountCRadioButton); discountButtonGroup.add(discountInvisibleRadioButton); //adding radio buttons to panel mainPanel.add(discountARadioButton); mainPanel.add(discountBRadioButton); mainPanel.add(discountCRadioButton); priceTextField.setEditable(false); calculateButton.setEnabled(false); mainPanel.add(new JLabel(" Price ")); mainPanel.add(priceTextField); mainPanel.add(calculateButton); mainPanel.add(outputScrollPane); //add the listeners priceTextField.addActionListener(this); calculateButton.addActionListener(this); discountARadioButton.addItemListener(this); discountBRadioButton.addItemListener(this); discountCRadioButton.addItemListener(this); //add the panel to the frame add(mainPanel); setSize(300,400); setVisible(true); } //create a listener for the Radio Buttons public void itemStateChanged(ItemEvent evt) { if(discountARadioButton.isSelected()) { enabledComponents(); } else if (discountBRadioButton.isSelected()) { enabledComponents(); } else if (discountCRadioButton.isSelected()) { enabledComponents(); } } public void actionPerformed(ActionEvent evt) { getInput(); clear(); } //declaring local variables public void getInput() { //declaring local variables char discountChar; int quantityInteger; int discountTypeInteger; double priceDouble; double discountDouble; String nameString; // get input try { quantityInteger = Integer.parseInt(quantityTextField.getText()); try { priceDouble = Double.parseDouble(priceTextField.getText()); } catch(NumberFormatException err) { JOptionPane.showMessageDialog(null, "Price is invalid"); priceTextField.selectAll(); priceTextField.requestFocus(); } } catch(NumberFormatException err) { JOptionPane.showMessageDialog(null, "Quantity is invalid"); quantityTextField.selectAll(); quantityTextField.requestFocus(); } if(discountARadioButton.isSelected()) { discountChar = 'a'; discountTypeInteger = getTypeOfDiscount(discountChar); nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); if((discountTypeInteger != -1)) { if (!(nameString.equals(""))) { Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble); discountDouble = myOrder.getDiscount(); displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar); clear(); } else { JOptionPane.showMessageDialog(null, "Please Enter Book Name"); bookNameTextField.selectAll(); bookNameTextField.requestFocus(); } } } else if(discountBRadioButton.isSelected()) { discountChar = 'b'; discountTypeInteger = getTypeOfDiscount(discountChar); nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); if((discountTypeInteger != -1)) { if (!(nameString.equals(""))) { Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble); discountDouble = myOrder.getDiscount(); displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar); clear(); } else { JOptionPane.showMessageDialog(null, "Please Enter Book Name"); bookNameTextField.selectAll(); bookNameTextField.requestFocus(); } } } else if(discountCRadioButton.isSelected()) { discountChar = 'c'; discountTypeInteger = getTypeOfDiscount(discountChar); nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); if((discountTypeInteger != -1)) { if (!(nameString.equals(""))) { Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble); discountDouble = myOrder.getDiscount(); displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar); clear(); } else { JOptionPane.showMessageDialog(null, "Please Enter Book Name"); bookNameTextField.selectAll(); bookNameTextField.requestFocus(); } }}} //set text fields visible once a discount is selected public void enabledComponents() { priceTextField.setEditable(true); calculateButton.setEnabled(true); } public int getTypeOfDiscount(char discountChar) { int discountTypeInteger = 0; { if(discountARadioButton.isSelected()) { discountTypeInteger = 1; } else if(discountBRadioButton.isSelected()) { discountTypeInteger = 2; } else if(discountCRadioButton.isSelected()) { discountTypeInteger = 3; } else if(discountInvisibleRadioButton.isSelected()) { discountTypeInteger = -1; } } return discountTypeInteger; } public void displayOutput(int quantityInteger, double priceDouble, double discountDouble, String nameString, char discountChar) { //object to format to currency DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00"); DecimalFormat formatNumberFormat = new DecimalFormat("0"); Calculation myCalcs = new Calculation(); String outputString; if(discountChar == 'a') { outputString = '\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity()) + '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice()) + '\n' + "Discount Percentage: 20% " + formatDecimalFormat.format(myCalcs.getDiscount()) + '\n' + "Subtotal: " + formatDecimalFormat.format(myCalcs.getSubTotal()) + '\n' + "Shipping: " + formatDecimalFormat.format(myCalcs.getShipping()) + '\n' + "Tax: " + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ "Total: " + formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal()) + '\n'; outputTextArea.append(outputString); } else if(discountChar == 'b') { outputString = '\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity()) + '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice()) + '\n' + "Discount Percentage: 10% " + formatDecimalFormat.format(myCalcs.getDiscount()) + '\n' + "Subtotal: " + formatDecimalFormat.format(myCalcs.getSubTotal()) + '\n' + "Shipping: " + formatDecimalFormat.format(myCalcs.getShipping()) + '\n' + "Tax: " + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ "Total: " + formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal()) + '\n'; outputTextArea.append(outputString); } else if(discountChar == 'c') { outputString = '\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity()) + '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice()) + '\n' + "No Discount: " + formatDecimalFormat.format(myCalcs.getDiscount()) + '\n' + "Subtotal: " + formatDecimalFormat.format(myCalcs.getSubTotal()) + '\n' + "Shipping: " + formatDecimalFormat.format(myCalcs.getShipping()) + '\n' + "Tax: " + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ "Total: " + formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal()) + '\n'; outputTextArea.append(outputString); } } public void clear() { //clear existing text from text fields and request cursor to top bookNameTextField.setText(""); quantityTextField.setText(""); discountInvisibleRadioButton.setSelected(true); priceTextField.setText(""); bookNameTextField.requestFocus(); priceTextField.setEditable(false); calculateButton.setEnabled(false); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-14-2008, 06:58 PM
rjuyal's Avatar
Senior Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
rjuyal is on a distinguished road
Post the code for Calculation class or let me know if this is some api ?
__________________
i am the future
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-14-2008, 07:02 PM
Member
 
Join Date: May 2008
Posts: 21
crazydeo is on a distinguished road
Calculation Class


Code:
public class Calculation { private double priceDouble, subTotalDouble, taxDouble, shippingDouble, totalDouble, discountDouble; private int quantityInteger, discountTypeInteger; private static double grandTotalDouble; private static int totalNumberOfOrdersInteger; private final double DISCOUNT_A = 0.20; private final double DISCOUNT_B = 0.10; private final double DISCOUNT_C = 0; private final double TAX_RATE = 0.0825; private final int SHIPPING_RATE_INTEGER = 1; public Calculation() { } public Calculation(int quantityNewInteger, int discountTypeNewInteger, double priceNewDouble) { setQuantity(quantityNewInteger); setPrice(priceNewDouble); setDiscount(discountTypeNewInteger); calculate(); } private void setQuantity(int quantityNewInteger) { //assign public variable to private quantityInteger = quantityNewInteger; } private void setPrice(double priceNewDouble) { //assign public variable to private priceDouble = priceNewDouble; } private void setDiscount(int discountTypeNewInteger) { //assign public variable to private discountTypeInteger = discountTypeNewInteger; } private void calculate() { switch(discountTypeInteger) { case 1: calculateADiscount(); totalNumberOfOrdersInteger++; break; case 2: calculateBDiscount(); totalNumberOfOrdersInteger++; break; case 3: calculateCDiscount(); totalNumberOfOrdersInteger++; break; } } //calculation of fees private void calculateADiscount() { //calculate the subTotal of the order subTotalDouble = priceDouble * quantityInteger; //calculate the discount of the order discountDouble = subTotalDouble * DISCOUNT_A; //calculate the shipping of the order shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger; //calculate the Tax of the order taxDouble = subTotalDouble * TAX_RATE; //calculate the Total including shipping and tax of the order totalDouble = taxDouble + subTotalDouble + shippingDouble; //total number of orders processed grandTotalDouble += totalDouble; } //calculation of fees private void calculateBDiscount() { //calculate the subTotal of the order subTotalDouble = priceDouble * quantityInteger; //calculate the discount of the order discountDouble = subTotalDouble * DISCOUNT_B; //calculate the shipping of the order shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger; //calculate the Tax of the order taxDouble = subTotalDouble * TAX_RATE; //calculate the Total including shipping and tax of the order totalDouble = taxDouble + subTotalDouble + shippingDouble; //total number of orders processed grandTotalDouble += totalDouble; } //calculation of fees private void calculateCDiscount() { //calculate the subTotal of the order subTotalDouble = priceDouble * quantityInteger; //calculate the discount of the order discountDouble = DISCOUNT_C; //calculate the shipping of the order shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger; //calculate the Tax of the order taxDouble = subTotalDouble * TAX_RATE; //calculate the Total including shipping and tax of the order totalDouble = taxDouble + subTotalDouble + shippingDouble; //total number of orders processed grandTotalDouble += totalDouble; } public double getQuantity() { //returning quantity return quantityInteger; } public double getPrice() { //returning price return priceDouble; } public double getDiscount() { //returning discount return discountDouble; } public double getSubTotal() { //returning subTotal return subTotalDouble; } public double getShipping() { //returning shipping return shippingDouble; } public double getTax() { //returning Tax return taxDouble; } public double getTotal() { //returning grand total of orders return totalDouble; } public double getGrandTotal() { //returning grand total of orders return grandTotalDouble; } public int getNumberOfOrders() { //return total number of orders processed return totalNumberOfOrdersInteger; } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-14-2008, 07:04 PM
Member
 
Join Date: May 2008
Posts: 21
crazydeo is on a distinguished road
Fixed acouple of things in the Presentation Class as well.

Code:
import javax.swing.*; import java.awt.event.*; import java.text.*; import java.awt.*; public class Presentation extends JFrame implements ActionListener, ItemListener { // All the GUI objects JPanel mainPanel = new JPanel(); JLabel storeLabel = new JLabel(" OrderBook INC. "); JTextField bookNameTextField = new JTextField(20); JTextField quantityTextField = new JTextField(20); JRadioButton discountARadioButton = new JRadioButton(" A "); JRadioButton discountBRadioButton = new JRadioButton(" B "); JRadioButton discountCRadioButton = new JRadioButton(" C "); JRadioButton discountInvisibleRadioButton = new JRadioButton(""); ButtonGroup discountButtonGroup = new ButtonGroup(); JTextField priceTextField = new JTextField(20); JButton calculateButton = new JButton(" Calculate "); JTextArea outputTextArea = new JTextArea("Books Ordered", 10, 25); JScrollPane outputScrollPane = new JScrollPane(outputTextArea); //object of the font Font storeFont = new Font("Arial",Font.BOLD,14); public static void main(String[] args) { //create an object of the class and code the close button Presentation myBooks = new Presentation(); myBooks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public Presentation() { designFrame(); add(mainPanel); setTitle("OrderBook INC."); setSize(304,600); setVisible(true); } public void designFrame() { //set the font and the color storeLabel.setFont(storeFont); storeLabel.setForeground(Color.GRAY); //add the components to the mainPanel mainPanel.add(storeLabel); mainPanel.add(new JLabel(" Book Name ")); mainPanel.add(bookNameTextField); mainPanel.add(new JLabel(" Quantity Ordered ")); mainPanel.add(quantityTextField); //adding discount radio buttons to car radio button group mainPanel.add(new JLabel(" Discount Type ")); discountButtonGroup.add(discountARadioButton); discountButtonGroup.add(discountBRadioButton); discountButtonGroup.add(discountCRadioButton); discountButtonGroup.add(discountInvisibleRadioButton); //adding radio buttons to panel mainPanel.add(discountARadioButton); mainPanel.add(discountBRadioButton); mainPanel.add(discountCRadioButton); priceTextField.setEditable(false); calculateButton.setEnabled(false); mainPanel.add(new JLabel(" Price ")); mainPanel.add(priceTextField); mainPanel.add(calculateButton); mainPanel.add(outputScrollPane); //add the listeners priceTextField.addActionListener(this); calculateButton.addActionListener(this); discountARadioButton.addItemListener(this); discountBRadioButton.addItemListener(this); discountCRadioButton.addItemListener(this); //add the panel to the frame add(mainPanel); setSize(300,400); setVisible(true); } //create a listener for the Radio Buttons public void itemStateChanged(ItemEvent evt) { if(discountARadioButton.isSelected()) { enabledComponents(); } else if (discountBRadioButton.isSelected()) { enabledComponents(); } else if (discountCRadioButton.isSelected()) { enabledComponents(); } } public void actionPerformed(ActionEvent evt) { getInput(); clear(); } //declaring local variables public void getInput() { //declaring local variables char discountChar; int quantityInteger; int discountTypeInteger; double priceDouble; double discountDouble; String nameString; // get input try { quantityInteger = Integer.parseInt(quantityTextField.getText()); try { priceDouble = Double.parseDouble(priceTextField.getText()); } catch(NumberFormatException err) { JOptionPane.showMessageDialog(null, "Price is invalid"); priceTextField.selectAll(); priceTextField.requestFocus(); } } catch(NumberFormatException err) { JOptionPane.showMessageDialog(null, "Quantity is invalid"); quantityTextField.selectAll(); quantityTextField.requestFocus(); } if(discountARadioButton.isSelected()) { discountChar = 'a'; discountTypeInteger = getTypeOfDiscount(discountChar); nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); if((discountTypeInteger != -1)) { if (!(nameString.equals(""))) { Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble); discountDouble = myOrder.getDiscount(); displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar); clear(); } else { JOptionPane.showMessageDialog(null, "Please Enter Book Name"); bookNameTextField.selectAll(); bookNameTextField.requestFocus(); } } } else if(discountBRadioButton.isSelected()) { discountChar = 'b'; discountTypeInteger = getTypeOfDiscount(discountChar); nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); if((discountTypeInteger != -1)) { if (!(nameString.equals(""))) { Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble); discountDouble = myOrder.getDiscount(); displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar); clear(); } else { JOptionPane.showMessageDialog(null, "Please Enter Book Name"); bookNameTextField.selectAll(); bookNameTextField.requestFocus(); } } } else if(discountCRadioButton.isSelected()) { discountChar = 'c'; discountTypeInteger = getTypeOfDiscount(discountChar); nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); if((discountTypeInteger != -1)) { if (!(nameString.equals(""))) { Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble); discountDouble = myOrder.getDiscount(); displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar); clear(); } else { JOptionPane.showMessageDialog(null, "Please Enter Book Name"); bookNameTextField.selectAll(); bookNameTextField.requestFocus(); } } } } //set text fields visible once a discount is selected public void enabledComponents() { priceTextField.setEditable(true); calculateButton.setEnabled(true); } public int getTypeOfDiscount(char discountChar) { int discountTypeInteger = 0; { if(discountARadioButton.isSelected()) { discountTypeInteger = 1; } else if(discountBRadioButton.isSelected()) { discountTypeInteger = 2; } else if(discountCRadioButton.isSelected()) { discountTypeInteger = 3; } else if(discountInvisibleRadioButton.isSelected()) { discountTypeInteger = -1; } } return discountTypeInteger; } public void displayOutput(int quantityInteger, double priceDouble, double discountDouble, String nameString, char discountChar) { //object to format to currency DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00"); DecimalFormat formatNumberFormat = new DecimalFormat("0"); Calculation myCalcs = new Calculation(); String outputString; if(discountChar == 'a') { outputString = '\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity()) + '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice()) + '\n' + "Discount Percentage: 20% " + formatDecimalFormat.format(myCalcs.getDiscount()) + '\n' + "Subtotal: " + formatDecimalFormat.format(myCalcs.getSubTotal()) + '\n' + "Shipping: " + formatDecimalFormat.format(myCalcs.getShipping()) + '\n' + "Tax: " + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ "Total: " + formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal()) + '\n'; outputTextArea.append(outputString); } else if(discountChar == 'b') { outputString = '\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity()) + '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice()) + '\n' + "Discount Percentage: 10% " + formatDecimalFormat.format(myCalcs.getDiscount()) + '\n' + "Subtotal: " + formatDecimalFormat.format(myCalcs.getSubTotal()) + '\n' + "Shipping: " + formatDecimalFormat.format(myCalcs.getShipping()) + '\n' + "Tax: " + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ "Total: " + formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal()) + '\n'; outputTextArea.append(outputString); } else if(discountChar == 'c') { outputString = '\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity()) + '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice()) + '\n' + "No Discount: " + formatDecimalFormat.format(myCalcs.getDiscount()) + '\n' + "Subtotal: " + formatDecimalFormat.format(myCalcs.getSubTotal()) + '\n' + "Shipping: " + formatDecimalFormat.format(myCalcs.getShipping()) + '\n' + "Tax: " + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ "Total: " + formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal()) + '\n'; outputTextArea.append(outputString); } } public void clear() { //clear existing text from text fields and request cursor to top bookNameTextField.setText(""); quantityTextField.setText(""); discountInvisibleRadioButton.setSelected(true); priceTextField.setText(""); bookNameTextField.requestFocus(); priceTextField.setEditable(false); calculateButton.setEnabled(false); } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-14-2008, 08:23 PM
rjuyal's Avatar
Senior Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
rjuyal is on a distinguished road
Code:
package com.faaltu.PriceCalculation; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.text.DecimalFormat; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class Presentation extends JFrame implements ActionListener, ItemListener { // All the GUI objects JPanel mainPanel = new JPanel(); JLabel storeLabel = new JLabel(" OrderBook INC. "); JTextField bookNameTextField = new JTextField(20); JTextField quantityTextField = new JTextField(20); JRadioButton discountARadioButton = new JRadioButton(" A "); JRadioButton discountBRadioButton = new JRadioButton(" B "); JRadioButton discountCRadioButton = new JRadioButton(" C "); JRadioButton discountInvisibleRadioButton = new JRadioButton(""); ButtonGroup discountButtonGroup = new ButtonGroup(); JTextField priceTextField = new JTextField(20); JButton calculateButton = new JButton(" Calculate "); JTextArea outputTextArea = new JTextArea("Books Ordered", 10, 25); JScrollPane outputScrollPane = new JScrollPane(outputTextArea); //object of the font Font storeFont = new Font("Arial",Font.BOLD,14); public static void main(String[] args) { //create an object of the class and code the close button Presentation myBooks = new Presentation(); myBooks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public Presentation() { designFrame(); add(mainPanel); setTitle("OrderBook INC."); setSize(304,600); setVisible(true); } public void designFrame() { //set the font and the color storeLabel.setFont(storeFont); storeLabel.setForeground(Color.GRAY); //add the components to the mainPanel mainPanel.add(storeLabel); mainPanel.add(new JLabel(" Book Name ")); mainPanel.add(bookNameTextField); mainPanel.add(new JLabel(" Quantity Ordered ")); mainPanel.add(quantityTextField); //adding discount radio buttons to car radio button group mainPanel.add(new JLabel(" Discount Type ")); discountButtonGroup.add(discountARadioButton); discountButtonGroup.add(discountBRadioButton); discountButtonGroup.add(discountCRadioButton); discountButtonGroup.add(discountInvisibleRadioButton); //adding radio buttons to panel mainPanel.add(discountARadioButton); mainPanel.add(discountBRadioButton); mainPanel.add(discountCRadioButton); priceTextField.setEditable(false); calculateButton.setEnabled(false); mainPanel.add(new JLabel(" Price ")); mainPanel.add(priceTextField); mainPanel.add(calculateButton); mainPanel.add(outputScrollPane); //add the listeners priceTextField.addActionListener(this); calculateButton.addActionListener(this); discountARadioButton.addItemListener(this); discountBRadioButton.addItemListener(this); discountCRadioButton.addItemListener(this); //add the panel to the frame add(mainPanel); setSize(300,400); setVisible(true); } //create a listener for the Radio Buttons public void itemStateChanged(ItemEvent evt) { if(discountARadioButton.isSelected()) { enabledComponents(); } else if (discountBRadioButton.isSelected()) { enabledComponents(); } else if (discountCRadioButton.isSelected()) { enabledComponents(); } } public void actionPerformed(ActionEvent evt) { getInput(); clear(); } //declaring local variables public void getInput() { //declaring local variables char discountChar; int quantityInteger; int discountTypeInteger; double priceDouble; double discountDouble; String nameString; // get input try { quantityInteger = Integer.parseInt(quantityTextField.getText()); try { priceDouble = Double.parseDouble(priceTextField.getText()); } catch(NumberFormatException err) { JOptionPane.showMessageDialog(null, "Price is invalid"); priceTextField.selectAll(); priceTextField.requestFocus(); } } catch(NumberFormatException err) { JOptionPane.showMessageDialog(null, "Quantity is invalid"); quantityTextField.selectAll(); quantityTextField.requestFocus(); } if(discountARadioButton.isSelected()) { discountChar = 'a'; discountTypeInteger = getTypeOfDiscount(discountChar); nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); if((discountTypeInteger != -1)) { if (!(nameString.equals(""))) { Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble); discountDouble = myOrder.getDiscount(); displayOutput(myOrder, quantityInteger, priceDouble, discountDouble, nameString, discountChar); clear(); } else { JOptionPane.showMessageDialog(null, "Please Enter Book Name"); bookNameTextField.selectAll(); bookNameTextField.requestFocus(); } } } else if(discountBRadioButton.isSelected()) { discountChar = 'b'; discountTypeInteger = getTypeOfDiscount(discountChar); nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); if((discountTypeInteger != -1)) { if (!(nameString.equals(""))) { Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble); discountDouble = myOrder.getDiscount(); displayOutput(myOrder, quantityInteger, priceDouble, discountDouble, nameString, discountChar); clear(); } else { JOptionPane.showMessageDialog(null, "Please Enter Book Name"); bookNameTextField.selectAll(); bookNameTextField.requestFocus(); } } } else if(discountCRadioButton.isSelected()) { discountChar = 'c'; discountTypeInteger = getTypeOfDiscount(discountChar); nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); if((discountTypeInteger != -1)) { if (!(nameString.equals(""))) { Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble); discountDouble = myOrder.getDiscount(); displayOutput(myOrder, quantityInteger, priceDouble, discountDouble, nameString, discountChar); clear(); } else { JOptionPane.showMessageDialog(null, "Please Enter Book Name"); bookNameTextField.selectAll(); bookNameTextField.requestFocus(); } } } } //set text fields visible once a discount is selected public void enabledComponents() { priceTextField.setEditable(true); calculateButton.setEnabled(true); } public int getTypeOfDiscount(char discountChar) { int discountTypeInteger = 0; { if(discountARadioButton.isSelected()) { discountTypeInteger = 1; } else if(discountBRadioButton.isSelected()) { discountTypeInteger = 2; } else if(discountCRadioButton.isSelected()) { discountTypeInteger = 3; } else if(discountInvisibleRadioButton.isSelected()) { discountTypeInteger = -1; } } return discountTypeInteger; } public void displayOutput( Calculation myCalcs, int quantityInteger, double priceDouble, double discountDouble, String nameString, char discountChar) { //object to format to currency DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00"); DecimalFormat formatNumberFormat = new DecimalFormat("0"); String outputString; if(discountChar == 'a') { outputString = '\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity()) + '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice()) + '\n' + "Discount Percentage: 20% " + formatDecimalFormat.format(myCalcs.getDiscount()) + '\n' + "Subtotal: " + formatDecimalFormat.format(myCalcs.getSubTotal()) + '\n' + "Shipping: " + formatDecimalFormat.format(myCalcs.getShipping()) + '\n' + "Tax: " + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ "Total: " + formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal()) + '\n'; outputTextArea.append(outputString); } else if(discountChar == 'b') { outputString = '\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity()) + '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice()) + '\n' + "Discount Percentage: 10% " + formatDecimalFormat.format(myCalcs.getDiscount()) + '\n' + "Subtotal: " + formatDecimalFormat.format(myCalcs.getSubTotal()) + '\n' + "Shipping: " + formatDecimalFormat.format(myCalcs.getShipping()) + '\n' + "Tax: " + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ "Total: " + formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal()) + '\n'; outputTextArea.append(outputString); } else if(discountChar == 'c') { outputString = '\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity()) + '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice()) + '\n' + "No Discount: " + formatDecimalFormat.format(myCalcs.getDiscount()) + '\n' + "Subtotal: " + formatDecimalFormat.format(myCalcs.getSubTotal()) + '\n' + "Shipping: " + formatDecimalFormat.format(myCalcs.getShipping()) + '\n' + "Tax: " + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ "Total: " + formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal()) + '\n'; outputTextArea.append(outputString); } } public void clear() { //clear existing text from text fields and request cursor to top bookNameTextField.setText(""); quantityTextField.setText(""); discountInvisibleRadioButton.setSelected(true); priceTextField.setText(""); bookNameTextField.requestFocus(); priceTextField.setEditable(false); calculateButton.setEnabled(false); } }


Code:
package com.faaltu.PriceCalculation; public class Calculation { private double priceDouble, subTotalDouble, taxDouble, shippingDouble, totalDouble, discountDouble; private int quantityInteger, discountTypeInteger; private static double grandTotalDouble; private static int totalNumberOfOrdersInteger; private final double DISCOUNT_A = 0.20; private final double DISCOUNT_B = 0.10; private final double DISCOUNT_C = 0; private final double TAX_RATE = 0.0825; private final int SHIPPING_RATE_INTEGER = 1; public Calculation() { } public Calculation(int quantityNewInteger, int discountTypeNewInteger, double priceNewDouble) { setQuantity(quantityNewInteger); setPrice(priceNewDouble); setDiscount(discountTypeNewInteger); calculate(); } public void setQuantity(int quantityNewInteger) { //assign public variable to private quantityInteger = quantityNewInteger; } public void setPrice(double priceNewDouble) { //assign public variable to private priceDouble = priceNewDouble; } public void setDiscount(int discountTypeNewInteger) { //assign public variable to private discountTypeInteger = discountTypeNewInteger; } public void calculate() { switch(discountTypeInteger) { case 1: calculateADiscount(); totalNumberOfOrdersInteger++; break; case 2: calculateBDiscount(); totalNumberOfOrdersInteger++; break; case 3: calculateCDiscount(); totalNumberOfOrdersInteger++; break; } } //calculation of fees public void calculateADiscount() { //calculate the subTotal of the order subTotalDouble = priceDouble * quantityInteger; //calculate the discount of the order discountDouble = subTotalDouble * DISCOUNT_A; //calculate the shipping of the order shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger; //calculate the Tax of the order taxDouble = subTotalDouble * TAX_RATE; //calculate the Total including shipping and tax of the order totalDouble = taxDouble + subTotalDouble + shippingDouble; //total number of orders processed grandTotalDouble += totalDouble; } //calculation of fees public void calculateBDiscount() { //calculate the subTotal of the order subTotalDouble = priceDouble * quantityInteger; //calculate the discount of the order discountDouble = subTotalDouble * DISCOUNT_B; //calculate the shipping of the order shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger; //calculate the Tax of the order taxDouble = subTotalDouble * TAX_RATE; //calculate the Total including shipping and tax of the order totalDouble = taxDouble + subTotalDouble + shippingDouble; //total number of orders processed grandTotalDouble += totalDouble; } //calculation of fees public void calculateCDiscount() { //calculate the subTotal of the order subTotalDouble = priceDouble * quantityInteger; //calculate the discount of the order discountDouble = DISCOUNT_C; //calculate the shipping of the order shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger; //calculate the Tax of the order taxDouble = subTotalDouble * TAX_RATE; //calculate the Total including shipping and tax of the order totalDouble = taxDouble + subTotalDouble + shippingDouble; //total number of orders processed grandTotalDouble += totalDouble; } public double getQuantity() { //returning quantity return quantityInteger; } public double getPrice() { //returning price return priceDouble; } public double getDiscount() { //returning discount return discountDouble; } public double getSubTotal() { //returning subTotal return subTotalDouble; } public double getShipping() { //returning shipping return shippingDouble; } public double getTax() { //returning Tax return taxDouble; } public double getTotal() { //returning grand total of orders return totalDouble; } public double getGrandTotal() { //returning grand total of orders return grandTotalDouble; } public int getNumberOfOrders() { //return total number of orders processed return totalNumberOfOrdersInteger; } }

Now it is working fine,
but my friend whichever practice you are following is not fine
__________________
i am the future
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 01:01 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org