View Single Post
  #4 (permalink)  
Old 05-13-2008, 11:50 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
here it is. I didnt change anything in your Calculation Class. I only modify the main class.

Code:
import java.text.DecimalFormat; import javax.swing.*; import java.awt.Color; import java.awt.Font; import java.awt.event.*; public class Main extends JFrame implements ActionListener { private Calculation myOrder = new Calculation(); // All the GUI objects JPanel mainPanel = new JPanel(); JLabel storeLabel = new JLabel(" OrderBook INC. "); JTextField bookNameTextField = new JTextField(20); JTextField quantityTextField = new JTextField(20); JTextField priceTextField = new JTextField(20); JButton calculateButton = new JButton(" Calculate "); JTextArea outputTextArea = new JTextArea("Books Ordered", 10, 25); JScrollPane outputScrollPane = new JScrollPane(outputTextArea); int quantityInteger; double priceDouble; String nameString; //object of the font Font storeFont = new Font("Arial",Font.BOLD,14); public static void main(String[] args) { Main myBooks = new Main(); myBooks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public Main() { designFrame(); } public void designFrame() { //set the font and the color storeLabel.setFont(storeFont); storeLabel.setForeground(Color.GRAY); //add the listeners // priceTextField.addActionListener(this); calculateButton.addActionListener(this); //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); mainPanel.add(new JLabel(" Price ")); mainPanel.add(priceTextField); mainPanel.add(calculateButton); mainPanel.add(outputScrollPane); //add the panel to the frame add(mainPanel); setSize(300,400); setVisible(true); } public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand(); if (cmd.equalsIgnoreCase(" Calculate ")){ nameString = bookNameTextField.getText(); quantityInteger = Integer.parseInt(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); myOrder.Calculation(quantityInteger, priceDouble); displayOutput(); } clear(); } public void displayOutput() { //object to format to currency DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00"); DecimalFormat formatNumberFormat = new DecimalFormat("0"); //variables to store the retrieved values from the calculation class double priceDouble, subTotalDouble, shippingDouble, taxDouble, totalDouble, grandTotalDouble; int totalNumberOfOrdersInteger; priceDouble = myOrder.getPrice(); System.out.println(priceDouble); subTotalDouble = myOrder.getSubTotal(); shippingDouble = myOrder.getShipping(); taxDouble = myOrder.getTax(); totalDouble = myOrder.getTotal(); grandTotalDouble = myOrder.getGrandTotal(); totalNumberOfOrdersInteger = myOrder.getNumberOfOrders(); //display output outputTextArea.append('\n' + "Name of the Book: " + nameString + '\n' + "Quantity Ordered: " + formatNumberFormat.format(quantityInteger) + '\n' + "Price: " + formatDecimalFormat.format(priceDouble)+ '\n' + '\n' + "Subtotal: " + formatDecimalFormat.format(subTotalDouble) + '\n' + "Shipping: " + formatDecimalFormat.format(shippingDouble) + '\n' + "Tax: " + formatDecimalFormat.format(taxDouble) + '\n'+ "Total: " + formatDecimalFormat.format(totalDouble) + '\n' +'\n' + "Total Quantity To-Date: " + totalNumberOfOrdersInteger + '\n' + " Grand Total To-Date: " + formatDecimalFormat.format(grandTotalDouble) + '\n'); return; } public void clear() { //clear existing text from text fields and request cursor to top bookNameTextField.setText(""); quantityTextField.setText(""); priceTextField.setText(""); bookNameTextField.requestFocus(); } }
I hope that helps. -Eku
Reply With Quote