• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error with math equations  RSS feed

 
Greenhorn
Posts: 2
Chrome Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. This is one of those things that make sense in your mind, but just won't work when you start turning it into code. 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.

I'll try my best to post an SSCCE, but it will my first attempt.

Machine Class (Handles the GUI and ActionListener):



Not sure what to name this class, but it actually executes everything when the actionlistener is fired:

 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right: Each instance of NotSureWhatToNameThis has its own copy of totalMoney. There are two ways around that: either ensure that there is only single, global copy of totalMoney (by declaring it to be static), or keeping track of the total in your Machine class (by having the calculateTotalAmount method return the subtotal, and then adding it to a variable in the Machine class).
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you forget the GUI for the time being. Create a Recycler class, which has a public interface which the GUI can use later.
Create a Recyclable class (or interface) with a value. Get that working. Test it. You may want this sort of code:Keep going untl you are sure about the workings of the Recyclable objects. Try to “break” the code with incorrect inputs.

Work out how to put recyclable objects into the recycler object. You might want a List, or a RecyclingBag objectTest it again.

In the example you showed, you can try changing the = operator to one of the compound operators. don't use a compound operator on a datatype “narrower” than 32 bits.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I intended the Recycling Bag class to contain one type of recyclable only.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Chuck Crocicchia
Greenhorn
Posts: 2
Chrome Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wonderful, thank you guys for the great answers! And thanks Campbell Ritchie showing a great approach to problem solving. My mama's gonna be so proud when I finish this!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!