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 Refactoring

Confused and in Need of Help

public void addItem(Product item) { 
      System.out.printf("Adding 1 of %s to the cart.\n", item.getName()); 
}

This is the only code I've added. I don't understand what else it is that I need to do. I've watched the video three times, but I'm still not able to make the connection between it and the exercise. I also have been going through and building the Hangman Game in workspaces to use as notes, but I still haven't been able to figure this one out. //System.out.println(":("); And while giving the answer would be fast and simple, I also need more help with what Craig is actually doing here and how to apply it elsewhere.

3 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Jacob, you did the same thing I did and many students do in this, you overloaded the method perfectly. However this challenge is expecting you to use the previous method inside your new overloaded method.

So try something like

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

Also, be sure to uncomment the code in example.java cart.addItem(dispenser);

Rogelio Valdez
Rogelio Valdez
6,244 Points

I forgot how to use method signatures. I only remembered that had to use one inside the other method and ended up the challenge repeating the code twice (I know it's not the best practice). Thank you for refreshing my memory.

Rogelio Valdez
Rogelio Valdez
6,244 Points

Hi Jacob! You were getting the idea right but the best practice is what Rob Bridges just mentioned. Since he is not repeating himself (remember DRY principle?). However you could have also used this code where instead of putting the "1" Inside quotes you could have used it outside the quotes instead of "quantity" and this way the challenge will be completed. It would be better if the challenge didn't let you do this, but anyway I am mentioning this to help you understand a little bit more.

Remember this is not the best practice since you are repeating your code. This was mentioned by Craig somewhere in the first lessons where he mentioned this is a way of setting a default value to a method (method signatures).

//This is not the best practice since you are repeating code but it will work

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

  public void addItem(Product item) {
    System.out.printf("Adding %d of %s to the cart.%n", 1, item.getName());
  }
Neelesh Tewani
Neelesh Tewani
1,239 Points

hi i am Neelesh i want to ask there is another way means without copying the code because craig said that" this is not the best practice"

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hi Neelash, you are right there is a better way to do it.

You can simply just create a new method signature and call the method like below.

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

This will automatically pass the value of the item and 1 into the method. Without repeating yourself.