-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
2566 lines (2105 loc) · 101 KB
/
GUI.java
File metadata and controls
2566 lines (2105 loc) · 101 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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Name: Mya Hussain
// Date: 2019-11-09
// Assignment: Inventory management system
// Description: An inventory management system that stores objects in a file by
// serializing and deserializing an array list of objects. Objects can be added
// edited, sold, searched through and removed from the inventory using the programs
// graphical user interface.
package Store;
// imports mainly for graphics and reading and writing to file
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JSplitPane;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
// start of class
public class GUI {
// attributes, private, only accessible to this class
public JFrame frmInventoryManagementSystem;
public JLayeredPane layeredPane;
// list of all product objects in inventory
public ArrayList<Product> products = deserializeFromFile();
Product ggg = new Product("iiiiiiii", "Grape", "purple, sweet", 30, 3.99, 2.56);
Product nnn = new Product("oooooooo", "Banana", "yellow, long", 10, 1.2, 0.5);
private JTextField TFnameofproduct;
private JTextField TFproductcode;
private JTextField TFquantity;
private JTextField TFretailvalue;
private JTextField TFcosttopurchase;
private JTable table;
private JTextField TFsearchname;
private JTextField TFsearchcode;
private JLabel lblProductcode1;
private JLabel lblpd1;
private JLabel lblProductName1;
private JLabel lblproductquantity1;
private JLabel lblcostprice1;
private JLabel lblretailprice1;
private JLabel lblquantitysold1;
private JTextField TFremovename;
private JTextField TFremovecode;
private JTextField TFpppproductname;
private JTextField TFppppcode;
private JTextField TFppppquantity2;
private JTextField TFppppquantity1;
private JTable table_1;
private JTextField TFSN;
private JTextField TFSC;
private JTextField TFSQ;
private JTextField TFPC;
private JTextField TFSD;
private JTextField TFPR;
private JTextField TFQS;
private JTextField TFQQQQ;
private JTextField TFQQQQSSSS;
private JTextField TFRRRRPPPP;
private JTextField TFCCCCPPPP;
private JTable table_2;
// Create the application.
// constructor
public GUI() {
// set up GUI
initialize();
populateTable();
}
// function to switch panels and create new screen effect
public void switchPanels(JPanel panel) {
// panels are switched by removing and adding them to layered pane
layeredPane.removeAll();
layeredPane.add(panel);
layeredPane.repaint();
layeredPane.revalidate();
}
// function to write object to the file
// takes object to serialize, in this case our product list
public void serializeToFile(ArrayList<Product> listt) {
// try to store it if it does not store catch with error message
try {
// where to store data
File f = new File("database");
// test statements to ensure file directory is working and valid
//System.out.println(f.exists());
//System.out.println(f.isDirectory());
//System.out.println(f.canRead());
//System.out.println(f.canWrite());
FileOutputStream fileout = new FileOutputStream(f);
ObjectOutputStream out = new ObjectOutputStream(fileout);
out.writeObject(listt);
out.close();
System.out.println("object was serialized");
}
catch (IOException e) {
System.out.println("object was not serialized");
}
}
// deserialize object from file to read data
public ArrayList<Product> deserializeFromFile () {
// return the array list
ArrayList<Product> p = null;
try {
// where to get data
File f = new File("database");
// if the file has nothing return empty list (running app for first time)
if(f.length() == 0) {
p = new ArrayList<Product> ();
System.out.println("New list of objects returned");
}
// else return what's in the list
else {
FileInputStream filein = new FileInputStream(f);
ObjectInputStream in = new ObjectInputStream(filein);
// read object of type Product
p = (ArrayList<Product>) in.readObject();
in.close();
filein.close();
System.out.println("object deserialized");
//filein = null;
//in = null;
//f = null;
}
}
// deal with unexpected error
catch(ClassNotFoundException | IOException e){
System.out.println("object not deserialized");
}
return p;
}
// takes array list of integers with all indexes to display
public void displayToSearchContains (ArrayList<Integer> integers) {
DefaultTableModel model = (DefaultTableModel) table_1.getModel();
// empty the table
model.setRowCount(0);
// list of objects to be used to populate table
Object rowData[] = new Object[7];
for (int x = 0; x < integers.size(); x++) {
// fill the table with variables that correspond to the column
rowData[0] = products.get(integers.get(x)).getProductName();
rowData[1] = products.get(integers.get(x)).getProductCode();
rowData[2] = products.get(integers.get(x)).getProductDescription();
rowData[3] = products.get(integers.get(x)).getQuantity();
rowData[4] = products.get(integers.get(x)).getPriceCost();
rowData[5] = products.get(integers.get(x)).getPriceRetail();
rowData[6] = products.get(integers.get(x)).getQuantitySold();
// fill in the row of data then move to next product
model.addRow(rowData);
}
}
public ArrayList<Integer> containsSearchName (String name) {
name.toLowerCase();
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see if all the names in products contains the character
for (int x = 0; x < products.size(); x++ ) {
if ((products.get(x).getProductName().strip().toLowerCase()).contains(name)) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Product name character not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> containsSearchCode (String code) {
code.toLowerCase();
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see if all the codes in products contains the character
for (int x = 0; x < products.size(); x++ ) {
if ((products.get(x).getProductCode().strip().toLowerCase()).contains(code)) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Product code character not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> containsSearchDescription (String desc) {
desc.toLowerCase();
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see if all the descriptions in products contains the character
for (int x = 0; x < products.size(); x++ ) {
if ((products.get(x).getProductDescription().strip().toLowerCase()).contains(desc)) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Description character not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> containsSearchQuantity (String quan) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see if all the quantities in products contains the character
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if ((String.valueOf(products.get(x).getQuantity())).contains(quan)) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"quantity character not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> containsSearchRetailPrice (String retail) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see if all the retail prices in products contains the character
for (int x = 0; x < products.size(); x++ ) {
// see if the retail price has character inputed
// type cast to string to compare characters with .contains()
if ((String.valueOf(products.get(x).getPriceRetail())).contains(retail)) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"retail price character not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> containsSearchCostPrice (String cost) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see if all the cost prices in products contains the character
for (int x = 0; x < products.size(); x++ ) {
// see if the price cost have character inputed
// type cast to string to compare characters with .contains()
if ((String.valueOf(products.get(x).getPriceCost())).contains(cost)) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Cost price character not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> containsSearchQuantitySold (String Qsold) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see if all the quantity sold in products contains the character
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if ((String.valueOf(products.get(x).getQuantitySold())).contains(Qsold)) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Quantity sold character not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
// function to display "less than more than" product list to table 2
// takes array list of integers with all indexes to display
public void displayToLessThanMoreThan (ArrayList<Integer> integers) {
DefaultTableModel model = (DefaultTableModel) table_2.getModel();
// empty the table
model.setRowCount(0);
// list of objects to be used to populate table
Object rowData[] = new Object[7];
for (int x = 0; x < integers.size(); x++) {
// fill the table with variables that correspond to the column
rowData[0] = products.get(integers.get(x)).getProductName();
rowData[1] = products.get(integers.get(x)).getProductCode();
rowData[2] = products.get(integers.get(x)).getProductDescription();
rowData[3] = products.get(integers.get(x)).getQuantity();
rowData[4] = products.get(integers.get(x)).getPriceCost();
rowData[5] = products.get(integers.get(x)).getPriceRetail();
rowData[6] = products.get(integers.get(x)).getQuantitySold();
// fill in the row of data then move to next product
model.addRow(rowData);
}
}
public ArrayList<Integer> lessThanQuantity (int quan) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see what products have a lower quantity
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if (products.get(x).getQuantity() < quan) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"No such products found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> moreThanQuantity (int quan) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see what products have a greater quantity
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if (products.get(x).getQuantity() > quan) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"No such products found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> lessThanQuantitySold (int quanS) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see what products have a lower quantity
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if (products.get(x).getQuantitySold() < quanS) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"No such products found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> moreThanQuantitySold (int quanS) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see what products have a lower quantity
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if (products.get(x).getQuantitySold() > quanS) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"No such products found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> lessThanRetailPrice (int ret) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see what products have a lower quantity
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if (products.get(x).getPriceRetail() < ret) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"No such numbers found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> moreThanPriceRetail (int ret) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see what products have a lower quantity
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if (products.get(x).getPriceRetail() > ret) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"No such numbers found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> lessThanCostPrice (int CP) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see what products have a lower quantity
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if (products.get(x).getPriceCost() < CP) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"No such numbers found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
public ArrayList<Integer> moreThanCostPrice (int CP) {
// list to print to table
ArrayList<Integer> integers = new ArrayList<Integer>();
// see what products have a lower quantity
for (int x = 0; x < products.size(); x++ ) {
// see if the quantities have character inputed
// type cast to string to compare characters with .contains()
if (products.get(x).getPriceCost() > CP) {
integers.add(x);
}
}
// show error if not found
if (integers.size() == 0) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"No such numbers found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
return integers;
}
// this function searches for a product by name and displays it on the GUI by editing labels
public void searchName (String name) {
boolean found = false;
// iterate through all products looking for a matching name
for (int x=0; x< products.size(); x++) {
// if the product names are the same you found the product
if (name.toLowerCase().strip().equals(products.get(x).getProductName().toLowerCase().strip())) {
// display all info for that product
lblProductName1.setText(products.get(x).getProductName());
lblProductcode1.setText(products.get(x).getProductCode());
lblpd1.setText(products.get(x).getProductDescription());
// casting needed to make string in order to display on label
lblproductquantity1.setText((String.valueOf(products.get(x).getQuantity())));
lblretailprice1.setText(String.valueOf(products.get(x).getPriceRetail()));
lblcostprice1.setText(String.valueOf(products.get(x).getPriceCost()));
lblquantitysold1.setText(String.valueOf(products.get(x).getQuantitySold()));
found = true;
}
}
if (!found) {
// if program did not find it alert user
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"name not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
}
// this function searches for a product by code and displays it's info on the GUI by editing labels
public void searchCode(String code) {
boolean found = false;
// iterate through all products looking for a matching code
for (int x=0; x< products.size(); x++) {
// if the code matches display the info
if (code.toLowerCase().strip().equals(products.get(x).getProductCode().toLowerCase().strip())) {
// display all info for that product
lblProductName1.setText(products.get(x).getProductName());
lblProductcode1.setText(products.get(x).getProductCode());
lblpd1.setText(products.get(x).getProductDescription());
// casting needed to make string
lblproductquantity1.setText((String.valueOf(products.get(x).getQuantity())));
lblretailprice1.setText(String.valueOf(products.get(x).getPriceRetail()));
lblcostprice1.setText(String.valueOf(products.get(x).getPriceCost()));
lblquantitysold1.setText(String.valueOf(products.get(x).getQuantitySold()));
found = true;
}
}
if (!found) {
// show error message
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Code not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
}
// this function removes a product from the inventory by its code
public void removeByCode (String code) {
boolean removed = false;
// iterate through all products looking for a matching code
for (int x=0; x< products.size(); x++) {
if (code.toLowerCase().strip().equals(products.get(x).getProductCode().toLowerCase().strip())) {
// if the code is found remove it from the inventory -> "arraylist of products"
products.remove(x);
removed = true;
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Product sucessfully removed",
"Success",JOptionPane.INFORMATION_MESSAGE);
serializeToFile(products);
}
}
if (!removed) {
// show error message
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Code not found, "
+ "product not removed","Inane error",JOptionPane.ERROR_MESSAGE);
}
}
// this function removes a product from inventory by name
public void removeByName(String name) {
boolean removed = false;
// iterate through all products looking for a matching name
for (int x=0; x< products.size(); x++) {
if (name.toLowerCase().equals(products.get(x).getProductName().toLowerCase())) {
// remove product
products.remove(x);
removed = true;
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Product sucessfully removed",
"Success",JOptionPane.INFORMATION_MESSAGE);
System.out.println(products);
serializeToFile(products);
}
}
if (!removed) {
// show error message
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"name not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
}
public void sellByName (String name, int quantity) {
boolean found = false;
boolean validQ = false;
// iterate through all products looking for a matching name
for (int x=0; x< products.size(); x++) {
if (name.toLowerCase().equals(products.get(x).getProductName().toLowerCase())) {
found = true;
// find out how many being sold make sure its not more than you have
int quantityleft = products.get(x).getQuantity() - quantity;
// if you didn't sell more than you have
if (quantityleft<0) {
validQ = false;
}
else {
validQ = true;
// subtract sold units from quantity and update sold unit variable
products.get(x).setQuantity(quantityleft);
products.get(x).addQuantitySold(quantity);
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Product sold",
"Success",JOptionPane.INFORMATION_MESSAGE);
}
}
}
// show error messages depending on what went wrong
if (!found) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Name not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
else if (!validQ) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Quantity too high",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
// update database
serializeToFile(products);
}
// this function sells a product by its code depending on its quantity
public void sellByCode (String code, int quantity) {
boolean found = false;
boolean validQ = false;
// iterate through all products looking for a matching name
for (int x=0; x< products.size(); x++) {
if (code.toLowerCase().equals(products.get(x).getProductCode().toLowerCase())) {
found = true;
// find out how many being sold make sure its not more than you have
int quantityleft = products.get(x).getQuantity() - quantity;
// if you didn't sell more than you have
if (quantityleft<0) {
validQ = false;
}
else {
validQ = true;
// subtract sold units from quantity and update sold unit variable
products.get(x).setQuantity(quantityleft);
products.get(x).addQuantitySold(quantity);
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Product sold",
"Success",JOptionPane.INFORMATION_MESSAGE);
}
}
}
// show error depending on what went wrong
if (!found) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Code not found",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
else if (!validQ) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Quantity too high",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
// update database
serializeToFile(products);
}
// this function populates the Jtable, to be used whenever data changes
public void populateTable() {
// get latest data
products = deserializeFromFile();
DefaultTableModel model= (DefaultTableModel) table.getModel();
// empty the table
model.setRowCount(0);
// list of objects to be used to populate table
Object rowData[] = new Object[7];
// for every product fill in it's column value
for (int i= 0; i<products.size(); i++ ) {
// fill the table with variables that correspond to the column
rowData[0] = products.get(i).getProductName();
rowData[1] = products.get(i).getProductCode();
rowData[2] = products.get(i).getProductDescription();
rowData[3] = products.get(i).getQuantity();
rowData[4] = products.get(i).getPriceCost();
rowData[5] = products.get(i).getPriceRetail();
rowData[6] = products.get(i).getQuantitySold();
// fill in the row of data then move to next product
model.addRow(rowData);
}
}
// allows you to edit values of products directly from inventory
// this function error checks and stores
public void updateFromTable () {
// the number of products will be the same at this stage
// however the value of some products will be different
// this function will error check them and only apply if no error
// temporary variables
String name;
String code;
String desc;
String quan;
String retail;
String cost;
// iterate through each product
for (int x= 0; x< products.size(); x++) {
// iterate through each variable except quantity sold
// quantity sold is changed when a product is sold through the app, not manually
// ERROR CHECK NAME
name = (String) table.getModel().getValueAt(x, 0);
if (name.isBlank()) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"name not stored",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
else {
boolean uniquename = true;
// the product name has to be unique
for(int r =0; r < products.size(); r++) {
if (name.toLowerCase().equals(products.get(r).getProductName().toLowerCase())) {
// if its the same index obviously there's no real "duplicate"
if (r == x) {
}
else {
uniquename = false;
}
}
}
if (uniquename){
// it passed store it
products.get(x).setProductName(name);
}
else {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"name not stored",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
}
// ERROR CHECK CODE
code = (String) table.getModel().getValueAt(x, 1);
if (code.isBlank()) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"code not stored",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
else {
// product code must be 8 characters and only 8 characters
if (code.length() != 8) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"code not stored",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
// check if it's unique
else {
boolean uniquecode = true;
// the product code has to be unique
for(int r =0; r < products.size(); r++) {
if (code.toLowerCase().equals(products.get(r).getProductCode().toLowerCase())) {
// if its the same index obviously there's no real "duplicate"
if (r == x) {
}
else {
uniquecode = false;
}
}
}
if (uniquecode){
// success store it
products.get(x).setProductCode(code);
}
else {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"code not stored",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
}
}
// CHECK DESCRIPTION
desc = (String) table.getModel().getValueAt(x, 2);
if (desc.isBlank()) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"description not stored",
"Inane error",JOptionPane.ERROR_MESSAGE);
}
else {
products.get(x).setProductDescription(desc);
}
// CHECK QUANTITY
quan = (table.getModel().getValueAt(x, 3).toString());
// if blank
if (quan.isBlank()) {
JOptionPane.showMessageDialog(frmInventoryManagementSystem,"Quantity not stored",
"Inane error",JOptionPane.ERROR_MESSAGE);
}