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

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Not sure why this code is not passing.

I'm not sure why the below code is not passing, I have uncommented the line in Example.java so that the argument of adding just the dispenser is passed, but it seems like there's no output. I'm thinking it might have something to do with the printf statement but I am not sure where the problem could be in it.

Thanks for the help.

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);
    Product dispenser = new Product("Yoda PEZ dispenser");

    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());
  }
  public void addItem(Product addedItem) {
    System.out.printf("Adding %s to the cart.\n", addedItem.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

Stone Preston
Stone Preston
42,016 Points

I did the exact same thing as you when I did this challenge.

in the shopping cart class you in your new method that does not take a quantity, you need to call the addItem method thats already been defined. since this method will always add 1 item, you can just pass in 1 as the quantity argument and the addedItem parameter as the item argument

public class ShoppingCart {

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

       addItem(addedItem, 1);
  }
}

while your current way will work, since you already have this method defined its better to go ahead and use it to avoid duplication. also the challenge is expecting you to do it this way

I believe your answer is exactly correct, because you have used method signatures. All you need to do is change

"Adding %s to the cart.\n"

to

"Adding 1 of %s to the cart.\n"