• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error with math equations

 
Greenhorn
Posts: 2
Windows XP Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I intended the Recycling Bag class to contain one type of recyclable only.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Chuck Crocicchia
Greenhorn
Posts: 2
Windows XP Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic