hello everybody i'm having a headache with this buggy code
every thing its working the way i want,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); } }
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