Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:48:33+00:00 2026-06-12T12:48:33+00:00

I am not to sure what the error is in my code but the

  • 0

I am not to sure what the error is in my code but the order variable does not seem to initialise properly and hence my gui displays the wrong output, for example when cheese,tomato and chicken are selected it only displays Chicken with Tomato Tomato $12.75. Any help will be greatly appreciated 🙂

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

class PizzaFrame extends JFrame 
{
// The following components are defined (but not created).
private JButton newButton, exitButton;
private JLabel priceLabel, grandTotalLabel;
private JCheckBox chkCheese, chkChicken, chkTomato;
private JTextArea jTA;

// Other values used throughout this frame
private double total, orderTotal, grandTotal;
private String order = "";
private static final String noOrder =  "Base price of basic pizza is $10.00";

// set up GUI
public PizzaFrame() 
{
    // Ensure there is a suitable title for the frame
    setTitle("Welcome to Mylo's Pizza world, please make an order");
    setLayout(new FlowLayout());

    // Create the buttons for the user to click, and add them to the frame
    newButton = new JButton("New Order");
    exitButton = new JButton("Exit \t \n");
    add(newButton);
    add(exitButton);

    // Create the Check Boxes and add these to the frame.
    chkCheese = new JCheckBox("Cheese");
    chkChicken = new JCheckBox("Chicken");
    chkTomato = new JCheckBox("Tomato");
    add(chkCheese);
    add(chkChicken);
    add(chkTomato);


    priceLabel = new JLabel(noOrder);
    add(priceLabel);

    // Create the TextArea in which to display the progressive orders.
    jTA = new JTextArea(10,10);
    //JScrollPane scroller = new JScrollPane(jTA);
    //jTA.setLineWrap(true);
    //scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    //scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    add(jTA);
    //add(scroller);

    // Create the lower Label which displays the       
    grandTotalLabel = new JLabel("Startup - No orders taken yet. Total: $0");
    add(grandTotalLabel);


    ButtonHandler event_handler = new ButtonHandler();
    newButton.addActionListener(event_handler);
    exitButton.addActionListener(event_handler);


    // create and register listeners for the JCheckBoxes
    CheckBoxHandler box_listener = new CheckBoxHandler();
    chkCheese.addItemListener(box_listener);
    chkChicken.addItemListener(box_listener);
    chkTomato.addItemListener(box_listener);

    //set the size of the frame
    setSize( 250, 310 );

} 


private class ButtonHandler implements ActionListener 
{
    // start new order or exit on button event
    public void actionPerformed( ActionEvent event ) 
    {
        orderTotal = total;
        grandTotal += orderTotal;
        //boolean action = false;
        if (event.getSource() == newButton) 
        {
            // TO BE COMPLETED
            order = "";
            chkCheese.setSelected (false);
            chkChicken.setSelected(false);
            chkTomato.setSelected(false);
            //String order_total = Double.toString(orderTotal);
            jTA.append(order + " $" + Double.toString(orderTotal) + "\n");



        }
        else if (event.getSource() == exitButton) 
        {
            System.exit(0);
        }
    } 
} 



private class CheckBoxHandler implements ItemListener 
{
    static final double CHEESE_COST = 2.75;
    static final double CHICKEN_COST = 4.00;
    static final double TOMATO_COST = 0.50;

// respond to checkbox events by adding cost of pizza topping options to price
    public void itemStateChanged( ItemEvent event )
    {
            total = 10.00;
            // TO BE COMPLETED
            String selection = "";
            if (chkCheese.isSelected()) 
                total = total + CHEESE_COST;
            else if (chkChicken.isSelected()) 
                total = total + CHICKEN_COST;
            else if (chkTomato.isSelected()) 
                total = total + TOMATO_COST;



            if (chkCheese.isSelected() && chkChicken.isSelected() && chkTomato.isSelected()) 
            {
                selection = "Cheese with Chicken and Tomato ";
                order += selection;
            }
            else if(chkCheese.isSelected() && chkChicken.isSelected() && chkTomato.isSelected())
            {
                      //else
                          order += "Cheese with Chicken ";
            }
            else if(chkCheese.isSelected() && chkTomato.isSelected())
            {
                    //if (chkTomato.isSelected()) 
                        order += "Cheese with Tomato ";
            }

            else if (chkChicken.isSelected() && chkTomato.isSelected())
            {
                  //if (chkTomato.isSelected()) 
                      order += "Chicken with Tomato ";
            }
            else if(chkCheese.isSelected())
            {
                        order += "Cheese ";     
            }
            else if (chkChicken.isSelected())
            {
                      order += "Chicken ";
            }
            else if (chkTomato.isSelected())
            {
                      order += "Tomato ";
            }


        priceLabel.setText("Price of pizza is: $" +  Double.toString(total));
        grandTotalLabel.setText("Total income is: $" +  Double.toString(grandTotal));

    } 
} 

//----------------------------------------- Program Entry/Start point:

public static void main( String args[] ) 
{
    PizzaFrame pFrame = new PizzaFrame();
    pFrame.addWindowListener ( 

                                  // TO BE COMPLETED
            new WindowAdapter() 
            {   
                public void windowClosing (WindowEvent e) 
                {
                            System.exit(0); 
                }            
            }   

    );                          

    // Show the pizza frame ...
    pFrame.setVisible( true );
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:48:35+00:00Added an answer on June 12, 2026 at 12:48 pm

    Else if quits, if a condition was fulfilled. You want the price to be the sum of all selected items, not just the first selected item.
    There was a tiny mistake in your conditions for the text-display, too.

    public void itemStateChanged( ItemEvent event )
        {
                total = 10.00;
                // TO BE COMPLETED
                String selection = "";
                if (chkCheese.isSelected()) 
                    total = total + CHEESE_COST;
                if (chkChicken.isSelected()) 
                    total = total + CHICKEN_COST;
                if (chkTomato.isSelected()) 
                    total = total + TOMATO_COST;
    
    
    
                if (chkCheese.isSelected() && chkChicken.isSelected() && chkTomato.isSelected()) 
                {
                    selection = "Cheese with Chicken and Tomato ";
                    order += selection;
                }
                else if(chkCheese.isSelected() && chkChicken.isSelected())
                {
                              order += "Cheese with Chicken ";
                }
                else if(chkCheese.isSelected() && chkTomato.isSelected())
                { 
                            order += "Cheese with Tomato ";
                }
    
                else if (chkChicken.isSelected() && chkTomato.isSelected())
                {
                          order += "Chicken with Tomato ";
                }
                else if(chkCheese.isSelected())
                {
                            order += "Cheese ";     
                }
                else if (chkChicken.isSelected())
                {
                          order += "Chicken ";
                }
                else if (chkTomato.isSelected())
                {
                          order += "Tomato ";
                }
    
    
            priceLabel.setText("Price of pizza is: $" +  Double.toString(total));
            grandTotalLabel.setText("Total income is: $" +  Double.toString(grandTotal));
    
        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

not sure what I'm missing here, but i keep getting the error. SQLSTATE[HY093]: Invalid
Good day, My code generates a run-time error and I'm not sure how to
I am not sure why, But when I run the following code and there
I am not sure what this error is. Thought I would ask you guys
Not sure why I'm getting this error and can't figure it out? The cycle
Hello not sure why Im getting this error. Basically I get it in these
I am not sure why I get this error: Caused by: java.lang.RuntimeException: Deferred binding
I have strange error and not sure how to tackle it without wasting too
I am getting the above error and I'm not sure how to fix it,
I'm getting the error mentioned in the title and not sure what exactly I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.