• Post Reply Bookmark Topic Watch Topic
  • New Topic

Transferring data from one method to another  RSS feed

 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assignment: create a soda vending machine & use methods addMoney, vend, & restock.

I'm good with addMoney & vend (except for working out BigDecimals, as recommended by Campbell Ritchie in my last thread), but I can't figure out how to make restock work. What I'm trying to do, as you no doubt see, is increase the number of each item that should be restocked every time one of that item is purchased. But from what I can tell, that isn't happening. I threw in a System.out.println to figure out if it was adding up the depletions & found out it wasn't. I tried various ways of making it work but was unsuccessful. Can someone tell me how to connect the decreasing sodaCount with the restock method? BTW, the only method I call from the main is vend. As my book says, I think recursively. Thank you.

 
Carey Brown
Bartender
Posts: 2998
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't declare sodaCount as static. The way you have it means that no matter how many flavors you have they will all share the same sodaCount.
These will increment the same variable.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there are several things wrong with the code, but let's start with the most obvious. When everything is static, this is a red flag that something is wrong. When you declare something static, that means it belongs to the Class, not any object of the class. I didn't see anything that needs to be static in your code. What I suspect happened is you are calling vend() from main without a Soda object. Create an object with new.

Second, you are calling vend() recursively; the question is, why? You should probably have a driver method with a loop in it.

Next, I see you are calling vend() with four (and sometimes five!) Sodas as parameters. You don't want to determine the type of soda just by its position as a parameter. What if the vendor starts carrying another type of soda?

See if you can correct these things -- or maybe start again be writing down what your vendor should do a programming afterwards.
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to get precision with money, as long as you don't get into the realms of millions of dollars: work out all your money in cents/pence and use integer arithmetic.

I am afraid there is another red flag in that code. You only seem to have one class. You should have several classes to get object‑oriented programming. At least a Soda class, and SodaMachine class and a driver class; I might call it SodaDemo. Also consider how you are restocking the machine. I would not have a count as a feature of a Soda class. Put the count somewhere else. You might want another class to encapsulate soda and counts, between the Soda class and the SodaMachine class. And don't stop selling when your count is < 1. Stop when it is == 0. Means the same but easier to read and more obvious meaning when you read it out.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*deep breath* OK...

Campbell (people with first & last names that can be reversed run the risk of having me address them by the wrong one): I looked up driver class, & I assume that that should be the class from which everything happens. But why do I need a SodaMachine class? If all my sodas are in the soda class, what am I going to make with the SodaMachine class?

Also, I looked for various ways to get entire coins & not pieces of coins returned as change, but the one that looked like the easiest & most practical (I think it was Math.round(value * 100)/100), but it doesn't work with numbers that are just fractions. (Soda price is $.65.)

Knute: I couldn't remember what "static" meant, but the code worked with it but flagged parts of the code without it, so I put it in. I just now got rid of the statics, & only the variable "credit" still caused red flags. So I got rid of it & just declared it when I needed it, & that fixed that problem.

I do have five objects in the main class, but I also call the vend method with Soda. Do I need to call it five times, once for each object? Oh--& the reason that sometimes it looks like 4 & sometimes it looks like 5 is that 5 made the lines too long, so I cut it down to 4 to paste here but apparently forgot to cut out a Mello Yello. At any rate, calling vend 5 times still isn't going to make anything happen in restock, correct?

The reason for all the parameters is to pass them so that I can use the variable reference everywhere I need to. Like, if I don't use cocaCola as a parameter in a method, Netbeans tells me it's not recognized in that method. So I think I'm missing something you're asking. And I don't know why adding another soda would be a problem. I'd just create an object called "cherryCoke" & put it after mrPibb everywhere the objects occur in the program.



Does any of this address my problem of getting the statements in restock to work?

Thanks!
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:. . . Campbell (people with first & last names that can be reversed run the risk of having me address them by the wrong one): . . .
I get called all sorts of things. As long as you don't spell it wrongly
But why do I need a SodaMachine class? If all my sodas are in the soda class, what am I going to make with the SodaMachine class?
Because you have a Soda and you have a machine which vends it. Believe it or not, you will find it much easier to program if you separate the concerns. One of the reasons you are having problems is because you are trying to do too much all at once. Divide and conquer. Your Soda class need only encapsulate name and price. You can have a SodaStock class which might have Soda and count as fields. Give it a toString method which prints something like this:-
“SodaStock: Diet Coke 12”
to mean you have 12 cans of Diet Coke. (Or similar)
The SodaMachine object will take the money and sell the can and alter the amounts in stock.
. . . Math.round(value * 100)/100), but it doesn't work with numbers that are just fractions. (Soda price is $.65.) . . .
That goes to show you how good floating‑point arithmetic is for money.As Mr Punch would say, “That's the way to do it!”
Thanks!
You're welcome
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, before I start messing with a program that does everything it's supposed to do with one exception, I need to know how to make the restock method kick into action. Invisible action, but action nevertheless. I think my professor will know if it's working even if the work is invisible. Then I'll copy the whole thing & start trying to divide & not be conquered.

