Has anyone ever used these 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 them, and so far everything is fine until I had to do math. I am only 13, so this part was quite complicated, although I thought that everything would be simple. I need a value of the reuse type multiplied by the quantity, and then added to the total amount of money. But instead of adding it to the total money, it seems to have simply changed the final result to the recent value that I added. Let's say I add 2 jars, which is 10 cents, and then I add another jar after that, instead of having 15 cents in total, I have only 5 cents. I hope you understand. I would also like to receive constructive criticism of my code. I know this is not the best, but I just started learning Java, so any help would be great.
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); } } } } } }