Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: buggy class

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default buggy class

    hello everybody i'm having a headache with this buggy code


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class JPizza extends JFrame implements ItemListener
    {
    	final int SMALL = 7, MEDIUM = 9, LARGE = 11, X_LARGE = 14;
    	//final int[] sizesPrices = {7,9,11,14};
    	final int TOOPING_PRICE = 1;
    	int totalPrice = SMALL;
     
    	JCheckBox cheeseBox = new JCheckBox("Cheese", true);
    	JCheckBox PepperoniBox = new JCheckBox("Pepperoni", false);
    	JCheckBox BlackOlivesBox = new JCheckBox("Black Olives", false);
    	JCheckBox MushroomsBox = new JCheckBox("Mushrooms", false);
    	JCheckBox PineappleBox = new JCheckBox("Pineapple", false);
    	JCheckBox HamBox = new JCheckBox("Ham", false);
     
     
    	JLabel sizelbl = new JLabel("Select the size for your pizza");
    	String[] size = { "Small $" + SMALL , "Medium $" + MEDIUM,
    							"Large $" + LARGE, "X Large $" + X_LARGE };
    	JComboBox sizeBox = new JComboBox(size);
     
    	JTextField totPriceTxt = new JTextField(10);
    	JLabel optionExplainLabel = new JLabel("The price For each topping is $" +TOOPING_PRICE);
    	JLabel optionExplainLabel2 = new JLabel("Theres no charge for cheese");
     
    	JLabel selectTopinglbl = new JLabel("please select your toppings");
    	JLabel ePrice = new JLabel("The price for you pizza is");
     
    	Container con;
    	JPanel panel = new JPanel();
    	//this is the contructor that builds the frame
    	public JPizza()
    	{
    		super("PIZZA MAKER");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		con = getContentPane();
    		con.add(panel);
    		panel.add(sizelbl);
    		panel.add(sizeBox);
    		panel.add(optionExplainLabel);
    		panel.add(optionExplainLabel2);
    		panel.add(selectTopinglbl);
    		panel.add(cheeseBox);
    		panel.add(PepperoniBox);
    		panel.add(BlackOlivesBox);
    		panel.add(MushroomsBox);
    		panel.add(PineappleBox);
    		panel.add(HamBox);
    		panel.add(ePrice);
    		panel.add(totPriceTxt);
    		totPriceTxt.setText("$" + totalPrice);
    		sizeBox.addItemListener(this);
    		PepperoniBox.addItemListener(this);
    		BlackOlivesBox.addItemListener(this);
    		MushroomsBox.addItemListener(this);
    		PineappleBox.addItemListener(this);
    		HamBox.addItemListener(this);
    	}
    	// this make the frame
    	public static void main(String[] args)
    	{
    		final int WIDHT = 280;
    		final int HEIGTH = 250;
    		JPizza aFrame = new JPizza();
    		aFrame.setSize(WIDHT,HEIGTH);
    		aFrame.setVisible(true);
    	}
    	//this will listen to everything
    	public void itemStateChanged(ItemEvent event)
    	{
    		Object source = event.getSource();
    		int select = event.getStateChange();
    		// this code listen to the comboBox
    		if(source == sizeBox)
    		{
    			int sizePrice;
    			if ((sizeBox.getSelectedIndex()) == 0)
    				 sizePrice = 7;
    			else if((sizeBox.getSelectedIndex()) == 1)
    				sizePrice = 9;
    			else if((sizeBox.getSelectedIndex()) == 2)
    				sizePrice = 11;
    			else //if((sizeBox.getSelectedIndex()) == 3)
    				sizePrice = 14;
    			totalPrice = sizePrice;
    		}
     
    		//this will listen to the checkboxes
    		else if (source == PepperoniBox)
    		{
    			if (select == ItemEvent.SELECTED)
    				totalPrice += TOOPING_PRICE;
    			else
    				totalPrice -=TOOPING_PRICE;
    		}
    		else if (source == BlackOlivesBox)
    		{
    			if (select == ItemEvent.SELECTED)
    				totalPrice += TOOPING_PRICE;
    			else
    				totalPrice -=TOOPING_PRICE;
    		}
    		else if (source == MushroomsBox)
    		{
    			if (select == ItemEvent.SELECTED)
    				totalPrice += TOOPING_PRICE;
    			else
    				totalPrice -=TOOPING_PRICE;
    		}
    		else if (source == PineappleBox)
    		{
    			if (select == ItemEvent.SELECTED)
    				totalPrice += TOOPING_PRICE;
    			else
    				totalPrice -=TOOPING_PRICE;
    		}
    		else //(source == HamBox)
    		{
    			if (select == ItemEvent.SELECTED)
    				totalPrice += TOOPING_PRICE;
    			else
    				totalPrice -=TOOPING_PRICE;
    		}
    		//this updates the final price
    		totPriceTxt.setText("$" + totalPrice);
    	}
    }
    every thing its working the way i want,
    but if i select a checkbox first and then i select a from the combobox
    it wont give me the correct result ...

    e.g.
    if i select "small $7" total is 7
    then i select "pepperoni" total its fine "8",
    now if i change "small $7" to "large $9" i would expect the total be "10" having the "pepperoni" selected,
    but it is not the total is "9"

    how can i solve this bug any suggestion?
    thanks in advance


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: buggy class

    Hi,
    this is the problem:
    when you change "small $7" to "large $9", the event, that is fired, is on the size
    select. And the totalprice is set to sizePrice (=> $9).
    Others if (source == PepperoniBox) or if (source == BlackOlivesBox) etc
    are not fired so $1 for pepperoni is not added and the totalprice remains $9 .

    Why in your code, you remove TOOPING_PRICE if
    unselect an ingredient and if you change the pizza's size
    you don't remove the different of price?

    Bye

Similar Threads

  1. any ways to run a class from another class?
    By javanub:( in forum Java Theory & Questions
    Replies: 3
    Last Post: May 9th, 2010, 06:57 AM
  2. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  3. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  4. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM
  5. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 10:18 AM