Java Project Calculator for Shipping Costs

Click For Summary

Discussion Overview

The discussion revolves around a Java project aimed at creating a GUI application for calculating shipping costs based on package weight. Participants address issues related to method calls, output display, and the integration of GUI components.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant notes that the shipping cost calculation is always returning zero and questions whether the calculation method is being called correctly.
  • Another participant suggests that the calculation method needs to be called within the action listener or the display output method to function properly.
  • A later reply provides a code snippet showing how to incorporate the calculation method before displaying the output, indicating that this should resolve the issue.
  • In a subsequent post, a participant attempts to modify the code to display output in JLabels instead of a JTextArea but encounters issues with the labels not appearing.
  • The participant seeks clarification on why the output labels are not showing up in the GUI after the revision.

Areas of Agreement / Disagreement

Participants generally agree on the need to call the calculation method to resolve the initial issue. However, the later issue regarding the display of output labels remains unresolved, indicating a lack of consensus on that specific problem.

Contextual Notes

There are limitations regarding the handling of GUI updates, as the addition of new components to the main panel may require a revalidation or repaint of the panel to be displayed correctly.

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 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
13K
  • · Replies 7 ·
Replies
7
Views
4K