Calculating the total price
<p>How do you calculate the total price of of groceries purchased? At the moment, I can only calculate only one of the grocery with its quantity.</p>

<p>Below are the classes that I have used:</p>

<pre><code>import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;

/**
    Long Distance Calls
*/

public class GroceryMain extends JFrame
{
    private RatePanel ratePanel;        // A panel for rates
    private QuantityPanel quantityPanel;  // A panel for minutes
    private JPanel buttonPanel;         // A panel for the buttons
    private JButton calcButton;         // Calculates everything
    private JButton exitButton;         // Exits the application

    /**
       Constructor
    */

    public GroceryMain()
    {
        // Display a title.
        setTitle("Grocery Shop");

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

        // Create a RatePanel object.
        ratePanel = new RatePanel();

        // Create a MinutesPanel object.
        quantityPanel = new QuantityPanel();

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

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

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

    /**
       The buildButtonPanel method creates a panel containing
       buttons.
     */

    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);
    }

   /**
      CalcButtonListener is an action listener class for the
      calcButton component.
   */

   private class CalcButtonListener implements ActionListener
   {
      /**
         actionPerformed method
         @param e An ActionEvent object.
      */

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

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

         // Get the applicable rate.
         rate = ratePanel.getRate();

         // Get the total charges
         totalCharges = quantityPanel.getCharges(rate);

         // Display the message.
         JOptionPane.showMessageDialog(null, "Total Charges: £" + 
                                             dollar.format(totalCharges));
      }
   } // End of inner class

   /**
      ExitButtonListener is an action listener class for the
      exitButton component.
   */

   private class ExitButtonListener implements ActionListener
   {
      /**
         actionPerformed method
         @param e An ActionEvent object.
      */

      public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   } // End of inner class

   /**
      The main method creates an instance of the LongDistance
      class, causing it to display its window.
   */

   public static void main(String[] args)
   {
       GroceryMain ld = new GroceryMain();
   }
}


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

/**
   MinutesPanel class
   Long Distance Calls
*/

public class QuantityPanel extends JPanel
{
   private JTextField quantity;   // To get minutes
   private JTextField quantity2;   // To get minutes
   private JTextField baked_beans_JT;     // JTextField box for baked_beans
   private JTextField Cornflakes_JT;       // JTextField box for cornflakes
   private JTextField Sugar_JT; // JTextField box for sugar box
   private JTextField Tea_Bags_JT;    // JTextField box for tea bag
   private JTextField Instant_Coffee_JT;    // JTextField box for Instant_Coffee_Box
   private JTextField Bread_JT;       // JTextField box for bread box
   private JTextField Sausage_JT;  // JTextField box for sausage box
   private JTextField egg_JT;  // JTextField box for egg box
   private JTextField milk_JT;  // JTextField box for milk
   private JTextField potatoes_JT;  // JTextField box for potatoes

   /**
      Constructor
   */

   public QuantityPanel()
   {
      // Create a label prompting the user and a text field.
      //JLabel minutesMsg = new JLabel("Quantity:");
      quantity = new JTextField(5);

      quantity2 = new JTextField(5);
      baked_beans_JT = new JTextField(5);
      Cornflakes_JT = new JTextField(5);
      Sugar_JT = new JTextField(5);
      Tea_Bags_JT = new JTextField(5);
      Instant_Coffee_JT = new JTextField(5);
      Bread_JT = new JTextField(5);
      Sausage_JT = new JTextField(5);
      egg_JT = new JTextField(5);
      milk_JT = new JTextField(5);
      potatoes_JT = new JTextField(5);


      //initialize text field to 0
      baked_beans_JT.setText("0");
      Cornflakes_JT.setText("0");
      Sugar_JT.setText("0");
      Tea_Bags_JT.setText("0");
      Instant_Coffee_JT.setText("0");
      Bread_JT.setText("0");
      Sausage_JT.setText("0");
      egg_JT.setText("0");
      milk_JT.setText("0");
      potatoes_JT.setText("0");


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

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

      // Add the labels and text fields to this panel.
      //add(minutesMsg);
      //add(quantity);
     // add(quantity2);
      add(baked_beans_JT);
      add(Cornflakes_JT);
      add(Sugar_JT);
      add(Tea_Bags_JT);
      add(Instant_Coffee_JT);
      add(Bread_JT);
      add(Sausage_JT);
      add(egg_JT);
      add(milk_JT);
      add(potatoes_JT);

   }

