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

isral bustami
PLUS
isral bustami
Courses Plus Student 5,932 Points

how can i solve this, there is no syntax error..

i have set the method signature, but still not correct.. can someone give me a hint..

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");
    cart.addItem(dispenser);

  }

}
ShoppingCart.java
public class ShoppingCart {

  public int itemAmount;

  public void addItem(Product item) {
    int quantity = 1; 
    System.out.printf("%d%s\n", quantity, item.getName());
  }

  public void addItem(Product item, int quantity) {
   int newAmount = itemAmount + quantity; 
    System.out.printf("Adding %d of %s to the cart.\n", quantity, item.getName());
    /* Other code omitted for clarity */
    itemAmount = newAmount;
  }
}
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

Marileen Mennerich
Marileen Mennerich
1,161 Points

If I understand the task correctly, you are supposed to create an addItem-Method that only takes a product and assumes the quantity to be 1. You did this, however in your method you forgot to actually call the original addItem-method with your item and the quantity 1. You can also remove the printf-call from your new method, as it will be called in the other addItem-method. This is what it should look like:

public void addItem(Product item) {
    additem(item, 1);
  }
isral bustami
PLUS
isral bustami
Courses Plus Student 5,932 Points

so we use the second addItem method to make default the new addItem method... ok that's a new one for me or maybe i am not following the course well.. thank you so much...

It appears to me that you are following the course well. The code you posted in your question will work, with one tiny modification.

All you need to do is change

"%d%s\n"

to

"%d of %s\n"