Thanks!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you post your current code and the code that drives it, I will take a look at it. But have you tried debugging it? Put some System.out.printlns in and see if your assumptions are correct.
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:. . . before I start . . . I need to know how to make the restock method kick into action. . . .
I think you are mistaken there. I think you do not need to know anything about restocking when you are selling. If you go to a real soda machine and pay your 65¢ for a can of Cola, you are not interested in how you refill the machine. Do you ask, “What time of day do they refill this machine? Do they use a key or a password to open it? Do they fill it from the front or the back? How many cans do they fill at a time?” I bet you don't. All you think is, “Am I going to get a can of Cola before I die of thirst or is the machine going to keep my money regardless?” Just as in real life, buying soda and restocking the machine are independent of each other. When restocking you want to know the capacity of the machine (e.g. this machine can hold 36 cans of Cola) and how many it contains at the moment.

You might want to do something like printing a message like “Please restock machine” whenever any of the drinks falls below 6 however.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Real life doesn't matter. This is school! I have to do what I'm told, not what makes sense.

I'd be thrilled if I could print out that message. But that brings me back to the problem: how do I make the restock method work?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us your updated code and how you run it.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I think I'm failing to communicate something here. I'm taking this class for fun. I'm a linguist, & programming languages are a lot like natural languages. So I do want to learn how to do it correctly, & I love working out programming problems. However, right now I'm taking a class.

Below is the assignment:

Your task in this assignment is to write a simple class definition for a vending machine. The class should have:
Attributes

Soda count
Money credit (how much money has been inserted)
Methods

Add money
Vend
Restock
Constructor
For simplicity sake, you may assume that the machine can hold up to 50 of one type of soda; you may also assume that all sodas cost $0.65. When adding money, simply take in a floating-point number representing how much money to add (i.e. you do not have to manage different coins).

Be sure to make sure that enough money was added to the machine before a soda is vended. Also, when a soda is vended, return the appropriate change (i.e. if more than $0.65 was added to the machine, then return the extra money). Again, don't worry about managing different kinds of coins – just display the amount returned.

When the machine is "restocked," just set the soda count to the maximum that the machine will hold.


