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

Dimitar Tsvetkov
Dimitar Tsvetkov
6,806 Points

Yoda pez dispeneser. I think it should work in a regular compiler, its not working here though. What am I doing wrong?

When I run that, it shows an error idn 2 red lines. First line just prints the first item in the shopping card. Second line prints the print statement for the second item in the shopping card, without the quantity.

Bummer! Did you uncomment the line in the Example class. It doesn't show "Yoda pez dispenser", or similar.....??? Its all printed.......

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 to the cart.%n ", 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

Kevin Faust
Kevin Faust
15,353 Points

Hey Dimitar,

Take a look at your shopping cart.java class. Notice how your using the exact same piece of code in your 2 methods? We already have a method that is doing what we want. Notice in the first method, it takes a product and an int. When we write a product without a value, we would call that second method.

Look at this comment that was written in the example file:

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.

It essentially says it wants to add one AND make it show in the console. What your code is doing is showing that you added something to the cart. What we want is for it to say we want 1 of something added to the cart. Obviously you can just change the print statement but the challenge wants you to take advantage of our pre-existing method which is already doing what we want. Look at this:

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

What would happen if we did this? When we pass in a product only as a parameter, it will actually run our first method from within. To run our first method, we need a product and an int. We already got a object and now we just want to put our int as 1. This'll be a fixed int value that we use if we only pass in an item and no int. This was a sortof long explanation on something you probably know but I just wanted to make sure.

Let me know if you have any further questions,

Happy coding and best of luck,

Kevin

Dimitar Tsvetkov
Dimitar Tsvetkov
6,806 Points

Thank you Kevin! Very thorough and precise explanation. It cleared up everything now.

Kevin Faust
Kevin Faust
15,353 Points

Glad I was able to help

Just had the same question and you nailed it on the head Kevin. Much appreciated :) -A newbie coder