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 PricedItem.java and overridden in Food.java, but in ShoppingBag, T is upper bounded by Integer. How do I call getPrice() on it when getPrice() is used for Food objects?
PricedItem.java:
public interface PricedItem {
T getPrice();
void setPrice(T price);
}
Food.java:
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;
}
}
and most importantly, ShoppingBag.java:
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