Need Assistance Please. My code is not adding the size and the toppins:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;

public class PizzaCalculator extends JFrame implements ActionListener {
    JPanel westPanel = new JPanel();
    JPanel eastPanel = new JPanel();
    JPanel centerPanel = new JPanel();
    JPanel northPanel = new JPanel();
    JPanel southPanel = new JPanel();

    // pizza size options
    JRadioButton Small = new JRadioButton("Small");
    JRadioButton Medium = new JRadioButton("Medium");
    JRadioButton Large = new JRadioButton("Large");

    // pizza topping selections
    JCheckBox Sausage = new JCheckBox("Sausage");
    JCheckBox Olives = new JCheckBox("Olives");
    JCheckBox Pepperoni = new JCheckBox("Pepperoni");
    JCheckBox Mushrooms = new JCheckBox("Mushrooms");
    JCheckBox Salami = new JCheckBox("Salami");
    JCheckBox Anchovies = new JCheckBox("Anchovies");

    // calculate, clear, and total buttons
    JButton Calculate = new JButton("Calculate");
    JButton Exit = new JButton("Exit");
    JLabel Total = new JLabel("Price: ");
    JTextField TotalResult = new JTextField(8);

    // variables used to calculate total
    int TopQty = 0;
    double Size = 0;
    double TopCost = 0;
    double TotalCost = 0;

   public PizzaCalculator() 
   {
       // layout north portion of frame
        northPanel.setLayout(new GridLayout());
        northPanel.setBorder(new TitledBorder("Size:"));

        // register listeners with buttons
        Small.addActionListener(this);
        Medium.addActionListener(this);
        Large.addActionListener(this);
        Calculate.addActionListener(this);

       ButtonGroup pizzaSize = new ButtonGroup();
       pizzaSize.add(Small);
       pizzaSize.add(Medium);
       pizzaSize.add(Large);


        // add buttons to north panel

        northPanel.add(Small);
        northPanel.add(Medium);
        northPanel.add(Large);


        centerPanel.setLayout(new GridLayout(5, 3));
        centerPanel.setBorder(new TitledBorder("Toppings:"));

        centerPanel.add(Sausage);

        centerPanel.add(Olives);

        centerPanel.add(Pepperoni);

        centerPanel.add(Mushrooms);

        centerPanel.add(Salami);

        centerPanel.add(Anchovies);

        southPanel.add(Total);
        southPanel.add(TotalResult);        
        southPanel.add(Calculate);
        southPanel.add(Exit);


        // add all portions of panel
        setLayout(new BorderLayout());
        add(northPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);
    }

   // handle action event for each pizza option
    public void actionPerformed(ActionEvent e) 
    {
       if (e.getSource() == Small)
            setSizeS();
        else if (e.getSource() == Medium)
            setSizeM();
        else if (e.getSource() == Large)
            setSizeL();

       if (e.getSource() == Sausage)
            TopQty++;
        else if (e.getSource() == Olives)
            TopQty++;
        else if (e.getSource() == Pepperoni)
            TopQty++;
        else if (e.getSource() == Mushrooms)
            TopQty++;
        else if (e.getSource() == Salami)
            TopQty++;
        else if (e.getSource() == Anchovies)
            TopQty++; 

       TopCost(TopQty);
        if (e.getSource() == Calculate)
            calculate(Size, TopCost);
    }

    public double setSizeS() 
    {
        return Size = 6.99;
    }

    public double setSizeM() 
    {
        return Size = 8.99;
    }

    public double setSizeL() 
    {
       return Size = 10.99;
    }


    public double setTopCost()
    {
                    if (TopQty == 1)
                        TopCost = 1.49;
                    else if (TopQty == 2)
                        TopCost = 1.49;
                    else if (TopQty == 3)
                        TopCost = 1.49;
                    else if (TopQty == 4)
                        TopCost = 0.99;
                    else if (TopQty == 5)
                        TopCost = 0.99;
                    else if (TopQty == 6)
                        TopCost = 0.99;


        return TopCost;

    }

    public void calculate(double Size, double TopCost) 
    {
       TotalCost = Size + TopCost;
        TotalResult.setText(String.valueOf(TotalCost));
    }

    public static void main(String[] args) 
    {
       PizzaCalculator PizzaCalc = new PizzaCalculator();
        // set window title
        PizzaCalc.setTitle("Pizza Calculator");
        // set window location
        PizzaCalc.setLocationRelativeTo(null);
        // specify window location
        PizzaCalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // set window size
        PizzaCalc.setSize(300, 350);
        // display window
        PizzaCalc.setVisible(true);
    }
}

Recommended Answers

All 3 Replies

