- #1
clook
- 35
- 0
I'm trying to create a bookstore calculation program with two classes, a presentation and computation class. Here is my code for the presentation class:
Problem is, I'm unable to output the variables and objects from getInput()
I'm not sure why.. can someone help me with this?
Code:
import javax.swing.*;
import java.awt.event.*;
import java.text.*;
import java.awt.*;
public class ch4Book extends JFrame implements ActionListener
{
// All the GUI objects
JPanel mainPanel = new JPanel();
JLabel companyLabel = new JLabel(" OrderBook Inc. ");
JTextField titleTextField = new JTextField(30);
JTextField quantityTextField = new JTextField(15);
JTextField priceTextField = new JTextField(15);
JButton calculateButton = new JButton(" Calculate ");
JTextArea outputTextArea = new JTextArea("Your Total Cost:", 10, 25);
JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
public static void main(String[] args)
{
//create an object of the class and code the close button
ch4Book myCost = new ch4Book();
myCost.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public ch4Book()
{
designFrame();
}
public void designFrame()
{
//add the components to the mainPanel
mainPanel.add(companyLabel);
mainPanel.add(new JLabel(" Title "));
mainPanel.add(titleTextField);
mainPanel.add(new JLabel(" Quantity "));
mainPanel.add(quantityTextField);
mainPanel.add(new JLabel(" Price "));
mainPanel.add(priceTextField);
mainPanel.add(calculateButton);
mainPanel.add(outputScrollPane);
//add the listeners
calculateButton.addActionListener(this);
//add the panel to the frame
add(mainPanel);
setSize(500,500);
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
getInput();
displayOutput();
clear();
}
public void getInput()
{
//input variables
double priceDouble;
int quantityInteger;
String titleString;
priceDouble = Double.parseDouble(priceTextField.getText());
quantityInteger = Integer.parseInt(quantityTextField.getText());
titleString = titleTextField.getText();
//send the input the calculation class
Cost myTotalCost = new Cost(priceDouble, quantityInteger);
displayOutput();
}
public void displayOutput()
{
// object to format to currency
DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");
//variables to store the retrieved values from the computation class
double subTotalDouble, totalCostDouble;
int counterInteger;
Cost myTotalCost = new Cost();
subTotalDouble = myTotalCost.getSubTotal();
totalCostDouble = myTotalCost.getTotalCost();
counterInteger = myTotalCost.getCounterInteger();
outputTextArea.append('\n' + "Book Title: " + titleString +
'\n' + " Quantity " + formatDecimalFormat.format(quantityInteger)+
'\n' + "Number of Customers " + counterInteger);
}
public void clear()
{
titleTextField.setText("");
quantityTextField.setText("");
priceTextField.setText("");
}
}
Problem is, I'm unable to output the variables and objects from getInput()
I'm not sure why.. can someone help me with this?