-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.java
1070 lines (866 loc) · 38.3 KB
/
frontend.java
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
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.awt.*;
@SuppressWarnings("unchecked")
/*
* @author Jordan Noel
* Frontend of CRM application
*
* Run With:
* java -classpath ".:/Library/Java/Extensions/sqlite-jdbc-3.27.2.1.jar" frontend
*/
public class frontend{
public static JFrame frame; //the main frame
public static JFrame secondFrame; //the frame for adding clients/sales
public static JFrame editingFrame; //the frame for editing clients/sales
public static JPanel clientPanel; //the panel with the client table and view/remove buttons
public static JPanel salesPanel; //the panel with the sales table and view/remove buttons
public static DefaultTableModel clientModel; //the model for the client table
public static DefaultTableModel salesModel; //the model for the sales table
public static void main(String[] args){
//create the db
backend.createDB();
//create the frame window
frame = new JFrame("CRM");
mainWindow();
}
/**
* Creates the main window that you view on opening the program.
* Contains a table of on current clients and sales with options to view them or remove them.
*/
public static void mainWindow(){
//make sure the frame is empty then repaint it and set the layout
frame.getContentPane().removeAll();
frame.getContentPane().repaint();
//create the tabbed pane
JTabbedPane jtp = new JTabbedPane();
frame.getContentPane().add(jtp);
//create the client tab
clientPanel = new JPanel();
constructClientTab();
//create the sales tab
salesPanel = new JPanel();
constructSaleTab();
//add the tabs to the window
jtp.addTab("Client Info",clientPanel);
jtp.addTab("Sales Info", salesPanel);
//Create MenuBar
JMenuBar bar = new JMenuBar();
JMenu clients = new JMenu("Clients");
JMenu sales = new JMenu("Sales");
JMenu reports = new JMenu("Reports");
bar.add(clients);
bar.add(sales);
bar.add(reports);
frame.setJMenuBar(bar);
//create ActionListener for add client
JMenuItem addClient = new JMenuItem("Add");
addClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
addingClient();
}
});
//Include add clients in clients subMenu
clients.add(addClient);
//create menu item to add a sale and create a action listener for it
JMenuItem addSale = new JMenuItem("Add");
addSale.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
addingSale();
}
});
//Include add sale in the sales subMenu
sales.add(addSale);
//Create menu item to run a report on all the clients and create an action listener for it
JMenuItem allClients = new JMenuItem("All Clients");
allClients.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//confirm the user wants to save the file
if(JOptionPane.showConfirmDialog(frame,"The file will be saved under the same directory as this program. Would you like to proceed?","Continue?",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
//open the backend and run the client report
backend back = new backend();
back.clientReport();
}
}
});
//Include all clients in the reports subMenu
reports.add(allClients);
//Create menu item to run a report on all the sales and create an action listener for it
JMenuItem allSales = new JMenuItem("All Sales");
allSales.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//confirm the user wants to save the file
if(JOptionPane.showConfirmDialog(frame,"The file will be saved under the same directory as this program. Would you like to proceed?","Continue?",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
//open the backend and run the sales report
backend back = new backend();
back.salesReport();
}
}
});
//Include all sales in the reports subMenu
reports.add(allSales);
//occupy JTables with info from backend
occupyTables();
//Set the frame parameters
frame.setSize(500,600);
frame.setResizable(false);
frame.setVisible(true);
}
/**
* Add all the information to the client panel
*/
public static void constructClientTab(){
//clear the client panel first
clientPanel.removeAll();
clientPanel.repaint();
//set the layout for the client panel
clientPanel.setLayout(new FlowLayout());
//Add label for JTable
JLabel clist = new JLabel("Client List");
clientPanel.add(clist);
//Create JTable from default model
clientModel = new DefaultTableModel();
JTable clientList = new JTable(clientModel);
//Add columns to the model
clientModel.addColumn("Name");
clientModel.addColumn("SIN");
//add the scroller to the pane
JScrollPane listscroller = new JScrollPane();
listscroller.setViewportView(clientList);
//add the JTable to the frame
clientPanel.add(listscroller);
//Create the view button
JButton viewClient = new JButton("View Info");
//create the remove action listener
viewClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//get selected row
Vector<String> info = clientModel.getDataVector().get(clientList.getSelectedRow());
//open the backend and retrieve the clients info using the sin
backend back = new backend();
ArrayList<String> individInfo = back.retrieveClientInfoBySIN((String)info.get(1));
//call view client to display information
viewClient(individInfo);
}
});
//Create the remove button
JButton removeClient = new JButton("Remove");
//create the remove action Listener
removeClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//get selected row
Vector<String> info = clientModel.getDataVector().get(clientList.getSelectedRow());
//open the backend and delete the client
backend back = new backend();
back.deleteClient((String)info.get(1));
//reload the window
mainWindow();
}
});
//Add view and remove button
clientPanel.add(viewClient);
clientPanel.add(removeClient);
}
/**
* Add all the information to the sales panel
*/
public static void constructSaleTab(){
//clear the sales panel first
salesPanel.removeAll();
salesPanel.repaint();
//set the layout of the sales panel
salesPanel.setLayout(new FlowLayout());
//Add the label for JTable
JLabel slist = new JLabel("Sales List");
salesPanel.add(slist);
//Create the JTable from default model
salesModel = new DefaultTableModel();
JTable salesList = new JTable(salesModel);
//Add columns to the model
salesModel.addColumn("Client");
salesModel.addColumn("Amount");
salesModel.addColumn("Type");
salesModel.addColumn("Transaction ID");
//add the scroller to the pane
JScrollPane salesScroller = new JScrollPane();
salesScroller.setViewportView(salesList);
salesPanel.add(salesScroller);
//Create view button
JButton viewSale = new JButton("View Info");
//create an action listener for the view button
viewSale.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//get selected row
Vector<String> info = salesModel.getDataVector().get(salesList.getSelectedRow());
//open the backend and retrieve the sale info using the transaction id
backend back = new backend();
ArrayList<String> saleInfo = back.retrieveSaleInfoByID((String)info.get(3));
//call viewing sale to display information
viewingSale(saleInfo);
}
});
//Create the remove button
JButton removeSale = new JButton("Remove");
//create the remove action Listener
removeSale.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//get selected row
Vector<String> info = salesModel.getDataVector().get(salesList.getSelectedRow());
//open the backend and delete the sale
backend back = new backend();
back.deleteSale((String)info.get(3));
//reload the window
mainWindow();
}
});
//add the buttons to the sales panel
salesPanel.add(viewSale);
salesPanel.add(removeSale);
}
/**
* Creates the small pop-up frame used to add the client to the db.
* Contains fields for all the parameters, sin, last name and first name are required.
*/
public static void addingClient(){
//Create the adding client frame
secondFrame = new JFrame("Add Client");
secondFrame.getContentPane().setLayout(new GridLayout(0,2));
//first name
JLabel fname = new JLabel("First Name:");
JTextField firstName = new JTextField();
firstName.setColumns(15);
//last name
JLabel lname = new JLabel("Last Name:");
JTextField lastName = new JTextField();
lastName.setColumns(15);
//D.O.B
JLabel date = new JLabel("D.O.B (DD/MM/YYYY):");
DateFormat form = new SimpleDateFormat("dd/MM/yyyy");
JFormattedTextField dob = new JFormattedTextField(form);
dob.setColumns(15);
//Town
JLabel townLabel = new JLabel("Town:");
JTextField town = new JTextField();
town.setColumns(15);
//Province
JLabel provLabel = new JLabel("Province:");
JTextField province = new JTextField();
province.setColumns(15);
//Postal Code
JLabel postLabel = new JLabel("Postal Code:");
JTextField postal = new JTextField();
postal.setColumns(15);
//Home Phone
JLabel homeLabel = new JLabel("Home Phone:");
JTextField homePhone = new JTextField();
homePhone.setColumns(15);
//Mobile Phone
JLabel mobileLabel = new JLabel("Mobile Phone:");
JTextField mobilePhone = new JTextField();
mobilePhone.setColumns(15);
//Sin
JLabel sinLabel = new JLabel("SIN:");
JTextField sin = new JTextField();
sin.setColumns(15);
//create the add button
JButton add = new JButton("Add");
//place an ActionListener on the add button
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//make sure required fields are filled
if (firstName.getText().isEmpty() == false && lastName.getText().isEmpty() == false && sin.getText().isEmpty() == false){
//open the backend
backend back = new backend();
//check to make sure the given sin is not already in the db
if(back.checkSin(sin.getText())) {
//add the client to the db using the info in the textfields
back.addClientToDB(sin.getText(),
firstName.getText(),
lastName.getText(),
dob.getText(),
town.getText(),
province.getText(),
postal.getText(),
homePhone.getText(),
mobilePhone.getText());
//close the add client frame
secondFrame.dispose();
//reload the main frame
occupyTables();
}
else {
JOptionPane.showMessageDialog(null, "The SIN you entered already exists in the database.", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null, "Please fill in the required fields. (SIN, First Name, Last Name)", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//clear button with ActionListener, clears the textfields
JButton clear = new JButton("Clear");
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sin.setText("");
firstName.setText("");
lastName.setText("");
dob.setText("");
town.setText("");
province.setText("");
postal.setText("");
homePhone.setText("");
mobilePhone.setText("");
}
});
//add all secondFrame components
secondFrame.getContentPane().add(fname);
secondFrame.getContentPane().add(firstName);
secondFrame.getContentPane().add(lname);
secondFrame.getContentPane().add(lastName);
secondFrame.getContentPane().add(date);
secondFrame.getContentPane().add(dob);
secondFrame.getContentPane().add(townLabel);
secondFrame.getContentPane().add(town);
secondFrame.getContentPane().add(provLabel);
secondFrame.getContentPane().add(province);
secondFrame.getContentPane().add(postLabel);
secondFrame.getContentPane().add(postal);
secondFrame.getContentPane().add(homeLabel);
secondFrame.getContentPane().add(homePhone);
secondFrame.getContentPane().add(mobileLabel);
secondFrame.getContentPane().add(mobilePhone);
secondFrame.getContentPane().add(sinLabel);
secondFrame.getContentPane().add(sin);
secondFrame.getContentPane().add(add);
secondFrame.getContentPane().add(clear);
//Set size and stop resize
secondFrame.setSize(300,300);
secondFrame.setResizable(false);
secondFrame.setVisible(true);
}
/**
* Method that is used for adding a sale. Creates a small window for adding the sale.
* Amount is required and a client must be selected from the table to link to the sale.
*/
public static void addingSale(){
//Create the adding sale frame
secondFrame = new JFrame("Add Sale");
secondFrame.getContentPane().setLayout(new GridLayout(0,2));
//amount
JLabel amountLabel = new JLabel("Amount ($):");
JTextField amountField = new JTextField();
amountField.setColumns(15);
//date
JLabel dateLabel = new JLabel("Date (DD/MM/YYYY):");
DateFormat form = new SimpleDateFormat("dd/MM/yyyy");
JFormattedTextField dateField = new JFormattedTextField(form);
dateField.setColumns(15);
//type
JLabel typeLabel = new JLabel("Type:");
JTextField typeField = new JTextField();
typeField.setColumns(15);
//select client
JLabel clientLabel = new JLabel("Select a Client:");
//Create JTable from default model
DefaultTableModel cmodel = new DefaultTableModel();
JTable clientList = new JTable(clientModel);
//Add columns to the model
cmodel.addColumn("Name");
cmodel.addColumn("SIN");
//add the scroller to the pane
JScrollPane listscroller = new JScrollPane();
listscroller.setViewportView(clientList);
//open the backend and retrieve the client information
backend back = new backend();
ArrayList<ArrayList<String>> clientInfo = back.retrieveClients();
//add the info retrieved to the client table
for (int i=0; i<clientInfo.size(); i++){
cmodel.addRow(new Object[]{clientInfo.get(i).get(1), clientInfo.get(i).get(0)});
}
//create the add button
JButton add = new JButton("Add");
//place an ActionListener on the add button
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//make sure required fields are filled
if (amountField.getText().isEmpty() == false && clientList.getSelectionModel().isSelectionEmpty() == false){
//collect info from selected client
Vector<String> info = cmodel.getDataVector().get(clientList.getSelectedRow());
//open the backend and add the sale to the db
backend back = new backend();
back.addSaleToDB(amountField.getText(),
(String)info.get(1),
dateField.getText(),
typeField.getText());
//close the adding sale frame
secondFrame.dispose();
//occupy the jtables again
occupyTables();
}
else{
JOptionPane.showMessageDialog(null, "Please fill in the required fields (Amount). And select a client from the table.", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//create a clear button
JButton clear = new JButton("Clear");
//add an actionlistener to clear, clears the textfields
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
amountField.setText("");
dateField.setText("");
typeField.setText("");
clientList.getSelectionModel().clearSelection();
}
});
//add all the content to the frame
secondFrame.getContentPane().add(amountLabel);
secondFrame.getContentPane().add(amountField);
secondFrame.getContentPane().add(dateLabel);
secondFrame.getContentPane().add(dateField);
secondFrame.getContentPane().add(typeLabel);
secondFrame.getContentPane().add(typeField);
secondFrame.getContentPane().add(clientLabel);
secondFrame.getContentPane().add(listscroller);
secondFrame.getContentPane().add(add);
secondFrame.getContentPane().add(clear);
//Set size and stop resize
secondFrame.setSize(400,400);
secondFrame.setResizable(false);
secondFrame.setVisible(true);
}
/**
* Method that removes all information from the table created in mainWindow().
*/
public static void clearTables(){
//remove the rows of the client table
while(clientModel.getRowCount() > 0){
clientModel.removeRow(0);
}
//remove the rows of the sales table
while(salesModel.getRowCount() > 0){
salesModel.removeRow(0);
}
}
/**
* Method the occupies the tables with information about all the clients and sales.
*/
public static void occupyTables(){
//Clear the table first
clearTables();
//Open up the backend and retrieve the client and sales info
backend back = new backend();
ArrayList<ArrayList<String>> clientInfo = back.retrieveClients();
ArrayList<ArrayList<String>> saleInfo = back.retrieveSales();
//add the client info retrieved to the table
for (int i=0; i<clientInfo.size(); i++){
clientModel.addRow(new Object[]{clientInfo.get(i).get(1), clientInfo.get(i).get(0)});
}
//add the sale info retrieved to the table
for (int i=0; i<saleInfo.size(); i++){
salesModel.addRow(new Object[]{saleInfo.get(i).get(2), "$" + saleInfo.get(i).get(0), saleInfo.get(i).get(1), saleInfo.get(i).get(3)});
}
}
/**
* Creates the client viewing frame, inside the client panel.
* Clears the panel then inserts all the info for a client, as well as a return and edit button.
*
* @param lst - a list of all information on an individual client.
*/
public static void viewClient(ArrayList<String> lst){
//clear the panel
clientPanel.removeAll();
clientPanel.repaint();
clientPanel.setLayout(new GridLayout(0,2));
//last name
JLabel lastNameLabel = new JLabel("Last Name: ");
lastNameLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel lastNameInfo = new JLabel(lst.get(0));
lastNameInfo.setFont(new Font("", Font.PLAIN, 18));
//first name
JLabel firstNameLabel = new JLabel("First Name: ");
firstNameLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel firstNameInfo = new JLabel(lst.get(1));
firstNameInfo.setFont(new Font("", Font.PLAIN, 18));
//SIN
JLabel sinLabel = new JLabel("SIN: ");
sinLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel sinInfo = new JLabel(lst.get(2));
sinInfo.setFont(new Font("", Font.PLAIN, 18));
//dob
JLabel dobLabel = new JLabel("Date of Birth: ");
dobLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel dobInfo = new JLabel(lst.get(3));
dobInfo.setFont(new Font("", Font.PLAIN, 18));
//town
JLabel townLabel = new JLabel("Town: ");
townLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel townInfo = new JLabel(lst.get(4));
townInfo.setFont(new Font("", Font.PLAIN, 18));
//province
JLabel provinceLabel = new JLabel("Province: ");
provinceLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel provinceInfo = new JLabel(lst.get(5));
provinceInfo.setFont(new Font("", Font.PLAIN, 18));
//postal code
JLabel postalLabel = new JLabel("Postal Code: ");
postalLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel postalInfo = new JLabel(lst.get(6));
postalInfo.setFont(new Font("", Font.PLAIN, 18));
//home phone
JLabel homeLabel = new JLabel("Home Phone: ");
homeLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel homeInfo = new JLabel(lst.get(7));
homeInfo.setFont(new Font("", Font.PLAIN, 18));
//mobile phone
JLabel mobileLabel = new JLabel("Mobile Phone: ");
mobileLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel mobileInfo = new JLabel(lst.get(8));
mobileInfo.setFont(new Font("", Font.PLAIN, 18));
//edit button
JButton edit = new JButton("Edit");
//add an actionlistener to edit, that opens editing window
edit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
editingClient(lst.get(2));
}
});
//return button
JButton returnToPrevious = new JButton("< Return");
//add an actionlistener to returnToPrevious that reloads the client tab and occupies the table
returnToPrevious.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
constructClientTab();
occupyTables();
}
});
//Insert all the labels/Info and buttons
clientPanel.add(lastNameLabel);
clientPanel.add(lastNameInfo);
clientPanel.add(firstNameLabel);
clientPanel.add(firstNameInfo);
clientPanel.add(sinLabel);
clientPanel.add(sinInfo);
clientPanel.add(dobLabel);
clientPanel.add(dobInfo);
clientPanel.add(townLabel);
clientPanel.add(townInfo);
clientPanel.add(provinceLabel);
clientPanel.add(provinceInfo);
clientPanel.add(postalLabel);
clientPanel.add(postalInfo);
clientPanel.add(homeLabel);
clientPanel.add(homeInfo);
clientPanel.add(mobileLabel);
clientPanel.add(mobileInfo);
clientPanel.add(returnToPrevious);
clientPanel.add(edit);
frame.setVisible(true);
}
/**
* Creates the sales viewing frame, inside the sales panel.
* Clears the panel then inserts all the info for a sale, as well as a return and edit button.
*
* @param lst - a list of all information on a sale.
*/
public static void viewingSale(ArrayList<String> lst){
//clear the panel
salesPanel.removeAll();
salesPanel.repaint();
salesPanel.setLayout(new GridLayout(0,2));
//transaction id
JLabel idLabel = new JLabel("Transaction ID: ");
idLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel idInfo = new JLabel(lst.get(0));
idInfo.setFont(new Font("", Font.PLAIN, 18));
//amount
JLabel amountLabel = new JLabel("Amount: ");
amountLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel amountInfo = new JLabel(lst.get(1));
amountInfo.setFont(new Font("", Font.PLAIN, 18));
//client name
JLabel nameLabel = new JLabel("Client Name: ");
nameLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel nameInfo = new JLabel(lst.get(2));
nameInfo.setFont(new Font("", Font.PLAIN, 18));
//client sin
JLabel sinLabel = new JLabel("Client SIN: ");
sinLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel sinInfo = new JLabel(lst.get(3));
sinInfo.setFont(new Font("", Font.PLAIN, 18));
//date
JLabel dateLabel = new JLabel("Date: ");
dateLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel dateInfo = new JLabel(lst.get(4));
dateInfo.setFont(new Font("", Font.PLAIN, 18));
//type
JLabel typeLabel = new JLabel("Type of Sale: ");
typeLabel.setFont(new Font("", Font.PLAIN, 18));
JLabel typeInfo = new JLabel(lst.get(5));
typeInfo.setFont(new Font("", Font.PLAIN, 18));
//edit button
JButton edit = new JButton("Edit");
//add an actionlistener to edit the sale
edit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
editingSale(lst.get(0));
}
});
//return button
JButton returnToPrevious = new JButton("< Return");
//add an actionlistener to returnToPrevious that reloads the sales tab and occupies the tables
returnToPrevious.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
constructSaleTab();
occupyTables();
}
});
//Insert all the labels/Info and buttons
salesPanel.add(idLabel);
salesPanel.add(idInfo);
salesPanel.add(amountLabel);
salesPanel.add(amountInfo);
salesPanel.add(nameLabel);
salesPanel.add(nameInfo);
salesPanel.add(sinLabel);
salesPanel.add(sinInfo);
salesPanel.add(dateLabel);
salesPanel.add(dateInfo);
salesPanel.add(typeLabel);
salesPanel.add(typeInfo);
salesPanel.add(returnToPrevious);
salesPanel.add(edit);
frame.setVisible(true);
}
/**
* Creates a frame for editing a client using the same JFrame using when adding a client/sale.
* Occupies the text fields using info retrieved from the backend about the individual client.
*
* @param sin - the sin for the client to be edited, used to retrieve all the rest of the info.
*/
public static void editingClient(String sin){
//Create the editing frame
secondFrame = new JFrame("Edit Client");
secondFrame.getContentPane().setLayout(new GridLayout(0,2));
//Retrieve info
backend back = new backend();
ArrayList<String> lst = back.retrieveClientInfoBySIN(sin);
//first name
JLabel fname = new JLabel("First Name:");
JTextField firstName = new JTextField(lst.get(1));
firstName.setColumns(15);
//last name
JLabel lname = new JLabel("Last Name:");
JTextField lastName = new JTextField(lst.get(0));
lastName.setColumns(15);
//D.O.B
JLabel date = new JLabel("D.O.B (DD/MM/YYYY):");
JTextField dob = new JTextField(lst.get(3));
dob.setColumns(15);
//Town
JLabel townLabel = new JLabel("Town:");
JTextField town = new JTextField(lst.get(4));
town.setColumns(15);
//Province
JLabel provLabel = new JLabel("Province:");
JTextField province = new JTextField(lst.get(5));
province.setColumns(15);
//Postal Code
JLabel postLabel = new JLabel("Postal Code:");
JTextField postal = new JTextField(lst.get(6));
postal.setColumns(15);
//Home Phone
JLabel homeLabel = new JLabel("Home Phone:");
JTextField homePhone = new JTextField(lst.get(7));
homePhone.setColumns(15);
//Mobile Phone
JLabel mobileLabel = new JLabel("Mobile Phone:");
JTextField mobilePhone = new JTextField(lst.get(8));
mobilePhone.setColumns(15);
//Sin
JLabel sinLabel = new JLabel("SIN:");
JLabel sinInfo = new JLabel(lst.get(2));
//create the update button
JButton update = new JButton("Update");
//place an ActionListener on the update button
update.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//make sure required fields are filled
if (firstName.getText().isEmpty() == false && lastName.getText().isEmpty() == false){
//open the backend and edit the clients info
backend back = new backend();
back.editClient(lst.get(2),
firstName.getText(),
lastName.getText(),
dob.getText(),
town.getText(),
province.getText(),
postal.getText(),
homePhone.getText(),
mobilePhone.getText());
//close the edit client frame
secondFrame.dispose();
//reload the main window
mainWindow();
}
else{
JOptionPane.showMessageDialog(null, "Please fill in the required fields. (SIN, First Name, Last Name)", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//create a clear button
JButton clear = new JButton("Clear");
//add an action listener to clear that clears the text fields
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstName.setText("");
lastName.setText("");
dob.setText("");
town.setText("");
province.setText("");
postal.setText("");
homePhone.setText("");
mobilePhone.setText("");
}
});
//add all secondFrame components
secondFrame.getContentPane().add(fname);
secondFrame.getContentPane().add(firstName);
secondFrame.getContentPane().add(lname);
secondFrame.getContentPane().add(lastName);
secondFrame.getContentPane().add(date);
secondFrame.getContentPane().add(dob);
secondFrame.getContentPane().add(townLabel);
secondFrame.getContentPane().add(town);
secondFrame.getContentPane().add(provLabel);
secondFrame.getContentPane().add(province);
secondFrame.getContentPane().add(postLabel);
secondFrame.getContentPane().add(postal);
secondFrame.getContentPane().add(homeLabel);
secondFrame.getContentPane().add(homePhone);
secondFrame.getContentPane().add(mobileLabel);
secondFrame.getContentPane().add(mobilePhone);
secondFrame.getContentPane().add(sinLabel);
secondFrame.getContentPane().add(sinInfo);
secondFrame.getContentPane().add(update);
secondFrame.getContentPane().add(clear);
//Set size and stop resize
secondFrame.setSize(300,300);
secondFrame.setResizable(false);
secondFrame.setVisible(true);
}
/**
* Creates a frame for editing a sale using the same JFrame used when adding a client/sale.
* Occupies the text fields using info retrieved from the backend about the sale.
*
* @param id - the transaction id for the sale to be edited, used to retrieve the rest of the info on the sale.
*/
public static void editingSale(String id){
//Create editing frame
secondFrame = new JFrame("Edit Sale");
secondFrame.getContentPane().setLayout(new GridLayout(0,2));
//Retrieve info
backend back = new backend();
ArrayList<String> lst = back.retrieveSaleInfoByID(id);
//id
JLabel idLabel = new JLabel("Transaction ID:");
JLabel idInfo = new JLabel(lst.get(0));
//amount
JLabel amountLabel = new JLabel("Amount ($):");
JTextField amountField = new JTextField(lst.get(1));
amountField.setColumns(15);
//date
JLabel dateLabel = new JLabel("Date (DD/MM/YYYY):");
JTextField dateField = new JFormattedTextField(lst.get(4));
dateField.setColumns(15);
//type
JLabel typeLabel = new JLabel("Type:");
JTextField typeField = new JTextField(lst.get(5));
typeField.setColumns(15);
//select client
JLabel clientLabel = new JLabel("Select a Client:");
//Create JTable from default model
DefaultTableModel cmodel = new DefaultTableModel();
JTable clientList = new JTable(clientModel);
//Add columns to the model
cmodel.addColumn("Name");
cmodel.addColumn("SIN");
//add the scroller to the pane
JScrollPane listscroller = new JScrollPane();
listscroller.setViewportView(clientList);
//open a backend and retrieve the client information
ArrayList<ArrayList<String>> clientInfo = back.retrieveClients();
//add the info retrieved to the client table
for (int i=0; i<clientInfo.size(); i++){
cmodel.addRow(new Object[]{clientInfo.get(i).get(1), clientInfo.get(i).get(0)});
}
//this keeps track of the row in clientList that holds the client this sale is attached to.
Integer row = null;
//iterate through the second column/sin column to find the client this sale is attached to
for (int i = cmodel.getRowCount() - 1; i >= 0; --i){
if (cmodel.getValueAt(i,1).equals(lst.get(3))){
row = i;
}
}