It would help if yu posted the actual code.
Line 115 you call a method TopCost, but you haven't defined any such method.

Many of your methods are confusing because they set an instance variable, but they also return that value, and when you call the method you ignore the return value.

Thanks JamesCherrill,

I thought i did add the code.
Im a beguinner sorry my code is not less confusing.
The applciation returns the GUI however I know that line 115 is a issue
because TopCost is a double and TopQty is an int
and that is where im stocked at. when i run it it only gives me the pizza price by size

I went over my code and made few changes and it works!!
in case anyoneelse needs this here is the answer.

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


public class PizzaCalculator extends JFrame implements ActionListener {
    JPanel westPanel = new JPanel();
    JPanel eastPanel = new JPanel();
    JPanel centerPanel = new JPanel();
    JPanel northPanel = new JPanel();
    JPanel southPanel = new JPanel();

    // pizza size options
    JRadioButton Small = new JRadioButton("Small");
    JRadioButton Medium = new JRadioButton("Medium");
    JRadioButton Large = new JRadioButton("Large");

    // pizza topping selections
    JCheckBox Sausage = new JCheckBox("Sausage");
    JCheckBox Olives = new JCheckBox("Olives");
    JCheckBox Pepperoni = new JCheckBox("Pepperoni");
    JCheckBox Mushrooms = new JCheckBox("Mushrooms");
    JCheckBox Salami = new JCheckBox("Salami");
    JCheckBox Anchovies = new JCheckBox("Anchovies");

    // calculate, clear, and total buttons
    JButton Calculate = new JButton("Calculate");
    JButton Exit = new JButton("Exit");
    JLabel Total = new JLabel("Price: ");
    JTextField TotalResult = new JTextField(8);

    // variables used to calculate total
    int TopQty = 0;
    double Size = 0;
    double TopCost = 0;
    double TotalCost = 0;

   public PizzaCalculator() 
   {
       // layout north portion of frame
        northPanel.setLayout(new GridLayout());
        northPanel.setBorder(new TitledBorder("Size:"));

        // register listeners with buttons
        Small.addActionListener(this);
        Medium.addActionListener(this);
        Large.addActionListener(this);
        Calculate.addActionListener(this);

       ButtonGroup pizzaSize = new ButtonGroup();
       pizzaSize.add(Small);
       pizzaSize.add(Medium);
       pizzaSize.add(Large);


        // add buttons to north panel

        northPanel.add(Small);
        northPanel.add(Medium);
        northPanel.add(Large);


        centerPanel.setLayout(new GridLayout(5, 3));
        centerPanel.setBorder(new TitledBorder("Toppings:"));

        centerPanel.add(Sausage);

        centerPanel.add(Olives);

        centerPanel.add(Pepperoni);

        centerPanel.add(Mushrooms);

        centerPanel.add(Salami);

        centerPanel.add(Anchovies);

        southPanel.add(Total);
        southPanel.add(TotalResult);        
        southPanel.add(Calculate);
        southPanel.add(Exit);


        // add all portions of panel
        setLayout(new BorderLayout());
        add(northPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);
    }

   // handle action event for each pizza option
    public void actionPerformed(ActionEvent e) 
    {
       if (e.getSource() == Small)
            setSizeS();
        else if (e.getSource() == Medium)
            setSizeM();
        else if (e.getSource() == Large)
            setSizeL();

       setTopCost();
              if (e.getSource() == Calculate)
                calculate(Size, TopCost);
      }

    public double setSizeS() 
    {
        return Size = 6.99;
    }

    public double setSizeM() 
    {
        return Size = 8.99;
    }

    public double setSizeL() 
    {
       return Size = 10.99;
    }


    public double setTopCost()
    {               if ( Sausage.isSelected())
                        TopCost = 1.49;
                    else if ( Olives.isSelected())
                        TopCost = 1.49;
                    else if (Pepperoni.isSelected())
                        TopCost = 1.49;
                    else if (Mushrooms.isSelected())
                        TopCost = 0.99;
                    else if (Salami.isSelected())
                        TopCost = 0.99;
                    else if (Anchovies.isSelected())
                        TopCost = 0.99;


        return TopCost;

    }

    public void calculate(double Size, double TopCost) 
    {
       TotalCost = Size + TopCost;
        TotalResult.setText(String.valueOf(TotalCost));
    }

    public static void main(String[] args) 
    {
       PizzaCalculator PizzaCalc = new PizzaCalculator();
        // set window title
        PizzaCalc.setTitle("Pizza Calculator");
        // set window location
        PizzaCalc.setLocationRelativeTo(null);
        // specify window location
        PizzaCalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // set window size
        PizzaCalc.setSize(300, 350);
        // display window
        PizzaCalc.setVisible(true);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.