3 Replies - 1284 Views - Last Post: 27 July 2014 - 12:47 PM Rate Topic: -----

#1 kishanp09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-July 14

Pizza Program for Java Help. Resetting prices does not function.

Posted 26 July 2014 - 09:58 PM

Hi all,
I am making a pizza program and most of the program works. I have made an order button that will output the total price of the order. The reset button that I made makes the JTextArea output $0.00, but if I press the order button to make another order, the program does not add the new amount to zero. For example, if I make an order that totals $5.00, but I want to make another order after resetting, the price for the new order will be added on to the previous order's price. Here is the code. This is a JApplet program. Any help would be greatly appreciated! Can you please tell me why the price will not reset? I have posted the code so that you can try the program for yourself. FYI, this code was made in BlueJ as I am a beginner with Java.



//Author:Kishan Patel
// Sources will be cited soon.
//This is Lab 7, the pizza parlour program.


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

public class Lab7beta extends JApplet  implements
                                                ItemListener, ActionListener
{
    private int intBold = Font.PLAIN;
    private int intItalic = Font.PLAIN;
    private final double smallprice=4.00;
    private final double mediumprice=7.00;
    private final double largeprice=9.00;
    private final double tomatoprice=1.00;
    private final double greenpepperprice=1.00;
    private final double pepperoniprice=1.00;
    private final double sausageprice=1.00;
    private final double greenolivesprice=1.00;
    private final double blackolivesprice=1.00;
    private final double mushroomsprice=1.00;
    private final double onionprice=1.00;
    public String pizza="";
    public    String t1="";
    public    String t2="";
     public   String t3="";
    public    String t4="";
     public   String t5="";
     public   String t6="";
     public   String t7="";
    public    String t8="";

    private Color currentColor = Color.black;
    private String currentFontName ="Courier";
    private JCheckBox topping1,topping2,topping3,topping4,topping5,topping6,topping7,topping8;
    private JRadioButton small,medium,large,delivery,pickup;
   
    
    private ButtonGroup ColorSelectBGroup;
    private ButtonGroup PickupSelectBGroup;
   
    private JButton button, resetbutton;
    private JTextArea text;
    

    private String[] fontNames
                     = {"Dialog"};
    
    //private JLabel  priceDisplayLabel;
     double price=0;
    
                     
   
    public void init() 
    {
        
        Container c = getContentPane();
        c.setLayout(null);

        topping1 = new JCheckBox("Tomato-$1.00");
        topping2 = new JCheckBox("Green Pepper-$1.00");
        topping3 = new JCheckBox ("Pepperoni-$1.00");
        topping4 = new JCheckBox ("Sausage-$1.00");
        topping5 = new JCheckBox ("Green Olives-$1.00");
        topping6 = new JCheckBox ("Black Olives-$1.00");
        topping7 = new JCheckBox ("Mushrooms-$1.00");
        topping8 = new JCheckBox ("Onions-$1.00");
   
        small    = new JRadioButton (" Small-$4.00");
        medium   = new JRadioButton ("Medium-$7.00");
        large    = new JRadioButton ("Large-$9.00");
        delivery = new JRadioButton ("Delivered To Your Doorstep!");
        pickup   = new JRadioButton ("Pick It Up At Our Store!");
        
        text     = new JTextArea  (40,40);
        //priceDisplayLabel = new JLabel ("$0.00");
        //priceDisplayLabel.setFont(new Font("Arial",Font.BOLD,50));
        
        
        
        
        
        
        
         button = new JButton ("order");
        button.addActionListener(this);
        
        
        resetbutton = new JButton ("Reset");
        resetbutton.addActionListener(this);
        
        //Define the checkbox values
        
        
        
        
        
        

        topping1.setSize(150, 40);
        topping2.setSize(150, 40);
        topping3.setSize(150, 40);
        topping4.setSize(150, 40);
        topping5.setSize(150, 40);
        topping6.setSize(150, 40);
        topping7.setSize(150, 40);
        topping8.setSize(150, 40);
        
        small.setSize(120,40);
        medium.setSize(120,40);
        large.setSize(120,40);
        delivery.setSize (200,40);
        pickup.setSize (200,40);
        button.setSize (200,80);
        resetbutton.setSize (200, 80);
        text.setSize   (600, 40);
        //priceDisplayLabel.setSize(200,40);
        

        topping1.setLocation(80, 70);
        topping2.setLocation(80, 100);
        topping3.setLocation(80,130);
        topping4.setLocation(80,160);
        topping5.setLocation(80,190);
        topping6.setLocation(80,220);
        topping7.setLocation(80,250);
        topping8.setLocation(80,280);
       
        small.setLocation(350,70);
        medium.setLocation(350,100);
        large.setLocation(350,130);
        delivery.setLocation (625,70);
        pickup.setLocation (625,100);
        button.setLocation (350,350);
        resetbutton.setLocation (625, 350);
        text.setLocation (625, 450);
        //priceDisplayLabel.setLocation(625,500);
        
        
        topping1.addItemListener(this);
        topping2.addItemListener(this);
        topping3.addItemListener(this);
        topping4.addItemListener(this);
        topping5.addItemListener(this);
        topping6.addItemListener(this);
        topping7.addItemListener(this);
        topping8.addItemListener(this);
        
        small.addItemListener(this);
        medium.addItemListener(this);
        large.addItemListener(this);
        delivery.addItemListener(this);
        pickup.addItemListener(this);
        
        
        
        c.add(topping1);
        c.add(topping2);
        c.add(topping3);
        c.add(topping4);
        c.add(topping5);
        c.add(topping6);
        c.add(topping7);
        c.add(topping8);
        
        
        c.add(small);
        c.add(medium);
        c.add(large);
        c.add(delivery);
        c.add(pickup);
        c.add(button);
        c.add(resetbutton);
        c.add(text);
        //c.add(priceDisplayLabel);
        ColorSelectBGroup = new ButtonGroup();
        ColorSelectBGroup.add(small);
        ColorSelectBGroup.add(medium);
        ColorSelectBGroup.add(large);
        
        PickupSelectBGroup = new ButtonGroup();
        
        PickupSelectBGroup.add(pickup);
        PickupSelectBGroup.add(delivery);
        
        
    }
     
        
    public void paint(Graphics g)
    {
        getContentPane().setBackground(Color.green);
        
        super.paint(g);
        setBackground(Color.green);
        g.setColor(Color.blue);
        g.drawRoundRect(50, 50, 800, 400, 10, 10);
        
        g.drawLine(300, 50, 300, 450);
        g.drawLine(600, 50, 600, 450);

        g.setColor(currentColor);
        g.setFont(new Font(currentFontName,
                           intBold + intItalic, 32));
        g.setColor(Color.red);
        g.drawString("Welcome to Chicago Style Pizza's!", 100, 30);
       
        
    }

    public void itemStateChanged(ItemEvent e)
    {
       
        
        if (small.isSelected()){
                pizza = "Small";
               
            }
            if (medium.isSelected()) {
                
                pizza = "Medium";
                
            }
           if (large.isSelected()) {
                pizza = "Large";
               
            }
           
            if(topping1.isSelected())   {
                t1="Tomato,";   
            }
            else
                t1 = "";  //change the rest
            
            if(topping2.isSelected()){
                t2="Green Pepper,";
            }
            else 
                t2="";
                
            if(topping3.isSelected()){
                t3="Pepperoni,";
            }
            else
                t3 = "";
                
            if(topping4.isSelected()){
                t4="Sausage,";
            }
            else
                t4 = "";
                
            if(topping5.isSelected()){
                t5="Green Olives,";
            }
            else 
                t5="";
                
            if(topping6.isSelected()){
                t6="Black Olives,";
            }
            else 
                t6="";
                
            if(topping7.isSelected()) {
                t7="Mushrooms,";   
            }
            else
                t7="";

            if (topping8.isSelected()) {
                t8="Onions,";
            } 
            else 
            t8 = "";
          
        
       repaint();
    }
    
     private void refreshPrice () {
        
       
       if (small.isSelected()) {
           price+= smallprice *1;
        }

       if (medium.isSelected()) {
           price+= mediumprice *1;
        }
        
        if (large.isSelected()) {
           price+=largeprice *1;
        }
       
        if (topping1.isSelected()) {
            price+= tomatoprice *1;
        }
        
        if (topping2.isSelected()) {
            price+= greenpepperprice*1;
        }
        if (topping3.isSelected()){
            price+=pepperoniprice*1;
        }
        
        if (topping4.isSelected()) {
            price +=sausageprice*1;
        }
        
        if (topping5.isSelected()) {
            price += greenolivesprice*1;
        }
        if (topping6.isSelected())  {
            price+= blackolivesprice*1;
        }
        
        if(topping7.isSelected()) {
            price += mushroomsprice*1;
        }
        
        if(topping8.isSelected()) {
            price += onionprice*1;
        }
       
        
            
       //priceDisplayLabel.setText("$"+price);
      }
      
      
    public void actionPerformed(ActionEvent e) {
        
        refreshPrice();
        
        
        // If order now button is pressed
        if (e.getSource() == button ){
           text.setText("You ordered a " + 
                  pizza + " pizza " + "with: " +t6+ t1+ " " + t2+ " "+t3+ " "+t4+
                   " "+t5+ "\nYour Total cost:$ "+price);    
            
            //JOptionPane.showMessageDialog(this, "You ordered a " + 
                  //pizza + " pizza " + "with: " +t6+ t1+ " " + t2+ " "+t3+ " "+t4+
                  //  " "+t5+ "\nYour Total cost:$ "+price, "Your Order", WIDTH);

            /*JOptionPane.showMessageDialog(this,
                "Thank you for your " + 
                priceDisplayLabel.getText() + "  payment." +
                "\n\nYour pizza includes be ready shortly. Why don't you wait?"+ 
                "\nLog on to our website for any of our other specialties!",
                "Orders Confirmed",
                JOptionPane.INFORMATION_MESSAGE);
                */
               
                refreshPrice();
                
        }
        
      
          if (e.getSource()==resetbutton) {
           
            text.setText("$0.00");
            
         if (small.isSelected()) {
           price+= smallprice *1;
          }

         if (medium.isSelected()) {
           price+= mediumprice *1;
           }
        
        if (large.isSelected()) {
           price+=largeprice *1;
        }
       
        if (topping1.isSelected()) {
            price+= tomatoprice *1;
           }
        
        if (topping2.isSelected()) {
            price+= greenpepperprice*1;
           }
        if (topping3.isSelected()){
            price+=pepperoniprice*1;
           }
        
        if (topping4.isSelected()) {
            price +=sausageprice*1;
           }
        
        if (topping5.isSelected()) {
            price += greenolivesprice*1;
          }
        if (topping6.isSelected())  {
            price+= blackolivesprice*1;
          }
        
        if(topping7.isSelected()) {
            price += mushroomsprice*1;
          }
        
        if(topping8.isSelected()) {
            price += onionprice*1;
          }
          
                PickupSelectBGroup.clearSelection();
                small.setSelected(false);
                medium.setSelected(false);
                large.setSelected(false);
                topping1.setSelected(false);
                topping2.setSelected(false);
                topping3.setSelected(false);
                topping4.setSelected(false);
                topping5.setSelected(false);
                topping6.setSelected(false);
                topping7.setSelected(false);
                topping8.setSelected(false);
               //pizzaText.setText("0");
               
               
            }
            
    }
    
    }


This post has been edited by macosxnerd101: 26 July 2014 - 09:58 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Pizza Program for Java Help. Resetting prices does not function.

#2 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Pizza Program for Java Help. Resetting prices does not function.

Posted 27 July 2014 - 02:15 AM

You never set the price variable declared on line 55 back to 0, only the the label.
Was This Post Helpful? 0
  • +
  • -

#3 kishanp09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-July 14

Re: Pizza Program for Java Help. Resetting prices does not function.

Posted 27 July 2014 - 10:23 AM

I tried setting the price variable to 0 in various different places, but it still does not calculate correctly. FYI, the Jlabel is commented so ignore that for the time being. Currently, I am using a JTextArea to display the order and price. Trying the program out might clear out any confusion. Thank You!
Was This Post Helpful? 0
  • +
  • -

#4 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Pizza Program for Java Help. Resetting prices does not function.

Posted 27 July 2014 - 12:47 PM

The obvious place to reset the price is here:
366          if (e.getSource()==resetbutton) {
367            
368            text.setText("$0.00");

The code here is not commented out as you suggested, that appears later on in the code:
424               //pizzaText.setText("0");

This post has been edited by mike73: 27 July 2014 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1