-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.java
More file actions
241 lines (208 loc) · 8.54 KB
/
gui.java
File metadata and controls
241 lines (208 loc) · 8.54 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
package myproject;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class InventoryGUI extends JFrame {
private InventoryManager inventoryManager;
private JTable productTable;
private DefaultTableModel tableModel;
private JTextField productNameField;
private JTextField quantityField;
private JTextField newProductNameField;
private JTextField newProductStockField;
private JLabel statusLabel;
public InventoryGUI() {
inventoryManager = new InventoryManager();
inventoryManager.addProduct(new Product("Eggs", 100));
inventoryManager.addProduct(new Product("Meat", 50));
setTitle("Stock Simulator");
setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(10, 10));
setLocationRelativeTo(null);
// Table setup
String[] columnNames = {"Product", "Stock", "Sold", "Remaining"};
tableModel = new DefaultTableModel(columnNames, 0);
productTable = new JTable(tableModel);
productTable.setFont(new Font("Arial", Font.BOLD, 16)); // Set font size for table headers and rows
productTable.setRowHeight(25); // Set row height for data
// Set font size for table headers
JTableHeader header = productTable.getTableHeader();
header.setFont(new Font("Arial", Font.BOLD, 20));
// Center-align table data
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
productTable.setDefaultRenderer(Object.class, centerRenderer);
updateTable();
add(new JScrollPane(productTable), BorderLayout.CENTER);
// Control panel setup
JPanel controlPanel = new JPanel(new BorderLayout());
// Panel for Sell and Restock
JPanel sellRestockPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
sellRestockPanel.add(new JLabel("Product:"), gbc);
productNameField = new JTextField(10);
gbc.gridx = 1;
sellRestockPanel.add(productNameField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
sellRestockPanel.add(new JLabel("Quantity:"), gbc);
quantityField = new JTextField(10);
gbc.gridx = 1;
sellRestockPanel.add(quantityField, gbc);
JButton sellButton = new JButton("Sell");
styleButton(sellButton, Color.RED);
sellButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sellProduct();
}
});
gbc.gridx = 2;
gbc.gridy = 0;
sellRestockPanel.add(sellButton, gbc);
JButton restockButton = new JButton("Restock");
styleButton(restockButton, Color.GREEN);
restockButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
restockProduct();
}
});
gbc.gridx = 2;
gbc.gridy = 1;
sellRestockPanel.add(restockButton, gbc);
// Panel for Add Product
JPanel addProductPanel = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
addProductPanel.add(new JLabel("New Product Name:"), gbc);
newProductNameField = new JTextField(10);
gbc.gridx = 1;
addProductPanel.add(newProductNameField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
addProductPanel.add(new JLabel("New Product Stock:"), gbc);
newProductStockField = new JTextField(10);
gbc.gridx = 1;
addProductPanel.add(newProductStockField, gbc);
JButton addProductButton = new JButton("Add Product");
styleButton(addProductButton, Color.BLUE);
addProductButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addProduct();
}
});
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
addProductPanel.add(addProductButton, gbc);
// Add Sell/Restock and Add Product panels to controlPanel
controlPanel.add(sellRestockPanel, BorderLayout.WEST);
controlPanel.add(addProductPanel, BorderLayout.EAST);
add(controlPanel, BorderLayout.NORTH);
// Status label
statusLabel = new JLabel(" ");
statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
statusLabel.setForeground(Color.BLUE);
add(statusLabel, BorderLayout.SOUTH);
// Display the initial table data
updateTable();
}
private void sellProduct() {
String productName = productNameField.getText();
if (productName.isEmpty()) {
showStatus("Product name cannot be empty!", Color.RED);
return;
}
try {
int quantity = Integer.parseInt(quantityField.getText());
Product product = inventoryManager.getProduct(productName);
if (product != null) {
product.sell(quantity);
updateTable();
showStatus("Sold " + quantity + " " + productName, Color.GREEN);
} else {
showStatus("Product not found!", Color.RED);
}
} catch (NumberFormatException e) {
showStatus("Quantity must be a number!", Color.RED);
}
}
private void restockProduct() {
String productName = productNameField.getText();
if (productName.isEmpty()) {
showStatus("Product name cannot be empty!", Color.RED);
return;
}
try {
int quantity = Integer.parseInt(quantityField.getText());
Product product = inventoryManager.getProduct(productName);
if (product != null) {
product.restock(quantity);
updateTable();
showStatus("Restocked " + quantity + " " + productName, Color.GREEN);
} else {
showStatus("Product not found!", Color.RED);
}
} catch (NumberFormatException e) {
showStatus("Quantity must be a number!", Color.RED);
}
}
private void addProduct() {
String newProductName = newProductNameField.getText();
if (newProductName.isEmpty()) {
showStatus("Product name cannot be empty!", Color.RED);
return;
}
try {
int newProductStock = Integer.parseInt(newProductStockField.getText());
Product newProduct = new Product(newProductName, newProductStock);
inventoryManager.addProduct(newProduct);
updateTable();
showStatus("Added new product: " + newProductName, Color.GREEN);
} catch (NumberFormatException e) {
showStatus("Stock must be a number!", Color.RED);
}
}
private void updateTable() {
tableModel.setRowCount(0); // Clear the table
for (Product product : inventoryManager.getProducts()) {
Object[] rowData = {
product.getName(),
product.getStock(),
product.getSold(),
product.getRemaining()
};
tableModel.addRow(rowData);
}
}
private void showStatus(String message, Color color) {
statusLabel.setText(message);
statusLabel.setForeground(color);
}
private void styleButton(JButton button, Color color) {
button.setBackground(color);
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setPreferredSize(new Dimension(120, 30)); // Adjust size as needed
button.setFont(new Font("Arial", Font.BOLD, 12));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new InventoryGUI().setVisible(true);
}
});
}
}