-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerGraphicalUserInterface.java
More file actions
379 lines (272 loc) · 13 KB
/
Copy pathCustomerGraphicalUserInterface.java
File metadata and controls
379 lines (272 loc) · 13 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
368
369
370
371
372
373
374
375
376
377
378
379
import org.w3c.dom.ls.LSOutput;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
public class CustomerGraphicalUserInterface extends WestminsterShoppingManager {
public JTable table;
JTable table2;
JPanel firstPanel;
JPanel secondPanel;
JPanel thirdPanel;
JPanel fourthPanel;
JScrollPane scrollPane;
JScrollPane scrollPane2;
ArrayList<Product> filteredList;
ItemsTable tableModel;
JLabel productLabel;
JComboBox productsType;
CartTableModel myCartTable;
Frame2 frame2;
ShoppingCart myShoppingCart;
private User user;
private void updateTable(String selectedType) {
productList = readFile();
filteredList = new ArrayList<>();
if (selectedType.equals("All")) {
filteredList.addAll(productList);
System.out.println(filteredList.size());
System.out.println(" ");
} else {
for (Product product : productList) {
if (product.getClass().getSimpleName().equalsIgnoreCase(selectedType)) {
filteredList.add(product);
System.out.println(filteredList.size());
System.out.println(" ");
}
}
}
if (scrollPane != null) {
firstPanel.remove(scrollPane);
}
tableModel = new ItemsTable(filteredList); // Create model with filtered list
table = new JTable(tableModel);
scrollPane = new JScrollPane(table);
scrollPane.setBounds(250, 120, 850, 200);
scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
table.setGridColor(Color.BLACK);
int i;
int j;
for (i = 0; i < 6; i++) {
for (j = 0; j < filteredList.size(); j++) {
tableModel.getValueAt(j, i);
}
}
table.getColumnModel().getColumn(0).setCellRenderer(new MyTableCellRenderer());
table.getSelectionModel().addListSelectionListener(rowListener);
table.getTableHeader().setBackground(Color.LIGHT_GRAY);
scrollPane.setVisible(true);
firstPanel.add(scrollPane);
firstPanel.revalidate();
firstPanel.repaint();
}
ListSelectionListener rowListener = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
if (table != null) {
if (!event.getValueIsAdjusting()) {
int viewRow = table.getSelectedRow();
if (viewRow != -1) {
int modelRow = table.convertRowIndexToModel(viewRow);
Product product = tableModel.tableProductList.get(modelRow);
updateProductLabel(product); // Call method to update label
//System.out.println("not null");
} else {
clearProductLabel(); // Clear label if no row is selected
}
} else System.out.println("");
}
}
private void updateProductLabel(Product product) {
if (productLabel != null) { // Remove existing label if present
secondPanel.remove(productLabel);
}
if (product instanceof Electronics) {
productLabel = new JLabel("<html>------------------------<br/><br/>Selected Product Information <br/><br/> Product ID : " + product.getProductID() + "<br/><br/>Product name: " + product.getProductName() +
" <br/><br/>Product Category: " + product.getClass().getSimpleName() + " <br/><br/>Brand name: " + ((Electronics) product).getBrandName() + " <br/><br/>Warranty period (Months): " + ((Electronics) product).getWarrantyPeriod()
+ " <br/><br/>No of available items: " + product.getNoOfAvailableItem() + "<br/><br/> -------------------------</html>", SwingConstants.CENTER);
} else if (product instanceof Clothing) {
productLabel = new JLabel("<html>------------------------<br/><br/>Selected Product Information <br/><br/> Product ID : " + product.getProductID() + "<br/><br/>Product name: " + product.getProductName() +
" <br/><br/>Product Category: " + product.getClass().getSimpleName() + " <br/><br/>Size: " + ((Clothing) product).getSize() + " <br/><br/>Colour : " + ((Clothing) product).getColour()
+ " <br/><br/>No of available items: " + product.getNoOfAvailableItem() + "<br/><br/> -------------------------</html>", SwingConstants.CENTER);
}
productLabel.setBounds(400, 350, 400, 200);
productLabel.setVisible(true);
secondPanel.add(productLabel);
//secondPanel.add(productLabel); // Use a layout manager
secondPanel.revalidate(); // Ensure label is displayed
}
private void clearProductLabel() {
if (productLabel != null) {
secondPanel.remove(productLabel);
productLabel = null;
secondPanel.revalidate();
}
}
};
ActionListener cartActionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (table != null) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
int modelRow = table.convertRowIndexToModel(selectedRow);
Product selectedProduct = tableModel.tableProductList.get(modelRow);
/*if (myShoppingCart == null) {
myShoppingCart = new shoppingCart();
}*/
myShoppingCart = user.getMyCart();
myShoppingCart.addToCart(selectedProduct);
//System.out.println("clicked btn"+myShoppingCart.getCart().size());
saveFile();
updateTable(productsType.getSelectedItem().toString());
}
} else
System.out.println("no table");
}
};
ActionListener cbActionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedType = (String) productsType.getSelectedItem();
updateTable(selectedType);
//System.out.println("ActionEvent");
}
};
ItemListener cbItemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
String selectedType = (String) productsType.getSelectedItem();
updateTable(selectedType);
}
}
};
private void updateCartTable()
{
/*if (scrollPane2 != null) {
frame2.remove(scrollPane2);
}*/
myCartTable = new CartTableModel(myShoppingCart); // Create model with filtered list
table2 = new JTable(myCartTable);
scrollPane2 = new JScrollPane(table2);
scrollPane2.setBounds(50, 50, 600, 300);
scrollPane2.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
table2.setGridColor(Color.BLACK);
//System.out.println("MyShopping cart size"+myShoppingCart.getCart().size());
int i;
int j;
for (i = 0; i < 3; i++) {
for (j = 0; j <myShoppingCart.getCart().size() ; j++) {
myCartTable.getValueAt(j, i);
}
}
table2.getTableHeader().setBackground(Color.LIGHT_GRAY);
scrollPane2.setVisible(true);
thirdPanel.add(scrollPane2,BorderLayout.CENTER);
thirdPanel.revalidate();
thirdPanel.repaint();
}
private void finalOutput(User user) {
double discountTen=0;
double discountTwenty=0;
JLabel text1 = new JLabel(" Total : "+ myShoppingCart.totalAmount());
//JLabel text2 = new JLabel(" First Purchase discount (10%) : -" + discount1);
// JLabel text3 = new JLabel(" Three items in the same Category Discount (20%) : -" + discount2);
//JLabel text4 = new JLabel(" Final Total : " + (myShoppingCart.totalAmount() - myShoppingCart.totalAmount() * 0.1 - myShoppingCart.totalAmount() * 0.2));
/* text1.setBounds(250,500,100,40);
text2.setBounds(250,550,100,40);
text3.setBounds(250,600,100,40);*/
fourthPanel.add(text1);
if (user.getNewCustomer() == true) {
discountTen=myShoppingCart.totalAmount() * 0.1;
//JLabel text2 = new JLabel("First Purchase discount (10%) : -" + discount1);
JLabel firstPurchaseDiscounttext2 = new JLabel(" First Purchase discount (10%) : -" + discountTen);
fourthPanel.add(firstPurchaseDiscounttext2);
}
for (CartProducts cartItem : user.getMyCart().getCart())
{
int productCount = cartItem.getCount();
if (productCount >= 3) {
discountTwenty=myShoppingCart.totalAmount() * 0.2;
//JLabel text3 = new JLabel(" Three items in the same Category Discount (20%) : -" + discount2);
JLabel discount20text3 = new JLabel(" Three items in the same Category Discount (20%) : -" + discountTwenty);
fourthPanel.add(discount20text3);
break;
}
}
//JLabel text4 = new JLabel(" Final Total : " + (myShoppingCart.totalAmount() - discount1-discount2));
JLabel text4 = new JLabel(" Final Total : "+ (myShoppingCart.totalAmount() - myShoppingCart.totalAmount() * 0.1 - myShoppingCart.totalAmount() * 0.2));
text4.setBounds(250,650,100,40);
fourthPanel.add(text4);
}
public class frame extends JFrame {
public frame() {
setTitle("Westminster Shopping Center");
setLayout(new BorderLayout());
setSize(1500, 1200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
firstPanel = new JPanel();
firstPanel.setLayout(null);
secondPanel = new JPanel();
}
}
public CustomerGraphicalUserInterface(User user) {
//JFrame frame = new JFrame("Westminster Shopping center ");
// frame.setSize(1500,1200);
//frame.setLayout(new BorderLayout());
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.user = user;
frame frame1 = new frame();
// firstPanel = new JPanel();
//firstPanel.setLayout(null);
JLabel text = new JLabel("Select Product Category");
text.setBounds(500, 45, 200, 100);
firstPanel.add(text);
String productTypeDropDownList[] = {"All", "Electronics", "Clothing"};
productsType = new JComboBox<>(productTypeDropDownList);
productsType.setSelectedIndex(0);
productsType.setBounds(700, 65, 200, 40);
firstPanel.add(productsType);
productsType.addActionListener(cbActionListener);
productsType.addItemListener(cbItemListener);
//JButton shoppingCartBtn = new JButton("Shopping Cart");
// shoppingCartBtn.setBounds(1250, 20, 150, 80);
//shoppingCartBtn.addActionListener(viewCartActionListener);
// firstPanel.add(shoppingCartBtn);
/*shoppingCartBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFrame frame2 = new JFrame();
frame2.setSize(900,700);
frame2.setLayout(new BorderLayout());
frame2.setTitle("Shopping cart");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
}
});*/
secondPanel.setBackground(Color.GRAY);
JButton btn = new JButton("Add to Shopping Cart");
btn.setSize(30, 50);
btn.setBounds(800, 100, 200, 100);
btn.addActionListener(cartActionListener);
secondPanel.add(btn);
frame1.add(firstPanel, BorderLayout.CENTER);
frame1.add(secondPanel, BorderLayout.SOUTH);
frame1.setVisible(true);
updateTable(productsType.getSelectedItem().toString());
}
class Frame2 extends JFrame {
public Frame2() {
setSize(900, 700);
setLayout(new BorderLayout());
setTitle("Shopping Cart");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
}