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

Defaulting parameters - Still not working...

I put the output Func. and the system is not printing the dispenser object.

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) {
    System.out.printf("Adding %s.", item.getName());
  }

  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. 
    */
  }
}
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;
  }
}

1 Answer

The challenge is expecting you to call the "addItem" method with 2 parameters inside your new "addItem" method. You made the same mistake that I did. Instead of using a printf inside the new method, try this:

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

Hope this helps!

eric Crawford
eric Crawford
2,381 Points

As i did the same as well i was wondering does anybody know if this is considered method "overloading"?

Pauliina K
Pauliina K
1,973 Points

Could someone explain what everything in this code means? I understand what the public void addItem is, but what is the (Product item) and addItem(item, 1); on the bottom line? What do they do? Why do you need two parameters for example? I get that Product is a class, but what is item? I can't see it being anywhere in the code except as addItem.

Is that whole method just for when someone adds one item? If so, it make a bit more sense. Otherwise, I'm still lost.

(sorry for tagging you again, Steve Hunter, but this thread doesn't seem to be active and you're the only one I know that could probably help me out.)

Yes, Pauliina K - it is a whole method for when just one item is added.

That method overloads the original one that required two parameters - a Product and a quantity. Because so many transactions were for adding just one item, the addItem() method is overloaded to take one parameter, rather than two, so that one unit of the Product parameter is added.

There's no duplication of code as the single-unit method just calls the original one, defaulting the parameter to 1.

Make sense?

Steve.