- #1
clook
- 35
- 0
i'm supposed to create a GUI project that will display a package ID and calculate its shipping cost. the shipping cost is calculated per ounce, and i must convert the pounds to ounces for the shipping cost. everything seems to be working fine except for the calculation. the shipping cost is always showing up as 0, and i am pretty confident my formulas are right. is the formula not being read since it is only in a method? if so, how should i go about doing this?
here is my code:
here is my code:
Code:
import javax.swing.*;
import java.awt.event.*;
public class shippingCharge extends JFrame implements ActionListener
{
//objects and data types created here
JPanel mainPanel = new JPanel();
JTextField packageIdentificationTextField = new JTextField(6);
JTextField poundsTextField = new JTextField(10);
JTextField ouncesTextField = new JTextField(10);
JButton displayButton = new JButton("Calculate ");
JTextArea outputTextArea = new JTextArea("Your shipping charge ", 10,30);
//Variables
String packageIdentificationString;
double poundDouble, ouncesDouble, poundToOunceOuncesDouble, shippingCostDouble;
public static void main(String[] args)
{
shippingCharge shippingTotal = new shippingCharge();
}
public shippingCharge()
{
designFrame();
setSize(500,500);
setVisible(true);
}
public void designFrame()
{
mainPanel.add(new JLabel("Package ID"));
mainPanel.add(packageIdentificationTextField);
mainPanel.add(new JLabel("Pounds"));
mainPanel.add(poundsTextField);
mainPanel.add(new JLabel("Ounces"));
mainPanel.add(ouncesTextField);
mainPanel.add(displayButton);
mainPanel.add(outputTextArea);
add(mainPanel);
//add listener to the object
packageIdentificationTextField.addActionListener(this);
displayButton.addActionListener(this);
}
public void getInput()
{
packageIdentificationString = packageIdentificationTextField.getText();
poundDouble = Double.parseDouble(poundsTextField.getText());
ouncesDouble = Double.parseDouble(ouncesTextField.getText());
}
public void actionPerformed(ActionEvent evt)
{
getInput();
displayOutput();
}
public void calculateShipping()
{
final double SHIPPING_RATE = .12;
final double OUNCES_PER_POUND = 16;
poundToOunceOuncesDouble = poundDouble * OUNCES_PER_POUND;
shippingCostDouble = (poundToOunceOuncesDouble + ouncesDouble) * SHIPPING_RATE;
}
public void displayOutput()
{
outputTextArea.append('\n' + "Package ID : " + packageIdentificationString + '\n' +
"Pounds : " + poundDouble + '\n' +
"Ounces : " + ouncesDouble + '\n'+
"Shipping Cost : " + shippingCostDouble + '\n');
}
}