-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHome.java
More file actions
760 lines (659 loc) · 34.1 KB
/
Copy pathHome.java
File metadata and controls
760 lines (659 loc) · 34.1 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
package POSmain;
import DBMySQL.ConnectionProvider;
import Products.Parts;
import Products.Lubricants;
import Products.Accessories;
import Products.Others;
import Products.Tires;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
public class Home extends javax.swing.JPanel {
private POSpage h;
public Home(POSpage h) {
this.h = h;
initComponents();
setEngine();
jTable4.setModel(new DefaultTableModel(
new Object[][]{},
new String[]{"ID", "Name", "Price", "Quantity", "Discount", "Subtotal"}
));
jTable4.setRowHeight(50);
jTable4.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
updateTotalPrice();
}
});
jTextField7.setEditable(true);
jTextField7.setEnabled(true);
jTextField7.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
public void insertUpdate(javax.swing.event.DocumentEvent e) {
calculateChange();
}
public void removeUpdate(javax.swing.event.DocumentEvent e) {
calculateChange();
}
public void changedUpdate(javax.swing.event.DocumentEvent e) {
calculateChange();
}
});
}
public JTable getJTable4() {
return jTable4;
}
private Lubricants en;
public void setEngine() {
if (en == null) {
en = new Lubricants(this);
}
jPanel1.removeAll();
jPanel1.add(en);
SwingUtilities.updateComponentTreeUI(jPanel1);
}
private Parts ch;
public void setChassis() {
if (ch == null) {
ch = new Parts(this);
}
jPanel1.removeAll();
jPanel1.add(ch);
SwingUtilities.updateComponentTreeUI(jPanel1);
jPanel1.revalidate();
}
private Accessories li;
public void setLighting() {
if (li == null) {
li = new Accessories(this);
}
jPanel1.removeAll();
jPanel1.add(li);
SwingUtilities.updateComponentTreeUI(jPanel1);
}
private Others ot;
public void setOthers() {
if (ot == null) {
ot = new Others(this);
}
jPanel1.removeAll();
jPanel1.add(ot);
SwingUtilities.updateComponentTreeUI(jPanel1);
}
private Tires wh;
public void setWheels() {
if (wh == null) {
wh = new Tires(this);
}
jPanel1.removeAll();
jPanel1.add(wh);
SwingUtilities.updateComponentTreeUI(jPanel1);
}
public void addToCartli(int id, String name1, double price, int qty, double discount, double subtotal) {
addToCart(li.getJTable1(), id, name1, price, qty, discount, subtotal, "Lubricants");
}
public void addToCartw(int id, String name1, double price, int qty, double discount, double subtotal) {
addToCart(wh.getJTable1(), id, name1, price, qty, discount, subtotal, "Tires");
}
public void addToCartch(int id, String name1, double price, int qty, double discount, double subtotal) {
addToCart(ch.getJTable1(), id, name1, price, qty, discount, subtotal, "Parts");
}
public void addToCartot(int id, String name1, double price, int qty, double discount, double subtotal) {
addToCart(ot.getJTable1(), id, name1, price, qty, discount, subtotal, "Others");
}
public void addToCarten(int id, String name1, double price, int qty, double discount, double subtotal) {
addToCart(en.getJTable1(), id, name1, price, qty, discount, subtotal, "Lubricants");
}
private void addToCart(JTable sourceTable, int id, String name, double price, int qty, double discount, double subtotal, String category) {
int selectedRow = sourceTable.getSelectedRow();
if (selectedRow != -1) {
int stock = getProductStock(id);
if (stock >= qty) {
DefaultTableModel model = (DefaultTableModel) jTable4.getModel();
boolean found = false;
for (int i = 0; i < model.getRowCount(); i++) {
int existingId = (int) model.getValueAt(i, 0);
if (existingId == id) {
int existingQty = (int) model.getValueAt(i, 3);
int newQty = existingQty + qty;
double newSubtotal = (price - discount) * newQty;
model.setValueAt(newQty, i, 3);
model.setValueAt(newSubtotal, i, 5);
found = true;
break;
}
}
if (!found) {
model.addRow(new Object[]{id, name, price, qty, discount, subtotal, category});
}
updateTotalPrice();
} else {
JOptionPane.showMessageDialog(this, "Insufficient stock! Available stock: " + stock, "Stock Insufficient", JOptionPane.WARNING_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "Please select a product first!", "Selection Required", JOptionPane.WARNING_MESSAGE);
}
}
private int getProductStock(int productId) {
int stock = 0;
try (Connection conn = ConnectionProvider.getCon()) {
String sql = "SELECT Stocks FROM product WHERE ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, productId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
stock = rs.getInt("Stocks");
}
}
}
} catch (SQLException e) {
System.err.println("Error fetching stock: " + e.getMessage());
JOptionPane.showMessageDialog(this, "Error fetching stock: " + e.getMessage());
}
return stock;
}
private void updateTotalPrice() {
double total = 0.0;
for (int i = 0; i < jTable4.getRowCount(); i++) {
try {
double subtotal = Double.parseDouble(jTable4.getValueAt(i, 5).toString());
total += subtotal;
} catch (Exception e) {
System.out.println("Error parsing subtotal: " + e.getMessage());
}
}
jTextField6.setText(String.format("%.2f", total));
}
private void calculateChange() {
try {
double total = Double.parseDouble(jTextField6.getText());
double paid = Double.parseDouble(jTextField7.getText());
if (paid < total) {
jTextField8.setText("Insufficient");
} else {
double change = paid - total;
jTextField8.setText(String.format("%.2f", change));
}
} catch (NumberFormatException e) {
jTextField8.setText("");
}
}
private void updateStock(int productId, int quantity) {
try (Connection conn = ConnectionProvider.getCon()) {
String sql = "UPDATE product SET Stocks = Stocks - ? WHERE ID = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, quantity);
pstmt.setInt(2, productId);
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected <= 0) {
JOptionPane.showMessageDialog(this, "Stock update failed. Product ID " + productId + " may not exist.");
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Database error while updating stock: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
private double getProductCost(int productId) {
double cost = 0.0;
try (Connection conn = ConnectionProvider.getCon()) {
String sql = "SELECT cost FROM product WHERE ID = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, productId);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
cost = rs.getDouble("cost");
}
}
}
} catch (SQLException e) {
System.err.println("Error getting product cost: " + e.getMessage());
JOptionPane.showMessageDialog(this, "Database error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
return cost;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel10 = new javax.swing.JPanel();
jPanel9 = new javax.swing.JPanel();
jButton30 = new javax.swing.JButton();
jButton31 = new javax.swing.JButton();
jButton32 = new javax.swing.JButton();
jButton33 = new javax.swing.JButton();
jButton34 = new javax.swing.JButton();
jPanel15 = new javax.swing.JPanel();
jLabel14 = new javax.swing.JLabel();
jScrollPane4 = new javax.swing.JScrollPane();
jTable4 = new javax.swing.JTable();
jLabel15 = new javax.swing.JLabel();
jTextField6 = new javax.swing.JTextField();
jButton20 = new javax.swing.JButton();
jButton21 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
jTextField7 = new javax.swing.JTextField();
jLabel16 = new javax.swing.JLabel();
jTextField8 = new javax.swing.JTextField();
jLabel17 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
setBackground(new java.awt.Color(255, 255, 255));
setPreferredSize(new java.awt.Dimension(1290, 720));
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel10.setBackground(new java.awt.Color(204, 204, 255));
jButton30.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton30.setText("Lubricants");
jButton30.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton30jButton8ActionPerformed(evt);
}
});
jButton31.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton31.setText("Spare Parts");
jButton31.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton31jButton9ActionPerformed(evt);
}
});
jButton32.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton32.setText("Tires");
jButton32.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton32jButton10ActionPerformed(evt);
}
});
jButton33.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton33.setText("Accessories");
jButton33.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton33jButton23ActionPerformed(evt);
}
});
jButton34.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton34.setText("Others");
jButton34.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton34jButton24ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
jPanel9.setLayout(jPanel9Layout);
jPanel9Layout.setHorizontalGroup(
jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel9Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jButton30, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(60, 60, 60)
.addComponent(jButton31, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(60, 60, 60)
.addComponent(jButton32, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(60, 60, 60)
.addComponent(jButton33, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
.addComponent(jButton34, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31))
);
jPanel9Layout.setVerticalGroup(
jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel9Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton30)
.addComponent(jButton31)
.addComponent(jButton32)
.addComponent(jButton33)
.addComponent(jButton34, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(11, 11, 11))
);
javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
jPanel10.setLayout(jPanel10Layout);
jPanel10Layout.setHorizontalGroup(
jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 810, Short.MAX_VALUE)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel10Layout.createSequentialGroup()
.addGap(0, 4, Short.MAX_VALUE)
.addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 4, Short.MAX_VALUE)))
);
jPanel10Layout.setVerticalGroup(
jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 70, Short.MAX_VALUE)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel10Layout.createSequentialGroup()
.addGap(0, 15, Short.MAX_VALUE)
.addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 15, Short.MAX_VALUE)))
);
add(jPanel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 810, 70));
jPanel15.setForeground(new java.awt.Color(0, 0, 0));
jLabel14.setFont(new java.awt.Font("Segoe UI Semibold", 0, 24)); // NOI18N
jLabel14.setForeground(new java.awt.Color(255, 255, 255));
jLabel14.setText("Current Transaction");
jTable4.setBackground(new java.awt.Color(255, 255, 255));
jTable4.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jTable4.setForeground(new java.awt.Color(0, 0, 0));
jTable4.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null}
},
new String [] {
"ID", "Name", "Unit Price", "Quantity", "Discount", "Sub Price"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.String.class, java.lang.Double.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable4.setShowGrid(true);
jTable4.setShowVerticalLines(false);
jScrollPane4.setViewportView(jTable4);
jLabel15.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
jLabel15.setForeground(new java.awt.Color(255, 255, 255));
jLabel15.setText("Total Amount:");
jTextField6.setBackground(new java.awt.Color(255, 255, 255));
jTextField6.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jTextField6.setForeground(new java.awt.Color(0, 0, 0));
jTextField6.setEnabled(false);
jTextField6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField6ActionPerformed(evt);
}
});
jButton20.setBackground(new java.awt.Color(204, 102, 0));
jButton20.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
jButton20.setForeground(new java.awt.Color(255, 255, 255));
jButton20.setText("Print Bill");
jButton20.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton20ActionPerformed(evt);
}
});
jButton21.setBackground(new java.awt.Color(204, 102, 0));
jButton21.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
jButton21.setForeground(new java.awt.Color(255, 255, 255));
jButton21.setText("Clear");
jButton21.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton21ActionPerformed(evt);
}
});
jButton22.setBackground(new java.awt.Color(204, 102, 0));
jButton22.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
jButton22.setForeground(new java.awt.Color(255, 255, 255));
jButton22.setText("Remove");
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton22ActionPerformed(evt);
}
});
jTextField7.setBackground(new java.awt.Color(255, 255, 255));
jTextField7.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jTextField7.setForeground(new java.awt.Color(0, 0, 0));
jTextField7.setEnabled(false);
jTextField7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField7ActionPerformed(evt);
}
});
jLabel16.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
jLabel16.setForeground(new java.awt.Color(255, 255, 255));
jLabel16.setText("Cash:");
jTextField8.setBackground(new java.awt.Color(255, 255, 255));
jTextField8.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jTextField8.setForeground(new java.awt.Color(0, 0, 0));
jTextField8.setEnabled(false);
jTextField8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField8ActionPerformed(evt);
}
});
jLabel17.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
jLabel17.setForeground(new java.awt.Color(255, 255, 255));
jLabel17.setText("Change:");
javax.swing.GroupLayout jPanel15Layout = new javax.swing.GroupLayout(jPanel15);
jPanel15.setLayout(jPanel15Layout);
jPanel15Layout.setHorizontalGroup(
jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel15Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel14, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 423, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel15Layout.createSequentialGroup()
.addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel15Layout.createSequentialGroup()
.addComponent(jLabel15)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton20, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel15Layout.createSequentialGroup()
.addComponent(jButton22)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton21)))
.addGap(30, 30, 30)
.addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel15Layout.createSequentialGroup()
.addComponent(jLabel17)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel15Layout.createSequentialGroup()
.addComponent(jLabel16)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGap(0, 26, Short.MAX_VALUE))
);
jPanel15Layout.setVerticalGroup(
jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel15Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel15)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel16)
.addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel15Layout.createSequentialGroup()
.addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel15Layout.createSequentialGroup()
.addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel15Layout.createSequentialGroup()
.addComponent(jButton20)
.addGap(18, 18, 18)))
.addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton22)
.addComponent(jButton21)))
.addComponent(jLabel17))
.addContainerGap(71, Short.MAX_VALUE))
);
add(jPanel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(810, 0, 480, 720));
jScrollPane1.setBackground(new java.awt.Color(204, 204, 255));
jScrollPane1.setBorder(null);
jPanel1.setBackground(new java.awt.Color(204, 204, 255));
jScrollPane1.setViewportView(jPanel1);
add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 70, 810, 650));
}// </editor-fold>//GEN-END:initComponents
private void jButton30jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton30jButton8ActionPerformed
setEngine();
}//GEN-LAST:event_jButton30jButton8ActionPerformed
private void jButton31jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton31jButton9ActionPerformed
setChassis();
}//GEN-LAST:event_jButton31jButton9ActionPerformed
private void jButton32jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton32jButton10ActionPerformed
setWheels();
}//GEN-LAST:event_jButton32jButton10ActionPerformed
private void jButton33jButton23ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton33jButton23ActionPerformed
setLighting();
}//GEN-LAST:event_jButton33jButton23ActionPerformed
private void jButton34jButton24ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton34jButton24ActionPerformed
setOthers();
}//GEN-LAST:event_jButton34jButton24ActionPerformed
private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton22ActionPerformed
int selectedRow = jTable4.getSelectedRow();
if (selectedRow != -1) {
DefaultTableModel model = (DefaultTableModel) jTable4.getModel();
model.removeRow(selectedRow);
updateTotalPrice();
if (model.getRowCount() == 0) {
jTextField7.setText("");
jTextField8.setText("");
}
} else {
JOptionPane.showMessageDialog(this, "Please select a row to remove.");
}
}//GEN-LAST:event_jButton22ActionPerformed
private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton20ActionPerformed
if (jTable4.getRowCount() == 0) {
JOptionPane.showMessageDialog(this, "No items in cart");
return;
}
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet generatedKeys = null;
try {
conn = ConnectionProvider.getCon();
String insertOrderSQL = "INSERT INTO orders (Date, TotalAmount, TotalCost, TotalProfit) VALUES (?, ?, ?, ?)";
pstmt = conn.prepareStatement(insertOrderSQL, PreparedStatement.RETURN_GENERATED_KEYS); // Get generated OrderID
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
pstmt.setDate(1, sqlDate);
double totalAmount = Double.parseDouble(jTextField6.getText());
pstmt.setDouble(2, totalAmount);
double totalCost = 0.0;
double totalProfit = 0.0;
DefaultTableModel cartModel = (DefaultTableModel) jTable4.getModel();
for (int i = 0; i < cartModel.getRowCount(); i++) {
int productId = (int) cartModel.getValueAt(i, 0);
int qty = (int) cartModel.getValueAt(i, 3);
double price = (double) cartModel.getValueAt(i, 2);
double subtotal = (double) cartModel.getValueAt(i, 5);
double cost = getProductCost(productId);
totalCost += cost * qty;
totalProfit += (price - cost) * qty;
updateStock(productId, qty);
}
pstmt.setDouble(3, totalCost);
pstmt.setDouble(4, totalProfit);
pstmt.executeUpdate();
int orderId = -1;
generatedKeys = pstmt.getGeneratedKeys();
if (generatedKeys.next()) {
orderId = generatedKeys.getInt(1);
} else {
throw new SQLException("Failed to retrieve OrderID.");
}
Receipt receiptPanel = new Receipt();
StringBuilder billText = new StringBuilder(" Official Receipt\n");
billText.append("Order ID: ").append(orderId).append("\n");
billText.append("Date: ").append(sqlDate).append("\n");
billText.append("=======================================\n");
billText.append("Product Name\tQty\tPrice\tSubtotal\n");
billText.append("=======================================\n");
double subtotalWithVAT = 0.0;
double totalDiscount = 0.0;
for (int i = 0; i < cartModel.getRowCount(); i++) {
String name = (String) cartModel.getValueAt(i, 1);
double price = (double) cartModel.getValueAt(i, 2);
int qty = (int) cartModel.getValueAt(i, 3);
double discount = (double) cartModel.getValueAt(i, 4);
double subtotal = (double) cartModel.getValueAt(i, 5);
totalDiscount += discount * qty;
subtotalWithVAT += price * qty;
billText.append(String.format("%-15s\t%d\t%.2f\t%.2f\n", name, qty, price, subtotal));
}
double taxRate = 0.12;
double subtotalWithoutVAT = subtotalWithVAT / (1 + taxRate);
double taxAmount = subtotalWithVAT - subtotalWithoutVAT;
double finalTotal = subtotalWithVAT;
billText.append("=======================================\n");
billText.append(String.format("Total Discount: \t\t%.2f\n", totalDiscount));
billText.append(String.format("Subtotal (Without VAT): \t%.2f\n", subtotalWithoutVAT));
billText.append(String.format("VAT (%d%%): \t\t\t%.2f\n", (int) (taxRate * 100), taxAmount));
billText.append(String.format("Total Amount: \t\t\t%.2f\n", finalTotal));
billText.append(String.format("Cash Paid: \t\t\t%.2f\n", Double.parseDouble(jTextField7.getText())));
billText.append(String.format("Change: \t\t\t%.2f\n", Double.parseDouble(jTextField8.getText())));
billText.append("=======================================\n");
billText.append(" Thank you for your purchase!\n");
receiptPanel.setBillText(billText.toString());
javax.swing.JFrame receiptFrame = new javax.swing.JFrame("Receipt");
receiptFrame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
receiptFrame.getContentPane().add(receiptPanel);
receiptFrame.pack();
receiptFrame.setLocationRelativeTo(null);
receiptFrame.setVisible(true);
cartModel.setRowCount(0);
jTextField7.setText("");
jTextField8.setText("");
} catch (SQLException e) {
System.err.println("Error: " + e.getMessage());
JOptionPane.showMessageDialog(this, "Database error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} finally {
try {
if (generatedKeys != null) generatedKeys.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
System.err.println("Error closing resources: " + e.getMessage());
}
}
}//GEN-LAST:event_jButton20ActionPerformed
private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton21ActionPerformed
DefaultTableModel model = (DefaultTableModel) jTable4.getModel();
model.setRowCount(0);
updateTotalPrice();
jTextField7.setText("");
jTextField8.setText("");
}//GEN-LAST:event_jButton21ActionPerformed
private void jTextField7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField7ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField7ActionPerformed
private void jTextField8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField8ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField8ActionPerformed
private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField6ActionPerformed
}//GEN-LAST:event_jTextField6ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton30;
private javax.swing.JButton jButton31;
private javax.swing.JButton jButton32;
private javax.swing.JButton jButton33;
private javax.swing.JButton jButton34;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel10;
private javax.swing.JPanel jPanel15;
private javax.swing.JPanel jPanel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTable jTable4;
private javax.swing.JTextField jTextField6;
private javax.swing.JTextField jTextField7;
private javax.swing.JTextField jTextField8;
// End of variables declaration//GEN-END:variables
}