Advertisement
grhey

Inventory System

Jan 2nd, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.21 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import java.util.*;
  6.  
  7. public class InventorySystem extends JFrame {
  8.   // Declare constants
  9.   public static final int NUM_ITEMS = 20;
  10.   public static final int WIDTH = 500;
  11.   public static final int HEIGHT = 500;
  12.  
  13.   // Declare instance variables
  14.   private JList itemList;
  15.   private DefaultListModel model;
  16.   private JTextField nameField;
  17.   private JTextField serialField;
  18.   private JTextField quantityField;
  19.   private JTextField priceField;
  20.   private JTextField searchField;
  21.   private JButton addButton;
  22.   private JButton updateButton;
  23.   private JButton deleteButton;
  24.   private JButton clearButton;
  25.   private JButton searchButton;
  26.   private ArrayList<Item> items;
  27.  
  28.   public static void main(String[] args) {
  29.     new InventorySystem();
  30.   }
  31.  
  32.   public InventorySystem() {
  33.     // Initialize instance variables
  34.     model = new DefaultListModel();
  35.     itemList = new JList(model);
  36.     nameField = new JTextField(10);
  37.     serialField = new JTextField(10);
  38.     quantityField = new JTextField(10);
  39.     priceField = new JTextField(10);
  40.     searchField = new JTextField(10);
  41.     addButton = new JButton("Add");
  42.     updateButton = new JButton("Update");
  43.     deleteButton = new JButton("Delete");
  44.     clearButton = new JButton("Clear");
  45.     searchButton = new JButton("Search");
  46.     items = new ArrayList<Item>();
  47.  
  48.     // Set up layout
  49.     JPanel panel = new JPanel();
  50.     panel.setLayout(new GridBagLayout());
  51.     GridBagConstraints gc = new GridBagConstraints();
  52.  
  53.     // Add items to layout
  54.     gc.gridx = 0;
  55.     gc.gridy = 0;
  56.     gc.gridheight = 6;
  57.     gc.weightx = 1;
  58.     gc.weighty = 1;
  59.     gc.fill = GridBagConstraints.BOTH;
  60.     panel.add(new JScrollPane(itemList), gc);
  61.  
  62.     gc.gridx = 1;
  63.     gc.gridy = 0;
  64.     gc.gridheight = 1;
  65.     gc.weightx = 0;
  66.     gc.weighty = 0;
  67.     gc.fill = GridBagConstraints.NONE;
  68.     panel.add(new JLabel("Name: "), gc);
  69.  
  70.     gc.gridx = 2;
  71.     gc.gridy = 0;
  72.     gc.gridheight = 1;
  73.     gc.weightx = 0;
  74.     gc.weighty = 0;
  75.     gc.fill = GridBagConstraints.HORIZONTAL;
  76.     panel.add(nameField, gc);
  77.  
  78.     gc.gridx = 1;
  79.     gc.gridy = 1;
  80.     gc.gridheight = 1;
  81.      gc.weightx = 0;
  82.     gc.weighty = 0;
  83.     gc.fill = GridBagConstraints.NONE;
  84.     panel.add(new JLabel("Serial Number: "), gc);
  85.  
  86.     gc.gridx = 2;
  87.     gc.gridy = 1;
  88.     gc.gridheight = 1;
  89.     gc.weightx = 0;
  90.     gc.weighty = 0;
  91.     gc.fill = GridBagConstraints.HORIZONTAL;
  92.     panel.add(serialField, gc);
  93.  
  94.     gc.gridx = 1;
  95.     gc.gridy = 2;
  96.     gc.gridheight = 1;
  97.     gc.weightx = 0;
  98.     gc.weighty = 0;
  99.     gc.fill = GridBagConstraints.NONE;
  100.     panel.add(new JLabel("Quantity: "), gc);
  101.  
  102.     gc.gridx = 2;
  103.     gc.gridy = 2;
  104.     gc.gridheight = 1;
  105.     gc.weightx = 0;
  106.     gc.weighty = 0;
  107.     gc.fill = GridBagConstraints.HORIZONTAL;
  108.     panel.add(quantityField, gc);
  109.  
  110.     gc.gridx = 1;
  111.     gc.gridy = 3;
  112.     gc.gridheight = 1;
  113.     gc.weightx = 0;
  114.     gc.weighty = 0;
  115.     gc.fill = GridBagConstraints.NONE;
  116.     panel.add(new JLabel("Price: "), gc);
  117.  
  118.     gc.gridx = 2;
  119.     gc.gridy = 3;
  120.     gc.gridheight = 1;
  121.     gc.weightx = 0;
  122.     gc.weighty = 0;
  123.     gc.fill = GridBagConstraints.HORIZONTAL;
  124.     panel.add(priceField, gc);
  125.  
  126.     gc.gridx = 1;
  127.     gc.gridy = 4;
  128.     gc.gridheight = 1;
  129.     gc.weightx = 0;
  130.     gc.weighty = 0;
  131.     gc.fill = GridBagConstraints.NONE;
  132.     panel.add(addButton, gc);
  133.  
  134.     gc.gridx = 2;
  135.     gc.gridy = 4;
  136.     gc.gridheight = 1;
  137.     gc.weightx = 0;
  138.     gc.weighty = 0;
  139.     gc.fill = GridBagConstraints.HORIZONTAL;
  140.     panel.add(updateButton, gc);
  141.  
  142.     gc.gridx = 1;
  143.     gc.gridy = 5;
  144.     gc.gridheight = 1;
  145.     gc.weightx = 0;
  146.     gc.weighty = 0;
  147.     gc.fill = GridBagConstraints.NONE;
  148.     panel.add(deleteButton, gc);
  149.  
  150.     gc.gridx = 2;
  151.     gc.gridy = 5;
  152.     gc.gridheight = 1;
  153.     gc.weightx = 0;
  154.      gc.weighty = 0;
  155.     gc.fill = GridBagConstraints.HORIZONTAL;
  156.     panel.add(clearButton, gc);
  157.  
  158.     gc.gridx = 1;
  159.     gc.gridy = 6;
  160.     gc.gridheight = 1;
  161.     gc.weightx = 0;
  162.     gc.weighty = 0;
  163.     gc.fill = GridBagConstraints.NONE;
  164.     panel.add(new JLabel("Search: "), gc);
  165.  
  166.     gc.gridx = 2;
  167.     gc.gridy = 6;
  168.     gc.gridheight = 1;
  169.     gc.weightx = 0;
  170.     gc.weighty = 0;
  171.     gc.fill = GridBagConstraints.HORIZONTAL;
  172.     panel.add(searchField, gc);
  173.  
  174.     gc.gridx = 3;
  175.     gc.gridy = 6;
  176.     gc.gridheight = 1;
  177.     gc.weightx = 0;
  178.     gc.weighty = 0;
  179.     gc.fill = GridBagConstraints.HORIZONTAL;
  180.     panel.add(searchButton, gc);
  181.  
  182.     // Set up event handling
  183.     addButton.addActionListener(new AddButtonListener());
  184.     updateButton.addActionListener(new UpdateButtonListener());
  185.     deleteButton.addActionListener(new DeleteButtonListener());
  186.     clearButton.addActionListener(new ClearButtonListener());
  187.     searchButton.addActionListener(new SearchButtonListener());
  188.  
  189.     // Set up the frame
  190.     setTitle("Inventory System");
  191.     setSize(WIDTH, HEIGHT);
  192.     setDefaultCloseOperation(EXIT_ON_CLOSE);
  193.     add(panel);
  194.     setVisible(true);
  195.   }
  196.  
  197.   private class AddButtonListener implements ActionListener {
  198.     public void actionPerformed(ActionEvent e) {
  199.       String name = nameField.getText();
  200.       String serial = serialField.getText();
  201.       int quantity = 0;
  202.       double price = 0;
  203.       try {
  204.         quantity = Integer.parseInt(quantityField.getText());
  205.         price = Double.parseDouble(priceField.getText());
  206.       } catch (NumberFormatException ex) {
  207.         JOptionPane.showMessageDialog(null, "Invalid input");
  208.         return;
  209.       }
  210.       Item item = new Item(name, serial, quantity, price);
  211.       items.add(item);
  212.       model.addElement(item);
  213.       nameField.setText("");
  214.       serialField.setText("");
  215.       quantityField.setText("");
  216.       priceField.setText("");
  217.     }
  218.   }
  219.  
  220.   private class UpdateButtonListener implements ActionListener {
  221.     public void actionPerformed(ActionEvent e) {
  222.       int index = itemList.getSelectedIndex();
  223.       if (index == -1) {
  224.         JOptionPane.showMessageDialog(null, "No item selected");
  225.         return;
  226.       }
  227.       Item item = items.get(index);
  228.       String name = nameField.getText();
  229.       String serial = serialField.getText();
  230.       int quantity = 0;
  231.       double price = 0;
  232.         try {
  233.         quantity = Integer.parseInt(quantityField.getText());
  234.         price = Double.parseDouble(priceField.getText());
  235.       } catch (NumberFormatException ex) {
  236.         JOptionPane.showMessageDialog(null, "Invalid input");
  237.         return;
  238.       }
  239.       item.setName(name);
  240.       item.setSerial(serial);
  241.       item.setQuantity(quantity);
  242.       item.setPrice(price);
  243.       model.set(index, item);
  244.       nameField.setText("");
  245.       serialField.setText("");
  246.       quantityField.setText("");
  247.       priceField.setText("");
  248.     }
  249.   }
  250.  
  251.   private class DeleteButtonListener implements ActionListener {
  252.     public void actionPerformed(ActionEvent e) {
  253.       int index = itemList.getSelectedIndex();
  254.       if (index == -1) {
  255.         JOptionPane.showMessageDialog(null, "No item selected");
  256.         return;
  257.       }
  258.       items.remove(index);
  259.       model.remove(index);
  260.       nameField.setText("");
  261.       serialField.setText("");
  262.       quantityField.setText("");
  263.       priceField.setText("");
  264.     }
  265.   }
  266.  
  267.   private class ClearButtonListener implements ActionListener {
  268.     public void actionPerformed(ActionEvent e) {
  269.       items.clear();
  270.       model.clear();
  271.       nameField.setText("");
  272.       serialField.setText("");
  273.       quantityField.setText("");
  274.       priceField.setText("");
  275.     }
  276.   }
  277.  
  278.   private class SearchButtonListener implements ActionListener {
  279.     public void actionPerformed(ActionEvent e) {
  280.       String search = searchField.getText();
  281.       boolean found = false;
  282.       for (Item item : items) {
  283.         if (item.getSerial().equalsIgnoreCase(search)) {
  284.           nameField.setText(item.getName());
  285.           serialField.setText(item.getSerial());
  286.           quantityField.setText(Integer.toString(item.getQuantity()));
  287.           priceField.setText(Double.toString(item.getPrice()));
  288.           found = true;
  289.           break;
  290.         }
  291.       }
  292.       if (!found) {
  293.         JOptionPane.showMessageDialog(null, "Item not found");
  294.       }
  295.     }
  296.   }
  297.  
  298.   private class Item {
  299.     private String name;
  300.     private String serial;
  301.     private int quantity;
  302.     private double price;
  303.  
  304.     public Item(String name, String serial, int quantity, double price) {
  305.       this.name = name;
  306.       this.serial = serial;
  307.       this.quantity = quantity;
  308.       this.price = price;
  309.     }
  310.  
  311.     public String getName() {
  312.       return name;
  313.     }
  314.  
  315.     public void setName(String name) {
  316.       this.name = name;
  317.     }
  318.  
  319.     public String getSerial() {
  320.       return serial;
  321.     }
  322.  
  323.     public void setSerial(String serial) {
  324.       this.serial = serial;
  325.     }
  326.  
  327.     public int getQuantity() {
  328.       return quantity;
  329.     }
  330.  
  331.     public void setQuantity(int quantity) {
  332.       this.quantity = quantity;
  333.     }
  334.     public double getPrice() {
  335.       return price;
  336.     }
  337.  
  338.     public void setPrice(double price) {
  339.       this.price = price;
  340.     }
  341.  
  342.     public String toString() {
  343.       return name + " (" + serial + "): " + quantity + " at $" + price;
  344.     }
  345.   }
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement