Using hashmap to display data in JTextField and update it

1.4k Views Asked by At

Below is code that creates items that have code number name, price and quantity respectively.

 public class StockData {
    private static class Item {
        Item(String n, double p, int q) {
            name = n;
            price = p;
            quantity = q;
        }
        public String getName() {
            return name;
        }
        public double getPrice() {
            return price;
        }
        public int getQuantity() {
            return quantity;
        }
        private final String name;
        private final double price;
        private int quantity;
    }
    public final static Map<String, Item> stock = new HashMap();
    static {
        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;
        }
    }
}

And here is a different class that is a part of my gui which looks like: https://i.stack.imgur.com/fJ7R5.jpg

and my idea is you type the code of an item eg. 22 then type how many you would like to add to the stock so for example 5 you click add so it adds to the variable but immidiately updates the text in the box as you can see on the screen.

I really got myself puzzled with hashmap / list I don't think there is a point copying all the data from hashmap to list and pretty much multiplying it there must be a better way to achieve this.

public class UpdateStock extends JFrame implements ActionListener {

JTextField stockNo = new JTextField(4);
JButton addButton = new JButton("ADD");
JSpinner quantitySlider = new JSpinner();
JTextArea catalog = new JTextArea(7, 30);
List items = new ArrayList();

public UpdateStock(){
    setLayout(new BorderLayout());
    setBounds(100, 100, 450, 500);
    setTitle("Update Stock");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    JPanel top = new JPanel();
    add("North", top);
    JPanel middle = new JPanel();
    add("Center", middle);

    top.add(stockNo);
    top.add(quantitySlider);
    top.add(addButton);

    catalog.setLineWrap(true);
    catalog.setWrapStyleWord(true);
    catalog.setEditable(false);
    middle.add(new JScrollPane(catalog));
    for(String key : StockData.getStock().keySet()) {
        catalog.append("Item: " + key +"\n");
        items.add(StockData.getName(key));
        catalog.append("Name: " + StockData.getName(key) +
                "  Price: " + StockData.getPrice(key) +
                " Qty: " + StockData.getQuantity(key)+"\n");
    }
    setResizable(false);
    setVisible(true);
}

}

1

There are 1 best solutions below

1
On

your code immediately puts text in the JTextArea because you tell it to. It's right there in the constructor:

for(String key : StockData.getStock().keySet()) {
    catalog.append("Item: " + key +"\n");
    items.add(StockData.getName(key));
    catalog.append("Name: " + StockData.getName(key) +
            "  Price: " + StockData.getPrice(key) +
            " Qty: " + StockData.getQuantity(key)+"\n");
}

If you want to wait until the user picks an item before setting any text, then register an ActionListener on addButton using its addActionListener() method. Use that listener's actionPerformed() method to set the text. Don't forget to remove the code shown above from your constructor, too.

I see you already know about the ActionListener class, since it's implemented by UpdateStock, but it's a little weird (though totally valid!) to do it that way; I don't think I've seen many subclasses of JFrame implement it directly. The usual pattern is to use an anonymous ActionListener and just register that instead. If you really want to use UpdateStock as an ActionListener, then you'll need an actionPerformed() method defined in UpdateStock and you'll need to register this as an action listener on your button.