-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryManager
137 lines (117 loc) · 4.54 KB
/
InventoryManager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class InventoryManager extends JFrame {
private List<Item> inventory;
private JTextField nameField;
private JTextField quantityField;
private JTextArea inventoryArea;
public InventoryManager() {
setTitle("Inventory Manager");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(new JLabel("Item Name:"));
nameField = new JTextField();
inputPanel.add(nameField);
inputPanel.add(new JLabel("Quantity:"));
quantityField = new JTextField();
inputPanel.add(quantityField);
JButton addButton = new JButton("Add Item");
addButton.addActionListener(new AddButtonListener());
inputPanel.add(addButton);
JButton removeButton = new JButton("Remove Item");
removeButton.addActionListener(new RemoveButtonListener());
inputPanel.add(removeButton);
mainPanel.add(inputPanel, BorderLayout.NORTH);
inventoryArea = new JTextArea();
inventoryArea.setEditable(false);
mainPanel.add(new JScrollPane(inventoryArea), BorderLayout.CENTER);
inventory = new ArrayList<>();
add(mainPanel);
setVisible(true);
}
private void updateInventoryArea() {
inventoryArea.setText("");
for (Item item : inventory) {
inventoryArea.append(item.getName() + ": " + item.getQuantity() + "\n");
}
}
private class AddButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText().trim();
String quantityStr = quantityField.getText().trim();
if (!name.isEmpty() && !quantityStr.isEmpty()) {
try {
int quantity = Integer.parseInt(quantityStr);
if (quantity > 0) {
inventory.add(new Item(name, quantity));
updateInventoryArea();
nameField.setText("");
quantityField.setText("");
} else {
JOptionPane.showMessageDialog(InventoryManager.this, "Quantity must be greater than 0", "Error", JOptionPane.ERROR_MESSAGE);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(InventoryManager.this, "Invalid quantity format", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(InventoryManager.this, "Please fill in all fields", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private class RemoveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText().trim();
if (!name.isEmpty()) {
boolean removed = false;
for (Item item : inventory) {
if (item.getName().equals(name)) {
inventory.remove(item);
removed = true;
break;
}
}
if (removed) {
updateInventoryArea();
nameField.setText("");
quantityField.setText("");
} else {
JOptionPane.showMessageDialog(InventoryManager.this, "Item not found", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(InventoryManager.this, "Please enter item name to remove", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(InventoryManager::new);
}
}
class Item {
private String name;
private int quantity;
public Item(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}