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 trialJavier MARQUEZ
11,877 PointsI am lost here, I just cannot seem to understand what they want here.
I am aware that I have to create a new method with different arguments but, I am quite lost.
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);
}
}
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 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;
}
}
2 Answers
andren
28,558 PointsIf you understand that a new method is needed then you have pretty much already solved half the challenge, this challenge is setup to teach about method overloading.
Method overloading is the act of having multiple methods with the same name which take different arguments, so step one is to make a method named addItem that only takes one argument:
public void addItem(Product item) {
}
The challenge demands that this method performs the same work as calling the other addItem code does with 1 as the quantity, but also asks that you don't replicate any of it's code.
Achiving this is actually far simpler than you probably might think, the answer is to literally just call the other method, providing it with 1 as the quantity and the item passed into this method as the item argument, like this:
public void addItem(Product item) {
addItem(item, 1);
}
Calling one version of a method from within another version might seem a bit strange and "wrong" at first, but it's actually a pretty common thing to do when you are overloading methods and it's a completely valid thing to do in Java.
Javier MARQUEZ
11,877 PointsThank you both, yes for some reason I was just blindly looking at the example.java, instead of creating the method on the shopping cart.
So basically java distinguishes between methods even if the have the same name, by simply having a diffferent signature (different arguments required). This can be useful as with the shopping cart, where a default addItem() function can use a static quantity, and call the addItem(int quantity , string product) by leaving one of the fields (in this case quantity) set to 1.
Genious.
Nikhil Sharma
1,173 PointsNikhil Sharma
1,173 Pointseven i stucked at that point , they are just testing your level of intelligence first thing you need to do is uncomment
// cart.addItem(dispenser); in example.java
then ,they just want to make the cart simple , like when you go to a Store you will always tend to ask for 1 quantity ,like 1chocolate or else 1 headset etc etc you need to write a add method that takes item as argument and which is used for purchasing only 1 quantity in Shopping cart.java here is the code :
hope you got that !