Comp Sci Java Project Calculator for Shipping Costs

AI Thread Summary
The discussion revolves around a Java project aimed at calculating shipping costs based on package weight. The initial issue was that the shipping cost always displayed as zero due to the `calculateShipping` method not being called before displaying the output. After correcting this by adding the method call in the `actionPerformed` method, the user faced a new problem where output labels for the package ID, weight, and shipping cost were not appearing. The solution suggested was to ensure that the panel is updated and revalidated after adding new components, which is necessary for the GUI to refresh and display the new labels correctly. The conversation highlights the importance of method calls and GUI updates in Java Swing applications.
clook
Messages
32
Reaction score
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:
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');
	
	}
}
 
Physics news on Phys.org
You never called calculuateShipping. It's been at least a year and a half since I've done any JAVA programming, but I think you could put it either within the actionlisten or within displayOutput.
 
how can i put it within displayoutput?
 
its been a while since i did ne java, but if u just put:

Code:
        public void actionPerformed(ActionEvent evt)
	{
		getInput();
                calculateShipping();
		displayOutput();
	}

Just adding it before u call displayOutput should calculate the shipping, then u can display it... i believe that is how it would work.
 
thanks.. works now.
 
Last edited:
i am trying to display the output in a jlabel. I have created individual jlabels for the package ID, weight, and shipping cost and tried to display them with the mainPanel:

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  ");
	
	
	//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);
		
		
        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 calculateShipping()
	{
		final double SHIPPING_RATE = .12;
		final double OUNCES_PER_POUND = 16;
		poundToOunceOuncesDouble = poundDouble * OUNCES_PER_POUND;
		shippingCostDouble = (poundToOunceOuncesDouble + ouncesDouble) * SHIPPING_RATE;
		
	}

	public void actionPerformed(ActionEvent evt)
	{
		getInput();
		calculateShipping();
		displayOutput();
		
		
	}
	
	public void displayOutput()
	{
		
		mainPanel.add(new JLabel("Package ID:" + packageIdentificationString));
        mainPanel.add(new JLabel("Weight:" + poundDouble + "lbs" + ouncesDouble + "oz."));
        mainPanel.add(new JLabel("Shipping Cost:" + shippingCostDouble));
	
			
	}
}

here is the revised code, but the output labels aren't showing up. what gives?
 
Last edited:

Similar threads

Replies
1
Views
4K
Replies
9
Views
3K
Replies
3
Views
3K
Replies
3
Views
13K
Replies
7
Views
4K
Back
Top