I'm trying to figure out how to update stock from entering in a number in a text field and then either clicking the add stock or delete stock button to update the stock.
//These are imported classes, within the code you will see codes with the same name //this is because I am refering to classes by name so I dont have to keep typing //import javax.swing. import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JTextField; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.text.DecimalFormat; import java.util.HashMap; import java.util.Map; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.*; public class UpdateStock extends JFrame implements ActionListener { //This code is creating text fields for the user to enter data, buttons for the user to click on //and a text area where text is displayed. The decimal format code sets the format for the price //once it's displayed. JTextField stockNo = new JTextField(7); JTextField newItem = new JTextField(7); JTextField deleteItem = new JTextField(7); JLabel numOneLabel = new JLabel("Add Stock:"); JLabel numTwoLabel = new JLabel("Enter Stock Number:"); JLabel numthreeLabel = new JLabel("Delete Stock:"); TextArea information = new TextArea(6, 70); JButton check = new JButton("Add Stock"); JButton delete = new JButton("Delete Stock"); JButton update = new JButton("Check Stock"); DecimalFormat pounds = new DecimalFormat("£#,##0.00"); JPanel jp = new JPanel(); JLabel jl = new JLabel(); ImageIcon icon3 = new ImageIcon("plus.png"); ImageIcon icon2 = new ImageIcon("minus.png"); public static void main(String[] args) { UpdateStock us = new UpdateStock(); } String key; int extra; public int getQuantity() { return quantity; } public int quantity; public UpdateStock() { //This code allows us to create the size, title and settings for the pop up menu. //This is where you can add the buttons and text fields/areas, you can tell the box //where it will be popping up. The code listens for if the buttons are clicked //then activates a code. setLayout(new BorderLayout()); setBounds(100, 100, 650, 400); setTitle("Update Stock"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JPanel top = new JPanel(); //top.add(new JLabel("Enter Stock Number:")); top.add(numTwoLabel); top.add(stockNo); top.setBackground(Color.CYAN); add(top); top.add(update); check.addActionListener(this); update.addActionListener(this); add("North", top); JPanel middle = new JPanel(); middle.add(numOneLabel); middle.add(newItem); middle.add(check); middle.add(numthreeLabel); middle.add(deleteItem); middle.add(delete); middle.add(information); middle.add(jp); middle.add(new JLabel("Enter in a number from 1 - 6 to Update the Stock")); middle.setBackground(Color.WHITE); add("Center", middle); check.setIcon(icon3); delete.setIcon(icon2); //This code is saying if I want box to be rezideable and visible to the user. setResizable(false); setVisible(true); } //This code is performing an action for when text is entered into the text field //if the text in the field is unrecognisable then the code will display an error message //and if the text is found relateable then it will display the price in pounds format //and how many there are in stock. public void actionPerformed(ActionEvent e) { if (e.getSource()== update) { String key = stockNo.getText(); String name = StockData.getName(key); if (name == null) { information.setText("No such item in stock"); } else { information.setText(name); information.append("\nPrice: " + pounds.format(StockData.getPrice(key))); information.append("\nNumber in stock: " + StockData.getQuantity(key)); information.append("\nDescription: " + StockData.Description(key)); } } } public void update(String key, int extra, ActionEvent e) { // if (e.getSource()== check) { //JOptionPane.showMessageDialog (null, "This is a message box"); // StockData.Item item = stock.get(key); // if (item != null) item.quantity += extra; } }