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 trialAmit Panchal
8,859 PointsI uncommented the comment in Example.java but it still shows me an error
Even though I uncommented the code in Example.java but it still shows me an error saying System.out does not display
public class Example {
public static void main(String[] args) {
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");
cart.addItem(dispenser);
/* Uncomment the line following this comment,
after adding a new method using method signatures,
to solve their request in ShoppingCart.java
*/
}
}
public class ShoppingCart {
private int mCount;
public ShoppingCart() {
mCount = 0;
}
public void addItem(Product item, int quantity) {
System.out.printf("Adding %d of %s to the cart.%n", quantity, item.getName());
mCount += quantity;
}
public void addItem(Product item) {
int quantity = 1;
System.out.printf("%d %s.%n", quantity, item.getName());
mCount += quantity;
}
}
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;
}
}
2 Answers
Placinta Alexandru
3,011 PointsAre you sure you have imported java.io.*? I dont see any mistake at your code But in "ShoppingCart.java" at the method "addItem(Product item)" you could use the one above to avoid rewriting same code, just like this "addItem( item, 1 );"
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsThe problem is in the way you solve the challenge.
The task of the challenge is to write re-usable method addItem(Product item)
that takes one argument and adds only one item.
Now you already have method that adds n
items, and now you have to reuse it very simply:
public void addItem(Product item) {
addItem(item, 1);
}
I'm not saying that your solution is incorrect. It is correct, but first of all Craig wants you to learn the way how to re-use the functions you are already written.