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
Sebastian Röder
13,878 PointsFor consistency, `getItemName()` for an empty bin should return an empty string.
After the video we have the following code in Bin.java:
public String getItemName() {
if (isEmpty()) return null;
return items.peek().getName();
}
public int getItemPrice() {
if (isEmpty()) return 0;
return items.peek().getRetailPrice();
}
The behaviour of the two methods is not consistent. The methods should either both return null or both return a "known good" value when the bin is empty. When the price for an empty bin is 0, I suppose the item name for an empty bin should be "" (the empty string), or maybe even better the string "Empty".
Imagine our vending machine has a display to show an item’s name and price. When the bin is empty, the machine should still be able to display something sensible there.
public String getItemName() {
if (isEmpty()) return "";
return items.peek().getName();
}
public int getItemPrice() {
if (isEmpty()) return 0;
return items.peek().getRetailPrice();
}
1 Answer
Pedro Cabral
33,586 PointsNice catch! Speaking of which, I think it might harm whoever calls the getter because there is a chance of being thrown a NullPointerException down the road.