-3

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.

mkobit
  • 43,979
  • 12
  • 156
  • 150
footballgeek
  • 15
  • 1
  • 5

1 Answers1

1

Your Stock class doesn't have a containsKey() method

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • It did have a containsKey method but it was named incorrectly (containsSalesItems), I have changed it to containsKey(). But now I have another error that I have listed above – footballgeek Mar 07 '15 at 19:46