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

Java Java Objects (Retired) Delivering the MVP Defaulting Parameters

Code Challenge Unclear.

My code looks fine and it doesn't have any compiler errors. However, the challenge keeps telling me "Did you uncomment the line in Example.java? The output from System.out does not show the 1 Yoda PEZ dispenser." and I don't know why. I have uncommented the cart.additem(dispenser) and the console displays "1 Yoda Dispenser" so I'm unsure what exactly the challenge is asking for me to do.

Example.java
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");
    /* Uncomment the line following this comment,
       after adding a new method using method signatures,
       to solve their request in ShoppingCart.java
    */
    cart.addItem(dispenser);
  }

}
ShoppingCart.java
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) {
    System.out.printf("Adding 1 %s to the cart. %n", item.getName());
  }
}
Product.java
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;
  }
}

3 Answers

Katherine Marino
Katherine Marino
3,286 Points

Ok, this is one of those frustrating things where you have to match the EXACT output that you would get if you called the method using addItem(dispenser, 1). Your solution works, but your format is just slightly off from what it expects. Your new addItem method should read:

  public void addItem(Product item) {
    System.out.printf("Adding 1 of %s to the cart.%n", item.getName());
  }

Note the "of" that I added back in, and also that there's no space before %n.

However, once you do that, it will give you a new error message, "Hmmm, seems like you shouldn't repeat yourself, why don't you try calling the original addItem method with the default parameter of 1." Go ahead and try to figure out how to solve this error on your own, but I'm going to include the solution in case you're still stuck. :)

What this problem is expecting you to do, so you aren't repeating yourself, is use the new method signature to call the first method. This is done by having your new method look like this:

public void addItem(Product item) {
    addItem(item, 1);
}

Let me know if you have any further questions! Happy coding!

Thank you very much. This worked.

Katherine Marino
Katherine Marino
3,286 Points

Glad I could help! Have a great day!

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Paul - What you need to do here is to take advantage of the fact that you already have an addItem() method that takes a product and a quantity. So in the body the addItem() method that takes one argument just call the other one and use 1 for the quantity:

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);  
  }
}
Julian Garcia
Julian Garcia
18,380 Points

For reusability you need to call the original method , addItem(Product item, int quantity), inside this new method and provide the right parameters, the quantity must be set to 1.