Using Buttons to count (functions)???
Dear friends,
I don't know, I cannot seem to figure this out, should i use arrays,,,or
what? Here is my problem, the user is allowed to press the button as many
times as they want, and I have to tally up the amounts.
My code is wild I know,, learning, and probablly going about it the wrong
way.
But it is the only way I know how to make it work,
Perhaps you can help me, please see my code:
class ButtonHandler implements ActionListener {
// moved to global scope and not block scope.
int num1, num2, num3, num4, total;
public int addMoney(){
total = num1 + num2 + num3 + num4;
return (total);
//infoMessage.setText(total);
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == bttnItem1) {
System.out.println("\nUser has chosen Chocolate.");
infoMessage.setText("User chosen Chocolate.");
} else if (evt.getSource() == bttnItem2) {
System.out.println("\nUser has chosen Peanuts.");
infoMessage.setText("User chosen Peanuts.");
} else if (evt.getSource() == bttnItem3) {
System.out.println("\nUser has chosen Candy.");
infoMessage.setText("User chosen Candy.");
} else if (evt.getSource() == bttnItem4) {
System.out.println("\nUser has chosen Soda Pop.");
infoMessage.setText("User chosen Soda Pop.");
} else if (evt.getSource() == bttnFive) {
System.out.println("\nUser has added 5 cents.");
moneyAmount.setText("5");
num1 = 5;
addMoney();
} else if (evt.getSource() == bttnTen) {
System.out.println("\nUser has added 10 cents.");
moneyAmount.setText("10");
num2 = 10;
addMoney();
}
}
I am just trying to add up the buttons clicked, how shall I go about this??
Any ideas would be greatly appreciated, as I am lost and need some advice,
professional pointers to how I should do this?
Please help?
Thanks
Brad