dimanche 18 septembre 2016

How do I prevent my if statement from changing the value when a check box or radio button is deselected

I'm currently working on a simple program to learn how to make a GUI. The user has the ability to check certain option of uncheck them if they no longer wish to have them. However when the box is deselected and another selection is made it will still count my else statements and decrease the value accordingly.

the following code is one such example, even if i'm doing nothing with it and I click something else the value is still deducted from the variable "itemTotal" I assume it's because an even is triggered each time and action is taken but this isnt something I want to happen

if(cheesebx.isSelected())
        {
            if(itemTotal == 0)
            {
                JOptionPane.showMessageDialog(null, "Please select Single, or Double burger first");
                cheesebx.setSelected(false);
            }
            else
            {
            itemTotal += cheese;
            }
        }
        else if (!cheesebx.isSelected())
        {
            itemTotal -= cheese;
        }

I have noticed that the following bit of code will also deduct value from the total box if something else is selected.

if(single_radio.isSelected())
        {
            if(itemTotal != 0 && itemTotal != single)
            {
                itemTotal -= two;

                itemTotal += single;
            }

            if(itemTotal == 0)
            {
                itemTotal += single;
            }


        }

Here is my full code.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week3lab;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class posGUI extends javax.swing.JFrame implements ActionListener {

    public double single = 3.50;
    public double two = 4.75;
    public double cheese = 0.50;
    public double bacon  = 1.25;
    public double meal   = 4.00;

    public double orderTotal = 0;
    public double itemTotal  = 0;

    public int quantity = 1;


    /**
     * Creates new form posGUI
     */
    public posGUI() {
        initComponents();
        exitbtn.addActionListener(this);
        addbtn.addActionListener(this);
        newbtn.addActionListener(this);
        nextbtn.addActionListener(this);

        single_radio.addActionListener(this);
        double_radio.addActionListener(this);

        cheesebx.addActionListener(this);
        baconbx.addActionListener(this);

        mealbx.addActionListener(this);

        pricetxt.setText(Double.toString(itemTotal));
        totaltxt.setText(Integer.toString(quantity));
    }



    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        single_radio = new javax.swing.JRadioButton();
        double_radio = new javax.swing.JRadioButton();
        cheesebx = new javax.swing.JCheckBox();
        baconbx = new javax.swing.JCheckBox();
        mealbx = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        pricetxt = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        quantitytxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        totaltxt = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ordertxt = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        exitbtn = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        addbtn = new javax.swing.JMenuItem();
        nextbtn = new javax.swing.JMenuItem();
        newbtn = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        buttonGroup1.add(single_radio);
        single_radio.setText("Single Burger");

        buttonGroup1.add(double_radio);
        double_radio.setText("Double Burger");

        cheesebx.setText("Add Cheese");

        baconbx.setText("Add Bacon");

        mealbx.setText("Make it a meal");

        jLabel1.setText("Item Price");

        jLabel2.setText("YOUR ORDER");

        jLabel3.setText("Quantity");

        jLabel4.setText("Order Total");

        ordertxt.setColumns(20);
        ordertxt.setRows(5);
        jScrollPane1.setViewportView(ordertxt);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(mealbx)
                            .addComponent(double_radio)
                            .addComponent(cheesebx)
                            .addComponent(baconbx))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(single_radio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addGap(92, 92, 92))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(pricetxt)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jLabel3)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(quantitytxt)
                            .addComponent(totaltxt, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                        .addGap(22, 22, 22))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(single_radio)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(double_radio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cheesebx)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(baconbx)
                        .addGap(18, 18, 18)
                        .addComponent(mealbx))
                    .addComponent(jScrollPane1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(quantitytxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(pricetxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(totaltxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(32, 32, 32))
        );

        jMenu1.setText("File");

        exitbtn.setText("Exit");
        jMenu1.add(exitbtn);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Order");

        addbtn.setText("Add to Order");
        jMenu2.add(addbtn);

        nextbtn.setText("Clear for Next Item");
        jMenu2.add(nextbtn);

        newbtn.setText("New Order");
        jMenu2.add(newbtn);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */


   @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == exitbtn)
        {
            System.exit(0);
        }

        if(e.getSource() == nextbtn)
        {
            ordertxt.append(Integer.toString(quantity) + "");

            if(single_radio.isSelected())
            {
                ordertxt.append("single ");
            }

            if (cheesebx.isSelected())
            {
                ordertxt.append("cheese ");
            }

            if (baconbx.isSelected())
            {
                ordertxt.append("bacon ");
            }

            if(mealbx.isSelected())
            {
                ordertxt.append("meal ");
            }

            orderTotal += itemTotal * quantity;

            ordertxt.append("at " + Double.toString(itemTotal) + "" + "each");

            totaltxt.setText(Double.toString(orderTotal));
        }

        if(single_radio.isSelected())
        {
            if(itemTotal != 0 && itemTotal != single)
            {
                itemTotal -= two;

                itemTotal += single;
            }

            if(itemTotal == 0)
            {
                itemTotal += single;
            }


        }

        if(double_radio.isSelected())
        {
            if(itemTotal != 0 && itemTotal != two)
            {
                itemTotal -= single;

                itemTotal += two;
            }

            if(itemTotal == 0)
            {
                itemTotal += two;
            }


        }

        if(cheesebx.isSelected())
        {
            if(itemTotal == 0)
            {
                JOptionPane.showMessageDialog(null, "Please select Single, or Double burger first");
                cheesebx.setSelected(false);
            }
            else
            {
            itemTotal += cheese;
            }


        }

        if(baconbx.isSelected())
        {
            if(itemTotal == 0)
            {
                JOptionPane.showMessageDialog(null, "Please select Single, or Double burger first");
                baconbx.setSelected(false);
            }
            else
            {
                itemTotal += bacon;
            }


        }

        if(mealbx.isSelected())
        {    
            if(itemTotal == 0)
            {
                JOptionPane.showMessageDialog(null, "Please select Single, or Double burger first");
                mealbx.setSelected(false);
            }
            else
            {
                itemTotal += meal;
            }
        }

        pricetxt.setText(Double.toString(itemTotal));
    }


    // Variables declaration - do not modify                     
    private javax.swing.JMenuItem addbtn;
    private javax.swing.JCheckBox baconbx;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox cheesebx;
    private javax.swing.JRadioButton double_radio;
    private javax.swing.JMenuItem exitbtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JCheckBox mealbx;
    private javax.swing.JMenuItem newbtn;
    private javax.swing.JMenuItem nextbtn;
    private javax.swing.JTextArea ordertxt;
    private javax.swing.JTextField pricetxt;
    private javax.swing.JTextField quantitytxt;
    private javax.swing.JRadioButton single_radio;
    private javax.swing.JTextField totaltxt;
    // End of variables declaration                   
}




Aucun commentaire:

Enregistrer un commentaire