INTERMEDIATE JAVA - Let's Get Takeout project

Hey guys,
Can you help please with this project https://www.codecademy.com/courses/learn-intermediate-java/projects/intermediate-java-collections-takeout

I’m stuck with step 7. " In ShoppingBag , create a method called getTotalPrice() that has no arguments and returns an int value. This method should iterate through all the entries in shoppingBag and accumulates the total price of each item with respect to the quantity of that item."

I wrote this one

public int getTotalPrice() {
  int sum = 0;
  for(Map.Entry<T,Integer> element : shoppingBag.entrySet()) {
  System.out.println("item: "+ element.getKey() + ", count: "+element.getValue());
   sum = sum + element.getKey().getPrice() * element.getValue();
  } return sum;
}

but I can’t understand how can I tie the price of Food with each item? since the price of Food is in another class “Food”

1 Like

The code you have looks correct for what it is supposed to be performing, have your tests been coming up inconsistent?

The price of the Food object is created at the same time the Food object is instantiated using the constructor

Food(String name, String description, int price).

because within the Food class we we performed an @Override on the getPrice()method ( contained in the PricedItem interface ) we can always access the price of any food object that has been created via the getPrice() method.

hopefully I didn’t make that confusing… I too am working through this project and trying to gain a better understanding of the topics

thank you for the answer. I found the problem. The problem was that I wrote the wrong type of generic class, I forgot about interface