step 7 asks to make a getTotalPrice() method, which needs to use the getPrice() method from Food.java. but given that ‘T’ is upper bounded by Integer in ShoppingBag, I don’t know how to make it work.
here is my ShoppingBag class:
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;
}
}
and here is my PricedItem interface and Food class:
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) {
this.name = name;
this.description = description;
this.price = price;
}
@Override
public void setPrice(Integer price) {
this.price = price;
}
@Override
public Integer getPrice() {
return this.price;
}
@Override
public String toString() {
String priceString = Integer.toString(this.price);
String foodDesc = “Item: " + this.name + " Cost: $” + priceString;
return foodDesc;
}
}
I’m just completely stumped here and don’t know how to proceed. Maybe I made some kind of oversight in the Food class or PricedItem interface?