Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math "equations" not working properly

Tags:

java

math

swing

Has anybody ever used those machines at a gas station or grocery store where you get money for donating your recyclables? Well, I wanted to make a virtual one of those and so far everything's okay until I had to do some math. I'm only 13, so this part was pretty tricky even though I thought it was gonna be simple. I need the recyclable type's value times the amount and then added to the total money. But instead of adding it to the total money it seems to just change the total to the recent value I added. Let's say I add 2 cans, which is 10 cents, and then I add one more can after that, instead of having 15 cents total, I just have 5 cents. Hopefully you understand. I would also like some constructive criticism about my code. I know it's not the best, but I just started learning java, so any help would be lovely.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class Machine {
    static JLabel label;
    static JComboBox typeList;
    static JComboBox amountList;
    public static void GUI(){

        JFrame frame = new JFrame("Recyclables Machine");
        frame.setVisible(true);
        frame.setSize(300,125);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.add(panel);

        Integer[] amounts = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
        amountList = new JComboBox(amounts);
        panel.add(amountList);

        String[] types = {"Choose Recycable Type","Plastic Bottle","Can","2 Liter","Glass Bottle"};
        typeList = new JComboBox(types);
        panel.add(typeList);

        JButton button = new JButton("Add");
        panel.add(button);

        label = new JLabel("Total Money: 0 cents");
        panel.add(label);

        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                gettinItDone();
            }
        });
    }
    public static void gettinItDone(){
        String type = (String)typeList.getSelectedItem();
        int amount = (int)amountList.getSelectedItem();
        int money = 0;
        int temp = 0;

        if(type.equals("Plastic Bottle")){
            temp = 5 * amount;
            money = temp + money;
            label.setText("Total Money: "+ money +" cents");
        }else{
            if(type.equals("Can")){
                temp = 5 * amount;
                money = temp + money;
                label.setText("Total Money: "+ money +" cents");
            }else{
                if(type.equals("2 Liter")){
                    temp = 10 * amount;
                    money = temp + money;
                    label.setText("Total Money: "+ money +" cents");
                }else{
                    if(type.equals("Glass Bottle")){
                        temp = 10 * amount;
                        money = temp + money;
                        label.setText("Total Money: "+ money +" cents");
                    }else{
                        JOptionPane.showMessageDialog(null,"Invalid Recyclable Type", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }
    }
}
like image 332
Wumbo4Dayz Avatar asked Oct 01 '13 19:10

Wumbo4Dayz


1 Answers

The scope of your "money" variable is only active when the event listener fires.

button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            gettinItDone();
        }
    });

You need to store your money outside of the scope of the #getIniitDone method.

like image 182
codethulhu Avatar answered Oct 20 '22 02:10

codethulhu