0

So I am trying to create a variable in a class and then retrieve it in a different class...

In one class there are products and the other class is for the quantity and I need to take the value from the text box and then multiply the cost of the item by the quantity.

I am no expert in Java by any means, I just cannot seem to get this working.

Without further a-do, here is the code I am working with:

SuperMarket Class:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;


public class SuperMarket extends JFrame
{
   private ProductPanel routine;       // A panel for routine charge check boxes
   private QuantityPanel nonRoutine;   // A panel for non-routine charges
   private JPanel buttonPanel;         // A panel for the buttons
   private JButton calcButton;         // Calculates everything
   private JButton exitButton;         // Exits the application

   public SuperMarket()
   {
      // Display a title.
      setTitle("Supermarket");

      // Specify what happens when the close button is clicked.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create a RoutinePanel object.
      routine = new ProductPanel();

      // Create a NonRoutinePanel object.
      nonRoutine = new QuantityPanel();

      // Build the panel that contains the buttons.
      buildButtonPanel();

      // Add the panels to the content pane.
      add(routine, BorderLayout.WEST);
      add(nonRoutine, BorderLayout.EAST);
      add(buttonPanel, BorderLayout.SOUTH);

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   private void buildButtonPanel()
   {
      // Create a button to calculate the charges.
      calcButton = new JButton("Calculate Charges");

      // Add an action listener to the button.
      calcButton.addActionListener(new CalcButtonListener());

      // Create a button to exit the application.
      exitButton = new JButton("Exit");

      // Add an action listener to the button.
      exitButton.addActionListener(new ExitButtonListener());

      // Put the buttons in their own panel.
      buttonPanel = new JPanel();
      buttonPanel.add(calcButton);
      buttonPanel.add(exitButton);
   }

   private class CalcButtonListener implements ActionListener
   {

      public void actionPerformed(ActionEvent e)
      {
         double totalCharges; // Total charges

         // Create a DecimalFormat object to format output.
         DecimalFormat dollar = new DecimalFormat("#,##0.00");

         // Calculate the total charges
         totalCharges = routine.getCharges();

         // Display the message.
         JOptionPane.showMessageDialog(null, "Total Charges: $" + 
                                             dollar.format(totalCharges));
      }
   }

   private class ExitButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   }

   public static void main(String[] args)
   {
      SuperMarket ja = new SuperMarket();
   }
}

ProductPanel Class

import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class ProductPanel extends JPanel
{
   // Named constants for charges
    private QuantityPanel nonRoutine;

   private final double BAKED_BEAN_CHARGE = 0.35;
   private final double CORNFLAKE_CHARGE = 1.75;
   private final double SUGAR_CHARGE = 0.75;
   private final double TEA_BAGS_CHARGE = 1.15;
   private final double INSTANT_COFFEE_CHARGE = 2.50;
   private final double BREAD_CHARGE = 1.25;
   private final double SAUSAGES_CHARGE = 1.30;
   private final double EGGS_CHARGE = 1.30;
   private final double MILK_CHARGE = 1.30;
   private final double POTATOES_CHARGE = 1.30;

   private JCheckBox bakedBean;     // Check box for oil change
   private JCheckBox cornFlake;       // Check box for lube job
   private JCheckBox sugar; // Check box for radiator flush
   private JCheckBox teaBags;    // Check box for transmission flush
   private JCheckBox instantCoffee;    // Check box for inspection
   private JCheckBox bread;       // Check box for muffler replacement
   private JCheckBox sausages;  // Check box for tire rotation
   private JCheckBox eggs;  // Check box for tire rotation
   private JCheckBox milk;  // Check box for tire rotation
   private JCheckBox potatoes;  // Check box for tire rotation

   /**
      Constructor
   */

   public ProductPanel()
   {
      // Create a DecimalFormat object.
      DecimalFormat dollar = new DecimalFormat("#,##0.00");

      // Create the check boxes.
      bakedBean = new JCheckBox("Baked Beans ($" +
                                dollar.format(BAKED_BEAN_CHARGE) + ")");
      cornFlake = new JCheckBox("Cornflakes ($" +
                              dollar.format(CORNFLAKE_CHARGE) + ")");
      sugar = new JCheckBox("Sugar ($" + 
                                    dollar.format(SUGAR_CHARGE) + ")");
      teaBags = new JCheckBox("Tea Bags ($" + 
                                 dollar.format(TEA_BAGS_CHARGE) + ")");
      instantCoffee = new JCheckBox("Instant Coffee ($" + 
                                 dollar.format(INSTANT_COFFEE_CHARGE) + ")");
      bread = new JCheckBox("Bread ($" + 
                              dollar.format(BREAD_CHARGE) + ")");
      sausages = new JCheckBox("Sausages ($" + 
              dollar.format(SAUSAGES_CHARGE) + ")");

      eggs = new JCheckBox("Eggs ($" + 
              dollar.format(EGGS_CHARGE) + ")");

      milk = new JCheckBox("Milk ($" + 
              dollar.format(MILK_CHARGE) + ")");

      potatoes = new JCheckBox("Potatoes ($" + 
              dollar.format(POTATOES_CHARGE) + ")");

      // Create a GridLayout manager.
      setLayout(new GridLayout(10, 1));

      // Create a border.
      setBorder(BorderFactory.createTitledBorder("Food Product"));

      // Add the check boxes to this panel.
      add(bakedBean);
      add(cornFlake);
      add(sugar);
      add(teaBags);
      add(instantCoffee);
      add(bread);
      add(sausages);
      add(eggs);
      add(milk);
      add(potatoes);
   }

   /**
      The getCharges method calculates the routine charges.
      @return The amount of routine charges.
   */

   public double getCharges()
   {
      double charges = 0;

      if (bakedBean.isSelected())
          charges += BAKED_BEAN_CHARGE * 1;
      if (cornFlake.isSelected())
         charges += CORNFLAKE_CHARGE;
      if (sugar.isSelected())
         charges += SUGAR_CHARGE;
      if (teaBags.isSelected())
         charges += TEA_BAGS_CHARGE;
      if (instantCoffee.isSelected())
         charges += INSTANT_COFFEE_CHARGE;
      if (bread.isSelected())
         charges += BREAD_CHARGE;
      if (sausages.isSelected())
          charges += SAUSAGES_CHARGE;
      if (eggs.isSelected())
          charges += EGGS_CHARGE;
      if (milk.isSelected())
          charges += MILK_CHARGE;
      if (potatoes.isSelected())
          charges += POTATOES_CHARGE;

      return charges;
   }
}

