Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.util.*;
- public class InventorySystem extends JFrame {
- // Declare constants
- public static final int NUM_ITEMS = 20;
- public static final int WIDTH = 500;
- public static final int HEIGHT = 500;
- // Declare instance variables
- private JList itemList;
- private DefaultListModel model;
- private JTextField nameField;
- private JTextField serialField;
- private JTextField quantityField;
- private JTextField priceField;
- private JTextField searchField;
- private JButton addButton;
- private JButton updateButton;
- private JButton deleteButton;
- private JButton clearButton;
- private JButton searchButton;
- private ArrayList<Item> items;
- public static void main(String[] args) {
- new InventorySystem();
- }
- public InventorySystem() {
- // Initialize instance variables
- model = new DefaultListModel();
- itemList = new JList(model);
- nameField = new JTextField(10);
- serialField = new JTextField(10);
- quantityField = new JTextField(10);
- priceField = new JTextField(10);
- searchField = new JTextField(10);
- addButton = new JButton("Add");
- updateButton = new JButton("Update");
- deleteButton = new JButton("Delete");
- clearButton = new JButton("Clear");
- searchButton = new JButton("Search");
- items = new ArrayList<Item>();
- // Set up layout
- JPanel panel = new JPanel();
- panel.setLayout(new GridBagLayout());
- GridBagConstraints gc = new GridBagConstraints();
- // Add items to layout
- gc.gridx = 0;
- gc.gridy = 0;
- gc.gridheight = 6;
- gc.weightx = 1;
- gc.weighty = 1;
- gc.fill = GridBagConstraints.BOTH;
- panel.add(new JScrollPane(itemList), gc);
- gc.gridx = 1;
- gc.gridy = 0;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.NONE;
- panel.add(new JLabel("Name: "), gc);
- gc.gridx = 2;
- gc.gridy = 0;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.HORIZONTAL;
- panel.add(nameField, gc);
- gc.gridx = 1;
- gc.gridy = 1;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.NONE;
- panel.add(new JLabel("Serial Number: "), gc);
- gc.gridx = 2;
- gc.gridy = 1;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.HORIZONTAL;
- panel.add(serialField, gc);
- gc.gridx = 1;
- gc.gridy = 2;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.NONE;
- panel.add(new JLabel("Quantity: "), gc);
- gc.gridx = 2;
- gc.gridy = 2;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.HORIZONTAL;
- panel.add(quantityField, gc);
- gc.gridx = 1;
- gc.gridy = 3;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.NONE;
- panel.add(new JLabel("Price: "), gc);
- gc.gridx = 2;
- gc.gridy = 3;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.HORIZONTAL;
- panel.add(priceField, gc);
- gc.gridx = 1;
- gc.gridy = 4;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.NONE;
- panel.add(addButton, gc);
- gc.gridx = 2;
- gc.gridy = 4;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.HORIZONTAL;
- panel.add(updateButton, gc);
- gc.gridx = 1;
- gc.gridy = 5;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.NONE;
- panel.add(deleteButton, gc);
- gc.gridx = 2;
- gc.gridy = 5;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.HORIZONTAL;
- panel.add(clearButton, gc);
- gc.gridx = 1;
- gc.gridy = 6;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.NONE;
- panel.add(new JLabel("Search: "), gc);
- gc.gridx = 2;
- gc.gridy = 6;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.HORIZONTAL;
- panel.add(searchField, gc);
- gc.gridx = 3;
- gc.gridy = 6;
- gc.gridheight = 1;
- gc.weightx = 0;
- gc.weighty = 0;
- gc.fill = GridBagConstraints.HORIZONTAL;
- panel.add(searchButton, gc);
- // Set up event handling
- addButton.addActionListener(new AddButtonListener());
- updateButton.addActionListener(new UpdateButtonListener());
- deleteButton.addActionListener(new DeleteButtonListener());
- clearButton.addActionListener(new ClearButtonListener());
- searchButton.addActionListener(new SearchButtonListener());
- // Set up the frame
- setTitle("Inventory System");
- setSize(WIDTH, HEIGHT);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- add(panel);
- setVisible(true);
- }
- private class AddButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- String name = nameField.getText();
- String serial = serialField.getText();
- int quantity = 0;
- double price = 0;
- try {
- quantity = Integer.parseInt(quantityField.getText());
- price = Double.parseDouble(priceField.getText());
- } catch (NumberFormatException ex) {
- JOptionPane.showMessageDialog(null, "Invalid input");
- return;
- }
- Item item = new Item(name, serial, quantity, price);
- items.add(item);
- model.addElement(item);
- nameField.setText("");
- serialField.setText("");
- quantityField.setText("");
- priceField.setText("");
- }
- }
- private class UpdateButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- int index = itemList.getSelectedIndex();
- if (index == -1) {
- JOptionPane.showMessageDialog(null, "No item selected");
- return;
- }
- Item item = items.get(index);
- String name = nameField.getText();
- String serial = serialField.getText();
- int quantity = 0;
- double price = 0;
- try {
- quantity = Integer.parseInt(quantityField.getText());
- price = Double.parseDouble(priceField.getText());
- } catch (NumberFormatException ex) {
- JOptionPane.showMessageDialog(null, "Invalid input");
- return;
- }
- item.setName(name);
- item.setSerial(serial);
- item.setQuantity(quantity);
- item.setPrice(price);
- model.set(index, item);
- nameField.setText("");
- serialField.setText("");
- quantityField.setText("");
- priceField.setText("");
- }
- }
- private class DeleteButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- int index = itemList.getSelectedIndex();
- if (index == -1) {
- JOptionPane.showMessageDialog(null, "No item selected");
- return;
- }
- items.remove(index);
- model.remove(index);
- nameField.setText("");
- serialField.setText("");
- quantityField.setText("");
- priceField.setText("");
- }
- }
- private class ClearButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- items.clear();
- model.clear();
- nameField.setText("");
- serialField.setText("");
- quantityField.setText("");
- priceField.setText("");
- }
- }
- private class SearchButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- String search = searchField.getText();
- boolean found = false;
- for (Item item : items) {
- if (item.getSerial().equalsIgnoreCase(search)) {
- nameField.setText(item.getName());
- serialField.setText(item.getSerial());
- quantityField.setText(Integer.toString(item.getQuantity()));
- priceField.setText(Double.toString(item.getPrice()));
- found = true;
- break;
- }
- }
- if (!found) {
- JOptionPane.showMessageDialog(null, "Item not found");
- }
- }
- }
- private class Item {
- private String name;
- private String serial;
- private int quantity;
- private double price;
- public Item(String name, String serial, int quantity, double price) {
- this.name = name;
- this.serial = serial;
- this.quantity = quantity;
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSerial() {
- return serial;
- }
- public void setSerial(String serial) {
- this.serial = serial;
- }
- public int getQuantity() {
- return quantity;
- }
- public void setQuantity(int quantity) {
- this.quantity = quantity;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public String toString() {
- return name + " (" + serial + "): " + quantity + " at $" + price;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement