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

Alex Lowe
Alex Lowe
15,147 Points

[SOLVED]Unclear on what I'm being asked to do on this challenge.

I assume I'm supposed to create the addItem method to have a second argument with a default value of one, but I'm not sure how to do this.

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 this line after 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 */
  }
}
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;
  }
}
Alex Lowe
Alex Lowe
15,147 Points

I see what I did wrong now. In case this happens to anyone else, you need to create another addItem method in shoppingCart.java.

It wasn't until posting this that I realized that there is more than one file. I was only looking at the Example.java file.

2 Answers

Example.java has a line of code that is commented out

    cart.addItem(dispenser);

If all you do is uncomment this line and check your work, you get a compiler error. We need to write a second addItem method that only takes one argument, rather than two.

So, in ShoppingCart.java, we can copy and paste the existing addItem method, but remove the second argument. We also need to change what is printed, because we know that we are Adding 1 of the item to the cart.

I am not getting it, I follow we on creating the second method signature. How do I call the addItem and pass 1?..

Alex Lowe
Alex Lowe
15,147 Points

@elsmart35 In the ShoppingCart.java file, there is a method called addItem, with two parameters: item and quantity. You're being asked to create another addItem method(in addition to the original, not replacing it) that takes only one parameter. So, in your new addItem method, you need to call the original addItem and pass in as parameters the sole parameter of your new method and the number 1.

That sounded increasingly convoluted as I typed it, so let me know if you want further clarification.

Thanx bro, i get it now.

My bro, me having some errors on my refactoring challenge. And initially when I imported the staff it seems the songbook comes errors. I