QuantityPanel Class

import java.awt.*;
import javax.swing.*;


public class QuantityPanel extends JPanel
{   
   public JTextField beans;  // Parts charges
   private JTextField cornFlakes;  // Hours of labor
   private JTextField sugar;  // Hours of labor
   private JTextField teaBags;  // Hours of labor
   private JTextField instantCoffee;  // Hours of labor
   private JTextField bread;  // Hours of labor
   private JTextField sausages;  // Hours of labor
   private JTextField eggs;  // Hours of labor
   private JTextField milk;  // Hours of labor
   private JTextField potatoes;  // Hours of labor

   public QuantityPanel()
   {
      beans = new JTextField("0", 4);
      double beanQuantity = Double.parseDouble(beans.getText());
      cornFlakes = new JTextField("0", 4);
      sugar = new JTextField("0", 4);
      teaBags = new JTextField("0", 4);
      instantCoffee = new JTextField("0", 4);
      bread = new JTextField("0", 4);
      sausages = new JTextField("0", 4);
      eggs = new JTextField("0", 4);
      milk = new JTextField("0", 4);
      potatoes = new JTextField("0", 4);


      // Create a GridLayout manager.
      setLayout(new GridLayout(10, 1));

      // Create a border.
      setBorder(BorderFactory.createTitledBorder("Quantity"));

      // Add the labels and text fields to this panel.
      add(beans);
      add(cornFlakes);
      add(sugar);
      add(teaBags);
      add(instantCoffee);
      add(bread);
      add(sausages);
      add(eggs);
      add(milk);
      add(potatoes);
   }
}

As you can see on the Quantity class, I have tried to create a double which gets the value from the textfield, and I have played around trying to retrieve it where you can see the BAKED_BEAN_CHARGE * 1, however instead of * 1, I need the users textfield input.

Any help is appreciated, cheers.

2 Answers 2

1

The issue here is scope: you are creating a new variable inside the constructor.

Take the double beanQuantity declaration outside of the constructor, and just initialize it with the textfield input.

With your beanQuantity as a class variable you can create a method to return it (a getter) anytime you need it.

you can just call it private double beanQuantity; right below the last JTextField and then create a method public double getBeanQuantity() { return beanQuantity; } just make sure that in your constructor you remove the double keyword

Sign up to request clarification or add additional context in comments.

Comments

0

First, you need to pass your QuantityPanel into your ProductPanel constructor so you can set the member variable that you have the stub for. To do this, change your SuperMarket constructor thusly:

  // Create a NonRoutinePanel object.
  nonRoutine = new QuantityPanel();

  // Create a RoutinePanel object.
  routine = new ProductPanel( nonRoutine );

Then modify your constructor in ProductPanel like this:

public ProductPanel( QuantityPanel nonRoutine ) {
    this.nonRoutine = nonRoutine;
    ...

You need to move this line:

double beanQuantity = Double.parseDouble(beans.getText());

To a method:

public double getBeanQuantity() {
    return Double.parseDouble( beans.getText() );
}

Then in your product panel you can use it thusly:

...
if (bakedBean.isSelected())
    charges += BAKED_BEAN_CHARGE * nonRoutine.getBeanQuantity();
...

1 Comment

Excellent, thank you very much! I tried a few of these steps and got close, but I followed all of yours and it worked straight away cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.