-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductPanel.java
More file actions
173 lines (147 loc) · 4.84 KB
/
ProductPanel.java
File metadata and controls
173 lines (147 loc) · 4.84 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
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class ProductPanel extends JPanel {
private JLabel productCodeLabel;
private JTextField productCodeField;
private JLabel quantityLabel;
private JTextField quantityField;
private JButton addButton;
private JButton removeButton;
private JButton showButton;
private DataModel model;
public ProductPanel(DataModel model) {
this.model = model;
setLayout(new BorderLayout(10, 10));
// Input Panel
JPanel inputPanel = new JPanel(new GridLayout(2, 2, 10, 10));
// Product Code
productCodeLabel = new JLabel("Product Code:", SwingConstants.RIGHT);
productCodeField = new JTextField(15);
inputPanel.add(productCodeLabel);
inputPanel.add(productCodeField);
// Quantity
quantityLabel = new JLabel("Quantity:", SwingConstants.RIGHT);
quantityField = new JTextField(15);
inputPanel.add(quantityLabel);
inputPanel.add(quantityField);
// Button Panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
addButton = new JButton("Add Item");
removeButton = new JButton("Remove Item");
showButton = new JButton("Show Products");
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
buttonPanel.add(showButton);
// Add panels to main panel
add(inputPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
// Add action listeners
addButton.addActionListener(_ -> addProduct());
removeButton.addActionListener(_ -> removeProduct());
showButton.addActionListener(_ -> showProducts());
}
private void addProduct() {
String productCode = productCodeField.getText().trim();
String quantity = quantityField.getText().trim();
if (!model.isValidProduct(productCode)) {
JOptionPane.showMessageDialog(this,
"The product code entered does not exist.",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if (!model.isValidQuantity(quantity)) {
JOptionPane.showMessageDialog(this,
"Please enter a valid quantity.",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if (!model.addProductToInvoice(productCode, Integer.parseInt(quantity))) {
JOptionPane.showMessageDialog(this,
"Product already exists in the invoice.",
"Error",
JOptionPane.ERROR_MESSAGE);
}
clearFields();
}
private void removeProduct() {
String productCode = productCodeField.getText().trim();
String quantityStr = quantityField.getText().trim();
if (productCode.isEmpty() || quantityStr.isEmpty()) {
JOptionPane.showMessageDialog(this,
"Product code cannot be emty. Please enter both product code and quantity",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
int quantity;
try {
quantity = Integer.parseInt(quantityStr);
if (quantity <= 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this,
"Quantity entered must be a postive number",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if (!model.removeProductFromInvoice(productCode, quantity)) {
JOptionPane.showMessageDialog(this,
"The product code does not exist in the invoice, or the quantity exceeds the available amount.",
"Error",
JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(this,
"Removed " + quantity + " units of product code " + productCode + " successfully.",
"Success",
JOptionPane.INFORMATION_MESSAGE);
}
clearFields();
}
private void showProducts() {
if (!model.isInventoryLoaded()) {
JOptionPane.showMessageDialog(this,
"Please load the inventory first.",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
String searchCode = productCodeField.getText().trim();
JFrame productList = new JFrame("Product List");
productList.setLayout(new BorderLayout());
JTextArea productArea = new JTextArea(20, 50);
productArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(productArea);
// If search contains wildcard, show filtered results
if (searchCode.contains("*")) {
String prefix = searchCode.replace("*", "");
productArea.setText(model.getFilteredProducts(prefix));
} else {
productArea.setText(model.getAllProducts());
}
JButton closeButton = new JButton("Close");
closeButton.addActionListener(_ -> productList.dispose());
productList.add(scrollPane, BorderLayout.CENTER);
productList.add(closeButton, BorderLayout.SOUTH);
productList.pack();
productList.setLocationRelativeTo(this);
productList.setVisible(true);
}
private void clearFields() {
productCodeField.setText("");
quantityField.setText("");
}
}