472,958 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 4631
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

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

Similar topics

13
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was...
3
by: Andrew Jocelyn | last post by:
Hi I have a simple ASP.NET web page with a single label control: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb" Inherits="proj.TheTime" EnableViewState="false"...
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
5
by: Richard Mintz | last post by:
Hi, I am trying to deploy a C# application to a Windows 2000 machine. When I try to run the app after the install i get a runtime error. the setup works on both XP and windows 2003 server...
8
by: Martin Randall | last post by:
Hi, I'm trying to get a simple dataview test page working on my hosted site. The hosted site runs ASP.Net 2.0, no problem. The test page works on my development machine, no problem... but when I...
5
by: awasthi.ashish | last post by:
Hi friends, I am writing a simple program to convert an IP address from a hexadecimal form to dotted-notation form. However, for some reason I am having problem getting it to work.Here it is: ...
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
4
by: Axel Dahmen | last post by:
Hi, I've created a few simple console tools. No .NET used at all, just STL. Now I want to just XCOPY them to another machine. But running them there yields a couple of errors in the application...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.