Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Delivering the MVP Defaulting Parameters

Default parameters

hello out there can you help out i dont get where im missing this on how use method signatures cos im being bummered this is my code.Thank you!!!

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 %s " ,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;
  }
}

2 Answers

Parker Busswood
Parker Busswood
20,207 Points

Hey Ashford,

So it looks like you've got both of the addItem methods in place in ShoppingCart.java which is great. One accepts a product and a quantity, and the other just accepts a product, and the quantity is assumed to be 1. You can refactor your code by having your addItem(Product Item) method just call into the other addItem method, passing in a quantity of 1.

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);
  }
}

This is awesome because now if you ever want to modify the message that is printed out for the other addItem method, both methods will now update with one change. You could also add additional functionality to the first addItem method and the second one will now be able to take advantage of those changes. This may be why your code isn't being accepted in the challenge.

Hope that helps!

Parker

thanks parker

Thanks a lot Parker. Had a problem with this and was wondering where the issue was. Thank you.