And below is the rubric (which won't keep its formatting):

Criteria Level 1: Nothing to Grade Level 2: Need Improvement Level 3: Meet Expectations Level 4: Exceptional
Criterion 1: Assignment 7.1 - uses methods 0 points
Nothing to grade or ungradeable

7 points
Code uses methods incorrectly - and serious problems exist

14 points
Code calls methods correctly, but minor problems exist

20 points
Code calls methods correctly

Criterion 2: Assignment 7.1 - Coding style 0 points
Nothing to grade

1 point
Poor coding style

3 points
Coding style ok, but needs improvement

5 points
Coding style is excellent

Criterion 3: Assignment 7.2 - Attributes 0 points
No attributes defined

1 point
Class has little to no attributes defined

3 points
Class has attributes, but minor problems

5 points
Class defined attributes correctly

Criterion 4: Assignment 7.2 - Constructor 0 points
No constructor defined

1 point
Constructor definition needs serious changing

3 points
Constructor defined, but minor changes need

5 points
Constructor defined correctly and initializes attributes

Criterion 5: Assignment 7.2 - Accessors/mutators 0 points
No accessors or mutators

1 point
Accessors and mutators need serious changes

3 points
Accessors and mutators are close to correct, but need minor changes

5 points
Accessors and mutators are defiend correctly

Criterion 6: Assignment 7.2 - Method getChangePercent 0 points
No method defined, or ungradeable

1 point
Method defined, but needs serious work

3 points
Method defined but needs minor change

5 points
Method defined correctly

Criterion 7: Assignment 7.2 - Coding style 0 points
Nothing to grade

1 point
Poor coding style

3 points
Coding style ok, but needs improvement

5 points
Coding style is excellent

Criterion 8: Assignment 7main - Attributes 0 points
No attributes defined

1 point
Class has little to no attributes defined

3 points
Class has attributes, but minor problems

5 points
Class defined attributes correctly

Criterion 9: Assignment 7main - Constructor 0 points
No constructor defined

1 point
Constructor definition needs serious changing

3 points
Constructor defined, but minor changes need

5 points
Constructor defined correctly and initializes attributes

Criterion 10: Assignment 7main - Vend method 0 points
Vend method not defined

1 point
Vend method defined, but needs serious work

3 points
Vend method defined, but needs minor work

5 points
Vend method defined and functions correctly

Criterion 11: Assignment 7main - Restock method 0 points
Restock method not defined

1 point
Restock method defined, but needs serious work

3 points
Restock method defined, but needs minor work

5 points
Restock method defined and functions correctly

Criterion 12: Assignment 7main - AddMoney method 0 points
AddMoney method not defined

1 point
AddMoney method defined, but needs serious work

3 points
AddMoney method defined, but needs minor work

5 points
AddMoney method defined and functions correctly

Criterion 13: Assignment 7main - Main program 0 points
Nothing to grade

5 points
Menu system, logic and variables need changing

10 points
Menu system, logic and variables need minor changing

15 points
Menu system, logic and variables declared correctly

Criterion 14: Assignment 7main - Coding style 0 points
Nothing to grade

3 points
Poor coding style

7 points
Coding style ok, but needs

10 points
Coding style is excellent

Overall Score Level 1
0 or more Level 2
27 or more Level 3
41 or more Level 4
55 or more


Before I can do all the things you've suggested for improving the program, I have to make this program work according to instructions. I'm not saying that the professor wouldn't be thrilled if I made it look better; I'm saying that I have a deadline & have to get this ready to turn in so that I can meet the deadline if I don't get the other stuff done before that deadline.

I run the program from the main method, where I've created 5 soda objects. All I'm doing to run it is calling the vend method. I've tried calling the restock method to try to make it add up the sodas that are bought, but that makes the program stop working. I've tried calling it from the vend method, & nothing happens. Other than BigDecimal, all I need to do to prepare the program for submission is make the restock method work. What am I missing/doing wrong/failing to see regarding the working/calling of the restock method?

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'll try once more. I want to help. To do that I would like to see exactly how you're running the code (please post it) and any changes you've made since you first posted your code (like removing statics). Once you post those two things I'll be happy to help you.

Note: your code does not need to be perfect at this point. Just post what you have.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute--thank you.

Here's my main class, exactly as I have it in Netbeans:


Now here's my Soda class, exactly as I have it in Netbeans, including the long lines (sorry):
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this code:


Will it ever exit?
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. I ran into that when I was counting the stock in the vend method, because the computer doesn't know that there's no such thing as -1 Coke. So it has to be > 0 & less than 50. Right?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that sodaCount doesn't change inside the while loop, so it will never exit.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why I was trying to call it from vend. In my mind, that made them interact. Not so, huh? I want the sodaCount from vend to go into restock so that every time sodaCount goes down 1, newStock goes up 1.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need a loop to do that?
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your questions suggests not. Let me think about it...
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I should be able to use an if statement. But that still leaves me with the problem of connecting vend with restock.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the assignment:
When the machine is "restocked," just set the soda count to the maximum that the machine will hold.

So it seems that restocking isn't something that happens each time you vend. Maybe you should restock after you have vended for a while.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just changed the while loops to if statements & requested a printout of newStock. Then I called the restock method from main. But nothing happened. No printout. Now THAT'S strange. I've really screwed up somewhere if it won't system.out.print.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should restock after you have vended for a while


Oh, that sounds worse. Actually, that statement confused me. I can't even make the method keep track of how many are needed, let alone throw some back in.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you were to "just set the soda count to the maximum", what would that look like?
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set it where?

And I was wrong. It is printing out, but when I buy 3 sodas, it only counts 1.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set it where?

In restock().
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What my mind comes up with is too simple. I'd say newStock = 50 & then call the restock method after vend. Then, no matter how many sodas had been sold, the number would automatically go back to 50 for each soda. Right?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's all that needs to happen, from reading the assignment.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoa! I was just trying to provide notice of when a soda needs to be restocked. But you're right--that's what it sounds like. This is where OOP is a little much for me: when I originally considered having to restock, I imagined having to get real cans of soda to a virtual store. I couldn't figure out how I was going to trot them from cyberspace to my restock method. Seriously--I was seeing real cans in my mind, & while I knew that wasn't possible, I never made the leap.

Thank you so much!
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, all things are lawful, but I will not be brought under the power of any.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. And for programming, it means that just because you can do something, doesn't mean you should.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!