   /**
      The getCharges method uses the specified rate to calculate
      the charges for the number of minutes entered.
      @param rate The per-minute rate.
      @return The charges for the number of minutes used.
   */

   public double getCharges(double rate)
   {
      double charges = Double.parseDouble(baked_beans_JT.getText()) * rate;

      return charges;
   }

}


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

/**
   RatePanel class
   Long Distance Calls
*/

public class RatePanel extends JPanel
{
   // Named constants for rates
   private final double BAKED_BEANS= 0.35;
   private final double CORNFLAKES = 1.75;
   private final double SUGAR = 0.75;
   private final double TEA_BAGS = 1.15;
   private final double INSTANT_COFFEE = 2.50;
   private final double BREAD = 1.25;
   private final double SAUSAGES = 1.30;
   private final double EGGS = 0.75;
   private final double MILK = 0.65;
   private final double POTATOES = 2.00;

   private JCheckBox bakedbeans; // Radio button for daytime rate
   private JCheckBox cornflakes; // Radio button for evening rate
   private JCheckBox sugar; // Radio button for off peak rate
   private JCheckBox teabags; // Radio button for off peak rate
   private JCheckBox instantcoffee; // Radio button for off peak rate
   private JCheckBox bread; // Radio button for off peak rate
   private JCheckBox sausages; // Radio button for off peak rate
   private JCheckBox eggs; // Radio button for off peak rate
   private JCheckBox milk; // Radio button for off peak rate
   private JCheckBox potatoes; // Radio button for off peak rate

   //private ButtonGroup bg;       // Radio button group

   /**
      Constructor
   */

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

      // Create the check boxes.
      bakedbeans = new JCheckBox("Bakedbeans (£" +
                        dollar.format(BAKED_BEANS) + " per packet)");
      cornflakes = new JCheckBox("Cornflakes (£" +
                        dollar.format(CORNFLAKES) + " per packet)");
      sugar = new JCheckBox("Sugar (£" + 
                        dollar.format(SUGAR) + " per packet)");

      teabags = new JCheckBox("Teabags (£" + 
              dollar.format(TEA_BAGS) + " per item)");
      instantcoffee = new JCheckBox("Instantcoffee (£" + 
              dollar.format(INSTANT_COFFEE) + " per packet)");
      bread = new JCheckBox("Bread (£" + 
              dollar.format(BREAD) + " per packet)");
      sausages = new JCheckBox("Sausages (£" + 
              dollar.format(SAUSAGES) + " per packet)");
      eggs = new JCheckBox("Eggs (£" + 
              dollar.format(EGGS) + " per packet)");
      milk = new JCheckBox("Milk (£" + 
              dollar.format(MILK) + " per packet)");
      potatoes = new JCheckBox("Potatoes (£" + 
              dollar.format(POTATOES) + " per packet)");

      // Create a GridLayout manager.
      setLayout(new GridLayout(15, 12));

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

   // Add the check boxes to this panel.
      add(bakedbeans);
      add(cornflakes);
      add(sugar);
      add(teabags);
      add(instantcoffee);
      add(bread);
      add(sausages);
      add(eggs);
      add(milk);
      add(potatoes);
   }

   /**
      The getRate method returns the rate for the selected
      rate category.
      @return One of the constants DAYTIME_RATE, EVENING_RATE, or
              OFF_PEAK_RATE.
   */

   public double getRate()
   {
      double rate = 0.0;

      if (bakedbeans.isSelected())
         rate += BAKED_BEANS;
      if (cornflakes.isSelected())
         rate = CORNFLAKES;
      else if (sugar.isSelected())
         rate += SUGAR;
      else if (teabags.isSelected())
          rate += TEA_BAGS;
      else if (instantcoffee.isSelected())
          rate += INSTANT_COFFEE;
      else if (bread.isSelected())
          rate += BREAD;
      else if (sausages.isSelected())
          rate += SAUSAGES;
      else if (eggs.isSelected())
          rate += EGGS;
      else if (milk.isSelected())
          rate += MILK;
      else if (potatoes.isSelected())
          rate += POTATOES;

      return rate;
   }
}
</code></pre>

<p>I want to be able to calculate the total price of groceries when the user select a multiple of check-boxes with its quantities.. </p>
