Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Aditya Sheode
1,291 PointsA different approach
instead of using the while and if loops as Demonstrated by craig I came up with a hybrid (sorta) is it okay? heres my code:
while (!CandyBar.isEmpty()) { CandyBar.dispense(); System.out.println("Just eat it"); }
1 Answer
Pedro Cabral
Full Stack JavaScript Techdegree Student 23,916 PointsI think it's the same really, what Craig did was to delegate the "isEmpty() check" to the dispense() method but as long as the logic is correct both the conditions are ultimately checking for a boolean value on whether or not there is something to be dispensed. I think though if you would want to be picky, the empty check should reside on the model as it should be it's responsibility to know if it can dispense or not. Also one more unrelated thing, if those methods you are calling are instance methods (which I think they are) as a rule of thumb you should name your instance variables using camelcase: candyBar and not CandyBar.