last day (15 days later) » 

14:57
26
Q: OO coffee dispenser

MaxThis picture inspired a contest between a few of my friends to rewrite this code in more proper OO style. This is what I have come up with. Any thoughts: public enum CoffeeKind { Black = 1, Cappuccino, Espresso } public class Coffee { //Measured in celcius public int Tem...

The idea itself gets a +1
Your Fill requires starting with an empty cup, i.e. no refilling or partial filling. And your Drink should clamp at 0 on the low end.
Max
Max
@Mat'sMug on the mug or my code? :)
Your code is fine, I mean the mug of course! (clicked the link?)
Max
Max
14:57
@Mat'sMug yeah, just thought maybe I missed something
Temperature should be the root of its own object hierarchy. An int and a comment aren't very sufficient to programmatically convey meaning.
Max
Max
@JesseC.Slicer good point. If I find something prebuilt, I'll switch to that. Otherwise I may just drop the whole temperature bit as enough people will drink coffee that has cooled to the point of cold
@JesseC.Slicer An object hierarchy to represent a temperature? How much did that ivocy tower cost you?
Not as much as having to pay down technical debt costs, interestingly enough.
OO-wise this is a spilled cup. It's all over the place. You've got everything except a CupOfCoffee. The "dispenser" in the title can only be inferred w/in the spillage.
Max
Max
14:57
@radarbob would you like to elaborate please?
Temprature - really? You need more coffee
Max
Max
@serbanghita :) thanks for catching that. If only you would know how bad my spelling used to be ;)
This code wouldn't fit on the side of a mug.
@DanDan true, but this code at least lets you drink the coffee you've just refilled. They're incompatible branches on the cup.
I don't have time to write a full answer at the moment, but consider this: message dispatch aka virtual method calls are already a form of conditional. In an object-oriented program, there is no need for if or switch. Smalltalk is the existence proof of that: it doesn't even have conditionals (nor does it have loops), all it has is method calls. I count ~10 conditionals, one switch, and one loop. Can you get rid of them all?
Max
Max
14:57
@JörgWMittag I plan on trying, but short of creating a separate class for each cup size I'm not sure how to get of every last conditional. To constrain them to main - maybe, but not all. If you have time to go into more detail that would be much appreciated
What if two people call GetCoffee with the same size. The static cup is returned to both, one drinks, the other doesn't.. Same object so the cup is empty for both... Or am I wrong?
Max
Max
@pinkfloydx33 good observation. I assume you are correct but will tweak the code in main to verify. As is each person needs to run there own instance so there are no such issues
I don't see much OO in the code, having a class and parametrize it with an enum is against the Object orienting idea (especially the open-closed principle). Related (OO coffee maker) and useful iei.liu.se/is/edu/courses/it-projekt---fordjupning/…

last day (15 days later) »