• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vending Machine Simulator  RSS feed

 
Jaime Alnwick
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I am struggling a bit with a current project. In the code below, the user inputs a number corresponding to a currency type (i.e. $10, $1, $0.25, etc). Currency types are in array "currency." I was wondering if someone might be able to give me some pointers on how I might be able to take the currency type numbers entered by the user (which are added to an array) and equate them with the value of the currency. Then the plan is to sum the values to get the total amount of money entered. Let me know if you'd like to see the rest of the code.

 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaime Alnwick wrote:... Then the plan is to sum the values to get the total amount of money entered. Let me know if you'd like to see the rest of the code.


Well, to start off with you have 2 types of currency objects, I would simplify that to 1, if it is not a requirement of the assignment, and I would make what is being entered by the user a String and use it as a token for a lookup in your currency object where you would have the value stored.  That way no fuss no muss on conversions and significant digits, rounding, or truncation while trying to match.

Les
 
Jaime Alnwick
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote: I would make what is being entered by the user a String and use it as a token for a lookup in your currency object where you would have the value stored.  That way no fuss no muss on conversions and significant digits, rounding, or truncation while trying to match.


Thank you Les. Not really familiar with how to do this (we haven't covered it in class yet) but I will look into it. Thanks again!
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you expect the user to type in for their currency types?
The name? actual value?  Or a number "0-6" which represents the option in the list.
i.e. to put in a Quarter, would I type:  "Quarter", "0.25", or "3"  (because the Quarter is the option at index 3 in the array)


Also I can see some if statements in your code looking at the item selected.
That doesn't look like the right approach for Object oriented code to me.
Instead you should be using the InventoryItem object and its methods/properties.

something like



What are those if statements based on the item meant to do?

 
Jaime Alnwick
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The available currency denominations are listed along with the numbers 0-6 and the user enters the number representing an option in the list. We were shown an example program output and this is how it was done in the example.

Sorry, the code is not complete. The if statements were just there as placeholders. They are meant to calculate the change. Here is my latest code for the VendingMachineSimulator class but it is still not complete. I probably do need to work on making it more Object oriented.

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid I don't like the multiple ifs. As you have been told, you should have an object which encapsulates its description and its price; those values don't belong in another method.
Also, your use of floating‑point arithmetic is error‑prone. It is only a matter of time until you think you have 10¢ somewhere and you actually have 9.99999999999999999something¢. Or 10.00000000000000001something¢. Then you will get a wrong output. That is an unavoidable problem with floating‑point arithmetic. There are at least two ways to avoid that:-
  • 1: Use integer arithmetic and denominate all prices in ¢.
  • 2: Use decimal arithmetic, which requires you use the class described in this discussion. Scroll down to the bottom of the page because the example code is enhanced later on.
  •  
    Carey Brown
    Saloon Keeper
    Posts: 3310
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One of my pet peeves. Method names should reflect what they do.
    In this case you can remove the comment.
     
    J. Kevin Robbins
    Bartender
    Posts: 1801
    28
    Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I realize that you are probably up against a class deadline and don't have time to refactor this code, but I feel compelled to  point out that this is not Object Oriented coding. You have only one class, there is too much code in the main method, and I see the "static" keyword all over the  place.

    My suggestion is this, after you've completed this to your  instructors satisfaction, come back here and let the kind folks of the Ranch walk you through refactoring this into a real OOP program. You will learn a great deal.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!