468,121 Members | 1,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,121 developers. It's quick & easy.

Simple Coke Machine problem

Hi,I want to create a coke machine that distributes cans with the cost of one token. All the cans in the coke machine coast the same. When a token is inserted, one can drops out. The coke machine should keep track of both cans and tokens count.

The problem I am having right now is finding the correct methods, especially one that allows one token to have one can come out. This is frustrating. Here is my program so far.



Expand|Select|Wrap|Line Numbers
  1. public class CokeMachine
  2. {
  3.     int number_tokens;
  4.     int number_cans; 
  5.     int token; 
  6.     int can; 
  7.  
  8.     public CokeMachine()
  9.     {
  10.         number_cans = 50;
  11.     }
  12.  
  13.  
  14.         public void insertToken(int number_tokens)
  15.         {
  16.  
  17.             token += number_tokens; // takes token, then adds number of tokens to token, then resign it back to token
  18.  
  19.         }
  20.  
  21.  
  22.  
  23. }
  24.  
Jan 28 '09 #1
8 4394
HxRLxY
23
If I understand you right, you want to decrease the coke count every time you increase the token count. So try this.

Expand|Select|Wrap|Line Numbers
  1. public void insertToken (int number_tokens)
  2. {
  3.      if (number_cans - number_tokens < 0) //not enough cokes left
  4.      {
  5.           System.out.println (/*some error message*/);  
  6.      }
  7.      else
  8.      {
  9.           tokens -= number_tokens;
  10.           number_cans -= number_tokens;
  11.      }
  12. }
  13.  
Jan 28 '09 #2
chaarmann
785 Expert 512MB
There should be a plus instead of a minus:
tokens += number_tokens;
Jan 28 '09 #3
Yes that is exactly what I want to do. Ill try it out
Jan 28 '09 #4
Ok I have compiled my program with the method you gave me. It displays no syntax errors, so everything seems to be working correctly. Here is the program with the methods you helped me with. From what I see from this prgram is that everytime a token is inserted, that token is being deducted from the number of tokens count. Then the number of cans is being deducted when the number of tokens is being deducted? Can you try to explain what is going on and see if I am understading the code correctly?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. public class CokeMachine 
  4.     int number_tokens; 
  5.     int number_cans;  
  6.     int tokens;  
  7.     int can;  
  8.  
  9.     public CokeMachine() 
  10.     { 
  11.         number_cans = 50; 
  12.     } 
  13.  
  14.  
  15.         public void insertToken(int number_tokens) 
  16.         { 
  17.  
  18.             tokens -= number_tokens; 
  19.           number_cans -= number_tokens; // takes token, then subtracts one token from number of tokens, then resign it back to token 
  20.  
  21.         } 
  22.  
  23.  
  24.  
  25.  
  26.  
Jan 28 '09 #5
chaarmann
785 Expert 512MB
@falconsx23
It doesn't make sense to deduct the tokens. That's what the machine gets, so we need to add. The cans are what the machine gives away, so we need to subtract. Look what he wrote:
@HxRLxY
Did you notice the word "increase"? And look what I wrote in my previous post:.
@chaarmann
It seems it was just a mistyping in his program.

Anyway, you don't need the number_tokens variable (redundant information), you can always compute the number of tokens with "50 - number_cans". I assume that the guy who refills the machine also puts out all the tokens.

Remark:
To have a variable "number_tokens" makes only sense when the machine malfunctions and it takes the token without putting out the coke! Then you can check if the customer's complaint is true or not.
Jan 29 '09 #6
Sorry it took me so long to reply, but I completed the program. You are right it was not really making sense along with the mis spelling. This is what I did.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class CokeMachine
  3. {
  4.  
  5.     int number_cans; 
  6.     int tokens; 
  7.  
  8.  
  9.     public CokeMachine()
  10.     {
  11.         number_cans = 50; //already holds 50 cokes
  12.  
  13.     }
  14.  
  15.     public CokeMachine(int num)
  16.     {
  17.         number_cans = num; 
  18.     }
  19.  
  20.         public void insertToken(int num)
  21.         {
  22.             tokens += 3;
  23.             number_cans -= num; // takes token, then adds number of tokens to token, then resign it back to token
  24.             System.out.println("The number of cans remaining are " + number_cans);
  25.             System.out.println("The number of tokens are " + tokens);
  26.             System.out.println("You get " + tokens + " cokes ");
  27.  
  28.         }
  29.         public void insertcans(int num)
  30.         {
  31.           number_cans += num; 
  32.         }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. }
  40.  
Feb 3 '09 #7
JosAH
11,448 Expert 8TB
Line #22 contains a typo or some testing leftover. If you insert 'num' tokens you get 'num' cans of coke if possible; you get less if the vending machine contains less than 'num' cans of coke.

kind regards,

Jos
Feb 3 '09 #8
o ok I see what you mean. Thanks alot
Feb 4 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by Michael B Allen | last post: by
3 posts views Thread by Andrew Jocelyn | last post: by
7 posts views Thread by abcd | last post: by
5 posts views Thread by awasthi.ashish | last post: by
30 posts views Thread by galiorenye | last post: by
18 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.