I get called all sorts of things. As long as you don't spell it wrongly
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): . . .
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:-
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?
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!”
. . . Math.round(value * 100)/100), but it doesn't work with numbers that are just fractions. (Soda price is $.65.) . . .
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.
Karen Guffey wrote:. . . before I start . . . I need to know how to make the restock method kick into action. . . .
When the machine is "restocked," just set the soda count to the maximum that the machine will hold.
Maybe you should restock after you have vended for a while
Set it where?
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Appshttps://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous