-3

Checkout在我的超市结账程序中,当我运行它时,它在方法的类中出现了这个错误addItem

找不到符号 - 方法 containsKey(Item)

我不明白为什么会出现这个错误,因为我检查了其他两个类(ItemStock),我看不出它们有什么问题。

以下是课程:

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;
        }
    }
} 

感谢您的任何帮助和建议。

4

1 回答 1

1

你的Stock班级没有containsKey()方法

于 2015-03-07T19:07:03.803 回答