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 trialAPRIL GOLDEN
812 PointsShoppingCart Challenge
Unable to get the ShoppingCart challenge without error compilers. The Example.Java will not accept cart.addItem(pez, 5) whether pez is capitalized or not, and a space before the number 5 isn't accepted as well! cartaddItem(dispenser); will not be accepted either. Is there someone able to show me an example of what the ShoppingCart challenge looks like in completion, I've been studying this for days!
public class Example {
public static void main(String[] args); {
System.out.printf("Adding %d of %s to the cart. %n", quantity, item, getName());
/*Other code omitted for clarity*/
}
ShoppingCart cart = new ShoppingCart();
Product pez = new Product("Cherry PEZ refill (12 pieces)");
cart.addItem( pez, 5);
/* Since a quantity of 1 is such a common argument when adding a product to the cart,
* your fellow developers have asked you to make the following code work, as well as keeping
* the ability to add a product and a quantity.
*/
Product dispenser = new Product("Yoda PEZ dispenser");
/* Uncomment the line following this comment,
after adding a new method using method signatures,
to solve their request in ShoppingCart.java
*/
cart.addItem(pez dispenser);
}
}
public class ShoppingCart {
public void addItem(Product item, int quantity) {
System.out.printf("Adding %d of %s to the cart.%n", quantity, item.getName());
/* Other code omitted for clarity. Please imagine
lots and lots of code here. Don't repeat it.
*/
}
public void addItem(Product item){
addItem(item, 1);
}
}
public class Product {
/* Other code omitted for clarity, but you could imagine
it would store price, options like size and color
*/
private String mName;
public Product(String name) {
mName = name;
}
public String getName() {
return mName;
}
public void addItem(Product addedItem){
addItem(addedItem, 1);
}
}
1 Answer
Christopher Augg
21,223 PointsApril,
We only need to make two changes to complete this challenge.
We make one change to ShoppingCart by adding an Overloaded addItem method. We pass in only the Product item to this new method. However, we call the orginal addItem method within our new addItem method and pass in (item, 1) because we want the ability to call this method with one argument whenever we only want one item.
The only other modification required to pass the challenge is to uncomment the line within Example.java :
cart.addItem(dispenser);
I would recommend starting over as you have altered a lot of code that was not necessary. Then just follow the tips above. Please let me know if you are still having issues and I will go over the code.
Regards,
Chris