• Post Reply Bookmark Topic Watch Topic
  • New Topic

Little help with a vending machine program  RSS feed

 
Carsten Joergensen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A have tried to make a vending machine program for my beginners programming class. But i have some small problems that i hope somebody could help me with.
First of all, if the amount paid if less than the Price of the product the program has to write Not enough. Return "amount". I can't make it do that.
Second i know that my program could probably be shortend a lot but i can't figure out how to collect the program in methods and classes. So i would really like some suggestion.

Here is what i have

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you mentioned, using multiple classes and methods is the best way to go about this (assuming you have learned about them).

The main object in your problem is a vending machine, so you should definitely have a VendingMachine class. It wouldn't be a bad idea at all to make classes for the other objects (e.g. Coin, Drink) but it's not necessary because a coin is basically an integer and a drink is basically a String (in the context of the project).

So for the VendingMachine class, make two lists: the types of things a vending machine knows (e.g. prices, etc.) and the types of things a vending machine does (e.g. displays prices, etc.).

The things a vending machine knows should be stored in fields, and then things the vending machine does can be written as methods. Then, from your main method (which can also reside in the VendingMachine class), you would create a VendingMachine object and interact with its methods (which use info from the fields) to buy a drink.

If you have any questions at all, feel free to ask.
 
Carsten Joergensen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem is i don't understand how to arrange the program in the correct way. I have tried for a week now. I don't understand how to write the code that collects all the three coin counting etc...

This is were i hope for an example

And also i need the program to return to the choice menu if selct is typed and amount is less than the price of the product. I can't make it do that either.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have been given good advice. More good advice, but I am afraid you are not going to like it.
  • 1: Find all the code you have already written and delete it.
  • 2: Turn your computer off. Pull the mains lead out of the wall.
  • 3: Go and sit down for at least half an hour and drink some coffee. If you are getting nervous, that half‑hour will be time well spent.
  • 4: Don't turn your computer on again.
  • 5: Do what Joel Christophel has already told you. Write down all the things a vending machine “knows” and all the things it does.
  • 6: Whenever you come across a noun like drink consider whether you need a enw entity. Write down what drinks “know” and what they do in the vending machine.
  • 7: Keep it simple. Don't create too many entities. Drink, Chocolate, Biscuit, etc., might be entities, or the might simply be different kinds of Product.
  • 8: For all the things each of those things does, write down very simply how it is done, in plain English (or other language).
  • 9: At this stage, if you have anything which looks like Java code, throw it away and start again.
  • 10: Now you might be in a position to turn your computer back on.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!