View Single Post
  #15 (permalink)  
Old 05-13-2008, 12:44 PM
crazydeo crazydeo is offline
Member
 
Join Date: May 2008
Posts: 21
crazydeo is on a distinguished road
Code:
import javax.swing.*; import javax.swing.JTextField; import java.awt.event.*; import java.text.*; import java.awt.*; public class Main extends JFrame implements ActionListener { // 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); //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 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 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 listeners priceTextField.addActionListener(this); calculateButton.addActionListener(this); //add the panel to the frame add(mainPanel); setSize(300,400); setVisible(true); } public void actionPerformed(ActionEvent evt) { getInput(); clear(); } //declaring local variables double quantityDouble, priceDouble; Calculation myOrder; String nameString; public void getInput() { // get input quantityDouble = Double.parseDouble(quantityTextField.getText()); priceDouble = Double.parseDouble(priceTextField.getText()); nameString = bookNameTextField.getText(); //send the input the calculation class myOrder = new Calculation(quantityDouble, priceDouble); displayOutput(myOrder.getQuantity()); } public void displayOutput(double CalculationDouble) { //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(); 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(quantityDouble) + '\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'); } public void clear() { //clear existing text from text fields and request cursor to top bookNameTextField.setText(""); quantityTextField.setText(""); priceTextField.setText(""); bookNameTextField.requestFocus(); } }
I orginally did it this way and everything worked, but was told that the object is not private
Reply With Quote