-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryManagementSystem.java
More file actions
367 lines (301 loc) · 14.9 KB
/
InventoryManagementSystem.java
File metadata and controls
367 lines (301 loc) · 14.9 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package semesterproject;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.util.Stack;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class InventoryManagementSystem extends JFrame {
Stack<Item> stack;
DefaultTableModel model;
JTable table;
JTextField nameBox, qtyBox, priceBox;
JTextArea logBox;
JLabel countLabel, valueLabel, statusLabel;
public InventoryManagementSystem() {
stack = new Stack<Item>();
setTitle("Inventory Management System");
setSize(1000, 700);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
getContentPane().setBackground(new Color(236, 240, 241));
setLayout(new BorderLayout(10, 10));
// Top Panel
JPanel top = new JPanel(new BorderLayout());
top.setBackground(new Color(41, 128, 185));
top.setBorder(new EmptyBorder(15, 20, 15, 20));
JLabel title = new JLabel("INVENTORY MANAGEMENT SYSTEM");
title.setFont(new Font("Segoe UI", Font.BOLD, 24));
title.setForeground(Color.WHITE);
JLabel subtitle = new JLabel("Stack-Based Data Structure Implementation");
subtitle.setFont(new Font("Segoe UI", Font.PLAIN, 14));
subtitle.setForeground(new Color(236, 240, 241));
JPanel titles = new JPanel(new GridLayout(2, 1));
titles.setBackground(new Color(41, 128, 185));
titles.add(title);
titles.add(subtitle);
top.add(titles, BorderLayout.WEST);
add(top, BorderLayout.NORTH);
// Split
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
split.setDividerLocation(650);
// Left Panel
JPanel left = new JPanel(new BorderLayout(10, 10));
left.setBackground(new Color(236, 240, 241));
left.setBorder(new EmptyBorder(10, 10, 10, 5));
// Table Panel
JPanel tableBox = new JPanel(new BorderLayout());
tableBox.setBackground(Color.WHITE);
tableBox.setBorder(BorderFactory.createCompoundBorder(
new TitledBorder(new LineBorder(new Color(41, 128, 185), 2),
"Current Inventory Stack", TitledBorder.LEFT, TitledBorder.TOP,
new Font("Segoe UI", Font.BOLD, 14), new Color(41, 128, 185)),
new EmptyBorder(10, 10, 10, 10)));
String[] cols = {"Position", "Item Name", "Quantity", "Price ($)", "Total Value ($)"};
model = new DefaultTableModel(cols, 0) {
public boolean isCellEditable(int r, int c) { return false; }
};
table = new JTable(model);
table.setFont(new Font("Segoe UI", Font.PLAIN, 13));
table.setRowHeight(30);
JTableHeader header = table.getTableHeader();
header.setFont(new Font("Segoe UI", Font.BOLD, 13));
header.setBackground(Color.WHITE);
header.setForeground(Color.BLACK);
table.setSelectionBackground(new Color(52, 152, 219));
table.getColumnModel().getColumn(0).setPreferredWidth(80);
table.getColumnModel().getColumn(1).setPreferredWidth(200);
table.getColumnModel().getColumn(2).setPreferredWidth(80);
table.getColumnModel().getColumn(3).setPreferredWidth(80);
table.getColumnModel().getColumn(4).setPreferredWidth(100);
tableBox.add(new JScrollPane(table));
left.add(tableBox, BorderLayout.CENTER);
// Button Panel
JPanel buttonBox = new JPanel(new BorderLayout(10, 10));
buttonBox.setBackground(Color.WHITE);
buttonBox.setBorder(BorderFactory.createCompoundBorder(
new TitledBorder(new LineBorder(new Color(41, 128, 185), 2),
"Inventory Operations", TitledBorder.LEFT, TitledBorder.TOP,
new Font("Segoe UI", Font.BOLD, 14), new Color(41, 128, 185)),
new EmptyBorder(10, 10, 10, 10)));
JPanel inputs = new JPanel(new GridLayout(3, 2, 10, 10));
inputs.setBackground(Color.WHITE);
JLabel l1 = new JLabel("Item Name:");
l1.setFont(new Font("Segoe UI", Font.PLAIN, 13));
nameBox = new JTextField();
nameBox.setFont(new Font("Segoe UI", Font.PLAIN, 13));
nameBox.setBorder(BorderFactory.createCompoundBorder(new LineBorder(new Color(189, 195, 199)), new EmptyBorder(5, 5, 5, 5)));
JLabel l2 = new JLabel("Quantity:");
l2.setFont(new Font("Segoe UI", Font.PLAIN, 13));
qtyBox = new JTextField();
qtyBox.setFont(new Font("Segoe UI", Font.PLAIN, 13));
qtyBox.setBorder(BorderFactory.createCompoundBorder(new LineBorder(new Color(189, 195, 199)), new EmptyBorder(5, 5, 5, 5)));
JLabel l3 = new JLabel("Unit Price ($):");
l3.setFont(new Font("Segoe UI", Font.PLAIN, 13));
priceBox = new JTextField();
priceBox.setFont(new Font("Segoe UI", Font.PLAIN, 13));
priceBox.setBorder(BorderFactory.createCompoundBorder(new LineBorder(new Color(189, 195, 199)), new EmptyBorder(5, 5, 5, 5)));
inputs.add(l1);
inputs.add(nameBox);
inputs.add(l2);
inputs.add(qtyBox);
inputs.add(l3);
inputs.add(priceBox);
JPanel buttons = new JPanel(new GridLayout(1, 3, 10, 0));
buttons.setBackground(Color.WHITE);
buttons.setBorder(new EmptyBorder(10, 0, 0, 0));
JButton btn1 = makeButton("PUSH (Add)", new Color(39, 174, 96), new Color(30, 140, 70));
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { addItem(); }
});
JButton btn2 = makeButton("POP (Remove)", new Color(231, 76, 60), new Color(180, 60, 50));
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { removeItem(); }
});
JButton btn3 = makeButton("PEEK (View Top)", new Color(41, 128, 185), new Color(30, 100, 150));
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { viewTop(); }
});
nameBox.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) { if (e.getKeyCode() == 10) addItem(); }
});
buttons.add(btn1);
buttons.add(btn2);
buttons.add(btn3);
buttonBox.add(inputs, BorderLayout.CENTER);
buttonBox.add(buttons, BorderLayout.SOUTH);
left.add(buttonBox, BorderLayout.SOUTH);
// Right Panel
JPanel right = new JPanel(new BorderLayout(10, 10));
right.setBackground(new Color(236, 240, 241));
right.setBorder(new EmptyBorder(10, 5, 10, 10));
JPanel stats = new JPanel(new GridLayout(2, 1, 5, 5));
stats.setBackground(Color.WHITE);
stats.setBorder(BorderFactory.createCompoundBorder(
new TitledBorder(new LineBorder(new Color(41, 128, 185), 2),
"Statistics", TitledBorder.LEFT, TitledBorder.TOP,
new Font("Segoe UI", Font.BOLD, 14), new Color(41, 128, 185)),
new EmptyBorder(10, 10, 10, 10)));
countLabel = new JLabel("Total Items in Stack: 0");
countLabel.setFont(new Font("Segoe UI", Font.BOLD, 14));
valueLabel = new JLabel("Total Inventory Value: $0.00");
valueLabel.setFont(new Font("Segoe UI", Font.BOLD, 14));
stats.add(countLabel);
stats.add(valueLabel);
right.add(stats, BorderLayout.NORTH);
JPanel logs = new JPanel(new BorderLayout());
logs.setBackground(Color.WHITE);
logs.setBorder(BorderFactory.createCompoundBorder(
new TitledBorder(new LineBorder(new Color(41, 128, 185), 2),
"Activity Log", TitledBorder.LEFT, TitledBorder.TOP,
new Font("Segoe UI", Font.BOLD, 14), new Color(41, 128, 185)),
new EmptyBorder(10, 10, 10, 10)));
logBox = new JTextArea();
logBox.setEditable(false);
logBox.setFont(new Font("Consolas", Font.PLAIN, 12));
logs.add(new JScrollPane(logBox));
right.add(logs, BorderLayout.CENTER);
split.setLeftComponent(left);
split.setRightComponent(right);
add(split, BorderLayout.CENTER);
// Bottom Panel
JPanel bottom = new JPanel(new BorderLayout());
bottom.setBackground(new Color(52, 73, 94));
bottom.setBorder(new EmptyBorder(10, 20, 10, 20));
statusLabel = new JLabel("System Ready");
statusLabel.setFont(new Font("Segoe UI", Font.PLAIN, 12));
statusLabel.setForeground(Color.WHITE);
JLabel copy = new JLabel("© 2025 Inventory Management System | Semester Project");
copy.setFont(new Font("Segoe UI", Font.PLAIN, 11));
copy.setForeground(new Color(189, 195, 199));
bottom.add(statusLabel, BorderLayout.WEST);
bottom.add(copy, BorderLayout.EAST);
add(bottom, BorderLayout.SOUTH);
setVisible(true);
}
public JButton makeButton(String text, Color bg, Color hover) {
JButton btn = new JButton(text);
btn.setFont(new Font("Segoe UI", Font.BOLD, 12));
btn.setBackground(bg);
btn.setForeground(Color.WHITE);
btn.setFocusPainted(false);
btn.setBorderPainted(false);
btn.setPreferredSize(new Dimension(120, 40));
btn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) { btn.setBackground(hover); }
public void mouseExited(MouseEvent e) { btn.setBackground(bg); }
});
return btn;
}
public void addItem() {
String name = nameBox.getText().trim();
String qty = qtyBox.getText().trim();
String price = priceBox.getText().trim();
if (name.isEmpty() || qty.isEmpty() || price.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in all fields!", "Input Error", JOptionPane.WARNING_MESSAGE);
return;
}
try {
int q = Integer.parseInt(qty);
double p = Double.parseDouble(price);
if (q <= 0 || p < 0) {
JOptionPane.showMessageDialog(this, "Quantity must be positive and price cannot be negative!", "Invalid Input", JOptionPane.WARNING_MESSAGE);
return;
}
stack.push(new Item(name, q, p));
refresh();
nameBox.setText("");
qtyBox.setText("");
priceBox.setText("");
nameBox.requestFocus();
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
logBox.append("[" + time + "] PUSH: Added '" + name + "' (Qty: " + q + ", Price: $" + p + ")\n");
logBox.setCaretPosition(logBox.getDocument().getLength());
setStatus("Item added successfully!", new Color(39, 174, 96));
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Please enter valid numbers for quantity and price!", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}
public void removeItem() {
if (stack.isEmpty()) {
JOptionPane.showMessageDialog(this, "Stack is empty! No items to remove.", "Empty Stack", JOptionPane.WARNING_MESSAGE);
return;
}
if (JOptionPane.showConfirmDialog(this, "Remove the top item from the stack?", "Confirm Pop Operation", JOptionPane.YES_NO_OPTION) == 0) {
Item item = stack.pop();
refresh();
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
logBox.append("[" + time + "] POP: Removed '" + item.name + "' (Qty: " + item.qty + ", Price: $" + item.price + ")\n");
logBox.setCaretPosition(logBox.getDocument().getLength());
setStatus("Item removed successfully!", new Color(231, 76, 60));
}
}
public void viewTop() {
if (stack.isEmpty()) {
JOptionPane.showMessageDialog(this, "Stack is empty! No items to view.", "Empty Stack", JOptionPane.INFORMATION_MESSAGE);
return;
}
Item item = stack.peek();
String msg = "Top Item Details:\n\n" + "Item Name: " + item.name + "\n" + "Quantity: " + item.qty + "\n" +
"Unit Price: $" + String.format("%.2f", item.price) + "\n" + "Total Value: $" + String.format("%.2f", item.getTotal()) + "\n" + "Position: Top of Stack";
JOptionPane.showMessageDialog(this, msg, "Peek Operation", JOptionPane.INFORMATION_MESSAGE);
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
logBox.append("[" + time + "] PEEK: Viewed '" + item.name + "' at top of stack\n");
logBox.setCaretPosition(logBox.getDocument().getLength());
}
public void setStatus(String text, Color color) {
statusLabel.setText(text);
statusLabel.setForeground(color);
new Timer(3000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText("System Ready");
statusLabel.setForeground(Color.WHITE);
}
}).start();
}
public void refresh() {
for (int i = model.getRowCount() - 1; i >= 0; i--) model.removeRow(i);
if (stack.isEmpty()) {
countLabel.setText("Total Items in Stack: 0");
valueLabel.setText("Total Inventory Value: $0.00");
return;
}
Stack<Item> temp = new Stack<Item>();
for (int i = 0; i < stack.size(); i++) temp.push(stack.get(i));
Stack<Item> display = new Stack<Item>();
while (!temp.isEmpty()) display.push(temp.pop());
int pos = 1;
while (!display.isEmpty()) {
Item item = display.pop();
Object[] data = {pos, item.name, item.qty, String.format("%.2f", item.price), String.format("%.2f", item.getTotal())};
model.addRow(data);
pos++;
}
int total = 0;
double value = 0.0;
for (int i = 0; i < stack.size(); i++) {
total = total + stack.get(i).qty;
value = value + stack.get(i).getTotal();
}
countLabel.setText("Total Items in Stack: " + stack.size());
valueLabel.setText("Total Inventory Value: $" + String.format("%.2f", value));
}
class Item {
String name;
int qty;
double price;
Item(String n, int q, double p) {
name = n;
qty = q;
price = p;
}
double getTotal() {
return qty * price;
}
}
public static void main(String[] args) {
new InventoryManagementSystem();
}
}