Intermediate Java "Let's get Takeout!" Step 7

Hello everyone, I’m currently stuck on step 7 of the Let’s Get Takeout project.
It requires you to use the getPrice() function, as defined in and overridden in, but in ShoppingBag, T is upper bounded by Integer. How do I call getPrice() on it when getPrice() is used for Food objects?

public interface PricedItem {
T getPrice();
void setPrice(T price);

public class Food implements PricedItem {
private String name;
private String description;
private int price;

public Food(String name, String description, int price) { = name;
this.description = description;
this.price = price;

public void setPrice(Integer price) {
this.price = price;
public Integer getPrice() {
return this.price;
public String toString() {
String priceString = Integer.toString(this.price);
String foodDesc = “Item: " + + " Cost: $” + priceString;
return foodDesc;

and most importantly,

import java.util.HashMap;
import java.util.Map;

public class ShoppingBag {
private Map<T,Integer> shoppingBag;

public ShoppingBag() {
this.shoppingBag = new HashMap<T,Integer>();

public void addItem(T item) {
if(shoppingBag.get(item) != null) {
shoppingBag.put(item, shoppingBag.get(item) + 1);
} else {
shoppingBag.put(item, 1);

public Integer getTotalPrice() {
Integer totalPrice = 0;
for(T item : shoppingBag.keySet()) {
Integer itemQuantity = shoppingBag.get(item);
Integer itemPrice = itemQuantity * item.getPrice();
totalPrice += itemPrice;
return totalPrice;

Apologies for the unformatted code, the codebyte system doesn’t have java as an option
Unless I’m missing it somewhere :man_shrugging:

1 Like

Method instructions: The method should “calculate the total price of each item by iterating through the item s in shoppingBag and multiplying each item ‘s quantity by its price. Sum up all of the total prices to find the grand total of everything in shoppingBag . Return the grand total.”

It seems you are confused about the type parameter T and its relationship with the PricedItem and Food classes. T should be bounded by the PricedItem class, not Integer. This would allow access to the getPrice() method within the ShoppingBag class, which is defined in PricedItem and overridden in Food.

To resolve this issue, update the ShoppingBag class definition to have the type parameter T bounded by the PricedItem class. The class definition would look like this:

public class ShoppingBag<T extends PricedItem> {
// Rest of the code…

By doing this, the type T in the ShoppingBag class is constrained to be a subclass of PricedItem, which means it will have access to the getPrice() method. This would fix the issue, and the corrected getTotalPrice() method provided earlier should work as expected.

In addition, there are multiple mistakes in your code. I will explain each mistake and the corresponding correction in the corrected code (my code that was used to complete the project).

  1. Using Integer instead of int:
    In your code, you used Integer instead of int for declaring variables. While it’s not necessarily wrong, it’s generally better to use the primitive data type int over the wrapper class Integer for performance reasons, as using Integer may lead to unnecessary boxing and unboxing.
    Correction: In the corrected code, all Integer variables are replaced with int.

  2. Iterating through the keySet():
    In your code, you iterated through the keySet() of the shoppingBag map. While it’s possible to iterate this way, it’s more efficient to iterate through the entrySet() because it allows you to access both the key and value directly in a single iteration, rather than using the key to access the value separately.
    Correction: In the corrected code, the iteration is done through the entrySet() of the shoppingBag map using for (Map.Entry<T, Integer> entry : shoppingBag.entrySet()).

  3. Accessing the item and its quantity:
    As the corrected code is iterating through the entrySet(), it can directly access the item and its quantity using entry.getKey() and entry.getValue(). This makes the code more efficient compared to using the key to access the value in the initial code.

  4. Calculating the item total price:
    In the corrected code, the item total price calculation is slightly modified for better readability, as follows:

int itemTotalPrice = item.getPrice() * quantity;

Here is my solution:

public int getTotalPrice() {
int totalPrice = 0;
for (Map.Entry<T, Integer> entry : shoppingBag.entrySet()) {
T item = entry.getKey();
int quantity = entry.getValue();
int itemTotalPrice = item.getPrice() * quantity;
totalPrice += itemTotalPrice;
return totalPrice;