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

Abe Daniels
PLUS
Abe Daniels
Courses Plus Student 2,781 Points

I need help being DRY!

read code

ShoppingCart.java
//So I need to make this work, without repeating my code, (Adding some code into *here*)
  public void addItem(Product item) {
    System.out.printf("Adding 1 of %s to the cart.%n", item.getName());
  }
  public void addItem(Product item, int quantity) {
    System.out.printf("Adding %d of %s to the cart.%n", quantity, item.getName());
    //*here*
  }

1 Answer

Hello Abe Daniels ,

You should simply call one method within the other. In this case, call the one with 2 parameters inside the one with 1. It should look like this:

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