0

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

}

njasi
  • 126
  • 8
Higeath
  • 541
  • 5
  • 20
  • Didn't I post a fully working example on your other post? – JayC667 Jan 21 '16 at 22:05
  • @JayC667 I've seen that example it is way overcomplicated for me to understand or get through and I don't see a point of using a list copying data from a hashmap to a list and doubling the amount of memory required to store it – Higeath Jan 21 '16 at 22:12
  • Well... uh... it actually just takes 4 bytes more for every *object reference* because the actual object is not getting copied. And yes, it's quite large, but it's a really clean and easily extensible example. And it's the basic idea to have two different lists of items, because stock and cart **HAPPEN TO BE** two completely different lists with different *PURPOSE*. Don't worry about memory size there, you could have at least a million such objects in the app and not have any memory problems. – JayC667 Jan 21 '16 at 22:22
  • @JayC667 but still I would like to have the simplest possible solution so I can actually understand it – Higeath Jan 21 '16 at 22:37

1 Answers1

1

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.

Dan O
  • 6,022
  • 2
  • 32
  • 50
  • I want it to be displayed immidiately like it is now so Gorrila Suit is Quantity is 7 so somebody types 22 (the code for gorilla suit) then 2 and clicks add and I want the JTextArea to get updated immidiately and show 9 instead of 7 that's my problem i don't know how to change just this value – Higeath Jan 21 '16 at 21:40