In my Supermarket Checkout Program when I run it, it comes up with this error in the Checkout
class in the addItem
method:
cannot find symbol - method containsKey(Item)
I cannot understand why it is coming up with this error as I have checked the other two classes (Item
and Stock
) and I can't see anything wrong with them.
Here are the classes:
import java.text.NumberFormat;
import java.util.HashMap;
public class Checkout {
private HashMap<Item, Integer> stock;
/**
* Constructor for objects of class Checkout. Instantiates the checkout.
*/
public Checkout() {
stock = new HashMap<Item, Integer>();
}
public void addItem(String itemCode) {
Stock stock = new Stock();
if (stock.containsSalesItem(itemCode)) {
Item item = stock.getItem(itemCode);
int quantity = 1;
if (stock.containsKey(item)) {
Integer quantity = stock.get(item);
quantity += quantity.intValue();
}
stock.put(item, new Integer(quantity));
}
}
}
import java.text.NumberFormat;
public class Item {
private String name;
private String code;
private int price;
/**
* Constructor for objects of class Item
*/
public Item(String itemName, String itemCode, int costOfItem) {
name = itemName;
price = costOfItem;
code = itemCode;
}
public int getPrice() {
return price;
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
}
import java.util.Map;
import java.util.HashMap;
import java.util.Collection;
public class Stock {
private static Map<String, Item> stock;
static {
stock = new HashMap<String, Item>();
stock.put("001", new Item("Diet Coke 1l Bottle", "001", 299));
stock.put("002", new Item("Haribo", "002", 100));
stock.put("003", new Item("Digestive Biscuits", "003", 120));
stock.put("004", new Item("Teacakes", "004", 80));
stock.put("005", new Item("Bacon", "005", 399));
stock.put("006", new Item("Bread", "006", 213));
}
public Collection<Item> getItems() {
return stock.values();
}
public Item getItem(String itemCode) {
return stock.get(itemCode);
}
public boolean containsSalesItem(String itemCode) {
if (stock.containsKey(itemCode)) {
return stock.containsKey(itemCode);
} else {
return false;
}
}
}
Thanks for any help and advice.