Question

You are to design and implement a GUI application that will calculate the price of a pizza,
based upon the size, style of crust, and selection of toppings.

The price is calculated
according to the following rules:

• A plain small pizza is $6, medium is $8, large is $10, and a partysize
is $12.

• Thick crust is $1.00 more than thin crust, regardless of size.

• Additional toppings are $0.75, $1.25, $1.75, and $2.25 each for small, medium,
large, and partysize
pizzas, respectively.

I recommend using radio buttons for the choice of size and crust, and check boxes for the
selection of toppings. There should be at least six different toppings available.

Program:

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");
    JRadioButton Party = new JRadioButton("Party");

    // pizza crust choices
    JRadioButton Thin = new JRadioButton("Thin");
    JRadioButton Thick = new JRadioButton("Thick");

    // pizza topping selections
    JCheckBox Cheese = new JCheckBox("Cheese");
    JCheckBox Pepperoni = new JCheckBox("Pepperoni");
    JCheckBox Sausage = new JCheckBox("Sausage");
    JCheckBox Ham = new JCheckBox("Ham");
    JCheckBox Pinapple = new JCheckBox("Pinapple");
    JCheckBox BlackOlives = new JCheckBox("Black Olives");

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

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

    public PizzaCalculator() {
        // layout north portion of frame
        northPanel.setLayout(new GridLayout(2, 3));
        northPanel.setBorder(new TitledBorder("Pizza Size and Crust Type:"));

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

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

        ButtonGroup pizzaCrust = new ButtonGroup();
        pizzaCrust.add(Thin);
        pizzaCrust.add(Thick);

        // add buttons to north panel
        northPanel.add(new JTextField("Pizza Total Calculator"));
        northPanel.add(Small);
        northPanel.add(Medium);
        northPanel.add(Large);
        northPanel.add(Party);
        northPanel.add(Thin);
        northPanel.add(Thick);

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

        centerPanel.add(Cheese);
        centerPanel.add(Pepperoni);
        centerPanel.add(Sausage);
        centerPanel.add(Ham);
        centerPanel.add(Pinapple);
        centerPanel.add(BlackOlives);

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

        // add all portions of panel
        setLayout(new BorderLayout(5, 5));
        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();
        else if (e.getSource() == Party)
            setSizeP();

        if (e.getSource() == Thin)
            setCrustThin();
        else if (e.getSource() == Thick)
            setCrustThick();

        if (e.getSource() == Cheese)
            TopQty++;
        else if (e.getSource() == Pepperoni)
            TopQty++;
        else if (e.getSource() == Sausage)
            TopQty++;
        else if (e.getSource() == Ham)
            TopQty++;
        else if (e.getSource() == Pinapple)
            TopQty++;
        else if (e.getSource() == BlackOlives)
            TopQty++;

        TopCost(TopQty);

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

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

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

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

    public double setSizeP() {
        return Size = 12.00;
    }

    public double setCrustThin() {
        return CrustCost = 0.00;
    }

    public double setCrustThick() {
        return CrustCost = 1.00;
    }

    public double TopCost(int TopQty) {

        if (e.getSource() == Small)
            if (TopQty == 1)
                TopCost = 0.75;
            else if (TopQty == 2)
                TopCost = 2 * (0.75);
            else if (TopQty == 3)
                TopCost = 3 * (0.75);
            else if (TopQty == 4)
                TopCost = 4 * (0.75);
            else if (TopQty == 5)
                TopCost = 5 * (0.75);
            else if (TopQty == 6)
                TopCost = 6 * (0.75);
            else if (e.getSource() == Medium)
                if (TopQty == 1)
                    TopCost = 1.25;
                else if (TopQty == 2)
                    TopCost = 2 * (1.25);
                else if (TopQty == 3)
                    TopCost = 3 * (1.25);
                else if (TopQty == 4)
                    TopCost = 4 * (1.25);
                else if (TopQty == 5)
                    TopCost = 5 * (1.25);
                else if (TopQty == 6)
                    TopCost = 6 * (1.25);
                else if (e.getSource() == Large)
                    if (TopQty == 1)
                        TopCost = 1.75;
                    else if (TopQty == 2)
                        TopCost = 2 * (1.75);
                    else if (TopQty == 3)
                        TopCost = 3 * (1.75);
                    else if (TopQty == 4)
                        TopCost = 4 * (1.75);
                    else if (TopQty == 5)
                        TopCost = 5 * (1.75);
                    else if (TopQty == 6)
                        TopCost = 6 * (1.75);
                    else if (e.getSource() == Party)
                        if (TopQty == 1)
                            TopCost = 2.25;
                        else if (TopQty == 2)
                            TopCost = 2 * (2.25);
                        else if (TopQty == 3)
                            TopCost = 3 * (2.25);
                        else if (TopQty == 4)
                            TopCost = 4 * (2.25);
                        else if (TopQty == 5)
                            TopCost = 5 * (2.25);
                        else if (TopQty == 6)
                            TopCost = 6 * (2.25);

        return TopCost;

    }

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

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

Error:

----jGRASP exec: javac -g PizzaCalculator.java

PizzaCalculator.java:166: error: cannot find symbol
if(e.getSource() == Small)
^
symbol: variable e
location: class PizzaCalculator
PizzaCalculator.java:179: error: cannot find symbol
else if(e.getSource() == Medium)
^
symbol: variable e
location: class PizzaCalculator
PizzaCalculator.java:192: error: cannot find symbol
else if(e.getSource() == Large)
^
symbol: variable e
location: class PizzaCalculator
PizzaCalculator.java:205: error: cannot find symbol
else if(e.getSource() == Party)
^
symbol: variable e
location: class PizzaCalculator
PizzaCalculator.java:237: error: cannot find symbol
PizzaCalc.setDefaultCloseOperation(JFrame.EXIT_ON_ClOSE );
^
symbol: variable EXIT_ON_ClOSE
location: class JFrame
errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

e.getSource() is something you expect in an actionPerformed method which's ActionEvent parameter is called e.

In the method you try to call 'e.getSource()' you don't have an e variable. either you put that code in your actionperformed, or you add an ActionEvent called e as parameter.

But, since this wouldn't make much sense here, how about:

if ( Small.isSelected())
... instead?

and your question? Just dumping something here that's clearly your homework assigment and expecting us to complete it for you is not going to help.
It's also clearly not allowed.

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.