从哈希图操作静态数据



下面是我被告知要使用的代码。

我对Java没有太多经验,但我对OOP有一些了解。

我有一个包含库存项目价格和数量的哈希图,我希望能够显示所有这些变量,将它们添加到列表中并访问不同类别中的价格等。我已经阅读了文档和很多网站,但我仍然不清楚如何使用这些数据。

public class StockData {
    public static class Item {
        Item(String n, double p, int q) {
            name = n;
            price = p;
            quantity = q;
        }
        // get methods
        public String getName() {
            return name;
        }
        public double getPrice() {
            return price;
        }
        public int getQuantity() {
            return quantity;
        }
        // instance variables 
        private final String name;
        private final double price;
        private int quantity;
    }
    private final static Map<String, Item> stock = new HashMap();
    static {
        // if you want to have extra stock items, put them in here
        // use the same style - keys should be Strings
        stock.put("00", new Item("Bath towel", 5.50, 10));
        stock.put("11", new Item("Plebney light", 20.00, 5));
        stock.put("22", new Item("Gorilla suit", 30.00, 7));
        stock.put("33", new Item("Whizz games console", 50.00, 8));
        stock.put("44", new Item("Oven", 200.00, 4));
    }
    public static Map<String, Item> getStock() {
        return stock;
       }
    public static String getName(String key) {
        Item item = stock.get(key);
        if (item == null) {
            return null; // null means no such item
        } else {
            return item.getName();
        }
    }
    public static double getPrice(String key) {
        Item item = stock.get(key);
        if (item == null) {
            return -1.0; // negative price means no such item
        } else {
            return item.getPrice();
        }
    }
    public static int getQuantity(String key) {
        Item item = stock.get(key);
        if (item == null) {
            return -1; // negative quantity means no such item
        } else {
            return item.getQuantity();
        }
    }
    public static void update(String key, int extra) {
        Item item = stock.get(key);
        if (item != null) {
            item.quantity += extra;
        }
    }
    public static void close() {
        // Does nothing for this static version.
        // Write a statement to close the database when you are using one
    }
}

您可以从任何位置访问此数据,因为它是静态定义的。 例如,您可以编写一个 Sandbox 类来运行如下所示的主函数:

public class Sandbox {
    public static void main (String[] args)
    {
        for(String key : StockData.getStock().keySet()) {
            System.out.println("Item: " + key);
            System.out.println("Name: " + StockData.getName(key) +
                    "  Price: " + StockData.getPrice(key) +
                    " Qty: " + StockData.getQuantity(key));
        }
    }
}

这输出:

Item: 00
Name: Bath towel  Price: 5.5 Qty: 10
Item: 11
Name: Plebney light  Price: 20.0 Qty: 5
Item: 22
Name: Gorilla suit  Price: 30.0 Qty: 7
Item: 33
Name: Whizz games console  Price: 50.0 Qty: 8
Item: 44
Name: Oven  Price: 200.0 Qty: 4

最新更新