-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
1456 lines (1206 loc) · 47.1 KB
/
Main.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.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.*;
import java.time.format.*;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
//Main Function
public class Main extends Application {
/* MAIN */
Button button;
static Stage window;
static Scene firstscene, login_register, loginScene, registerScene;
public static void main(String[] args) throws IOException {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FirstLaunchCheck();
window = primaryStage;
firstMenu(window);
}
private static void firstMenu(Stage window) {
window.setTitle("Covid-19 Tracking System");
GridPane grid = new GridPane();
grid.setVgap(10);
grid.setHgap(12);
VBox buttonBox = new VBox();
buttonBox.setSpacing(10.0);
// Type of User
Label typeuserLabel = new Label("Type of User:");
typeuserLabel.setMaxWidth(Double.MAX_VALUE);
typeuserLabel.setAlignment(Pos.CENTER);
grid.add(typeuserLabel, 0, 0);
// Customer Button
Button customerButton = new Button("Customer");
customerButton.setOnAction(e -> {
try {
menuscreen("customer");
} catch (IOException e1) {
e1.printStackTrace();
}
});
// Admin Button
Button adminButton = new Button("Admin");
adminButton.setOnAction(e -> admin_loginscreen());
// Shop Button
Button shopButton = new Button("Shop");
shopButton.setOnAction(e -> {
try {
menuscreen("shop");
} catch (IOException e1) {
e1.printStackTrace();
}
});
buttonBox.setAlignment(Pos.BOTTOM_CENTER);
buttonBox.getChildren().addAll(customerButton, adminButton, shopButton);
grid.setAlignment(Pos.CENTER);
GridPane innergrid = new GridPane();
innergrid.setAlignment(Pos.CENTER);
innergrid.add(buttonBox, 0, 0);
grid.add(innergrid, 0, 3, 1, 1);
firstscene = new Scene(grid, 400, 600);
window.setScene(firstscene);
window.show();
}
// Function for checking if the user first time launching the program
private static void FirstLaunchCheck() throws IOException {
File customerinfo = new File("CustomerInfo.csv");
File custvisithistory = new File("CustomerVisitHistory.csv");
File shopinfo = new File("shop.csv");
// Checking if the user running this program first time or not
if (!customerinfo.exists() && !custvisithistory.exists() && !shopinfo.exists()) {
popupBox("Warning", "First time Lauch: \n Proceeding to create relevant CSV files.");
}
// Checking if all of the relevant files exists or not
if (customerinfo.exists() && custvisithistory.exists() && shopinfo.exists()) {
popupBox("All clear", "All relevant CSV files exists in current directory: \n Proceeding the program.");
}
// Creating CustomerInfo.csv if the file does not exists and preloading 5
// customers into the CustomerInfo.csv
if (customerinfo.createNewFile()) {
popupBox("Warning", "CustomerInfo.csv is missing");
ArrayList<Customer> preload_cust = new ArrayList<Customer>();
BufferedWriter writer = new BufferedWriter(new FileWriter("CustomerInfo.csv", true));
preload_cust.add(new Customer("ali011", "012345", "Ali bin Abu", "0175551298", "normal"));
preload_cust.add(new Customer("sitimay012", "012345", "Siti Maysarah", "0125551346", "normal"));
preload_cust.add(new Customer("azrul013", "012345", "Azrul Mutu", "0155555105", "normal"));
preload_cust.add(new Customer("shuhail014", "012345", "Shuhail Azri", "0145353574", "normal"));
preload_cust.add(new Customer("arif015", "012345", "Arif Bakar", "0145575671", "normal"));
for (int i = 0; i < preload_cust.size(); i++) {
writer.write(preload_cust.get(i).toCSVString());
writer.newLine();
}
writer.close();
popupBox("Clear", "CustomerInfo.csv succesfully created.");
}
// Creating CustomerVisitHistory.csv if the file does not exists
if (custvisithistory.createNewFile()) {
popupBox("Warning", "CustomerVisitHistory.csv is missing");
popupBox("Clear", "CustomerVisitHistory.csv succesfully created.");
}
// Creating shop.csv if the file does not exists and preloading 4 shops into the
// shop.csv
if (shopinfo.createNewFile()) {
ArrayList<Shop> preload_shop = new ArrayList<Shop>();
BufferedWriter writer = new BufferedWriter(new FileWriter("shop.csv", true));
preload_shop.add(new Shop("Aeon", "0331015568", "Imran", "normal"));
preload_shop.add(new Shop("Tesco", "0392820771", "Shanthur", "normal"));
preload_shop.add(new Shop("Jusco", "0332902193", "YapzhengYew", "normal"));
preload_shop.add(new Shop("1Utama", "0333235399", "Kuberan", "normal"));
for (int i = 0; i < preload_shop.size(); i++) {
writer.write(preload_shop.get(i).toCSVString());
writer.newLine();
}
writer.close();
popupBox("Warning", "shop.csv is missing");
popupBox("Clear", "shop.csv succesfully created.");
}
}
// Function to fetch the correct menuscreen according to the user type that is
// logged in.
private static void menuscreen(String Valid_UserType) throws IOException {
// Menu Screen for admins
if (Valid_UserType.equals("admin")) {
admin_menuscreen();
}
// Menu Screen for customers
else if (Valid_UserType.equals("customer")) {
customer_LogRegScreen();
}
// Menu for shops
else if (Valid_UserType.equals("shop")) {
shop_menuscren();
}
}
/*
* ===================================== ADMIN
* =====================================
*/
// Admin Login Screen
private static void admin_loginscreen() {
window.setTitle("Admin Login");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setVgap(10);
grid.setHgap(12);
HBox buttonBox = new HBox();
buttonBox.setSpacing(10.0);
Label title = new Label("Please enter the masterpassword");
title.setMaxWidth(Double.MAX_VALUE);
title.setAlignment(Pos.CENTER);
grid.add(title, 0, 0);
PasswordField masterPassword = new PasswordField();
masterPassword.setPromptText("Master Password");
grid.add(masterPassword, 0, 1);
masterPassword.setOnAction(c -> {
if (MasterPassword_validating(masterPassword.getText())) {
admin_menuscreen();
} else {
popupBox("Admin Login Error!", "Invalid Masterpassword\nPlease try again");
}
});
Button proceedButton = new Button("Proceed");
proceedButton.setOnAction(c -> {
if (MasterPassword_validating(masterPassword.getText())) {
admin_menuscreen();
} else {
popupBox("Admin Login Error!", "Invalid Masterpassword\nPlease try again");
}
});
Button backButton = new Button("Back");
backButton.setOnAction(c -> firstMenu(window));
buttonBox.setAlignment(Pos.BOTTOM_CENTER);
buttonBox.getChildren().addAll(proceedButton, backButton);
GridPane innergrid = new GridPane();
innergrid.setAlignment(Pos.CENTER);
innergrid.add(buttonBox, 0, 0);
grid.add(innergrid, 0, 3, 1, 1);
loginScene = new Scene(grid, 400, 600);
window.setScene(loginScene);
window.show();
}
// Admin MenuScreen
private static void admin_menuscreen() {
window.setTitle("Admin MenuScreen");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
VBox buttonBox = new VBox();
buttonBox.setSpacing(10.0);
grid.setVgap(10);
grid.setHgap(12);
Label title = new Label("Please select the option below");
title.setMaxWidth(Double.MAX_VALUE);
title.setAlignment(Pos.CENTER);
grid.add(title, 0, 0);
Button custdetailsButton = new Button("View all customer's detials");
custdetailsButton.setOnAction(c -> {
try {
admin_viewcustdetials();
} catch (IOException e) {
e.printStackTrace();
}
});
Button shopdetailsButton = new Button("View all shop's details");
shopdetailsButton.setOnAction(c -> {
try {
admin_viewshopdetials();
} catch (IOException e) {
e.printStackTrace();
}
});
Button masterhistoryButton = new Button("View the master visit history");
masterhistoryButton.setOnAction(c -> {
try {
admin_viewmastervisit();
} catch (IOException e) {
e.printStackTrace();
}
});
Button flagcustButton = new Button("Flag a customer");
flagcustButton.setOnAction(c -> {
try {
admin_flagcus();
} catch (IOException e) {
e.printStackTrace();
}
});
Button generate30visitsButton = new Button("Generata random 30 visit history");
generate30visitsButton.setOnAction(c -> {
try {
generating30randomvisit();
popupBox("Random Visit Generated!", "30 random visit succesfully generated");
} catch (IOException e) {
e.printStackTrace();
}
});
Button backButton = new Button("back");
backButton.setOnAction(c -> firstMenu(window));
buttonBox.setAlignment(Pos.BOTTOM_CENTER);
buttonBox.getChildren().addAll(custdetailsButton, shopdetailsButton, masterhistoryButton, flagcustButton,
generate30visitsButton, backButton);
GridPane innergrid = new GridPane();
innergrid.setAlignment(Pos.CENTER);
innergrid.add(buttonBox, 0, 0);
grid.add(innergrid, 0, 3, 1, 1);
Scene adminmenuScene = new Scene(grid, 400, 600);
window.setScene(adminmenuScene);
window.show();
}
// Admin MenuScreenAction
// Admin view all customer details action
@SuppressWarnings("unchecked")
private static void admin_viewcustdetials() throws IOException {
window.setTitle("Customer Details");
ArrayList<Customer> CustInfo = readCustomerInfoFromFile();
ObservableList<Customer> ListCustInfo = FXCollections.observableArrayList();
for (int i = 0; i < CustInfo.size(); i++) {
ListCustInfo.add(new Customer(CustInfo.get(i).getCust_username(), CustInfo.get(i).getCust_password(),
CustInfo.get(i).getCust_fullname(), CustInfo.get(i).getCust_phonenum(),
CustInfo.get(i).getCust_status()));
}
// Ful Name
TableColumn<Customer, String> fullnameColumn = new TableColumn<>("Name");
fullnameColumn.setMinWidth(200);
fullnameColumn.setCellValueFactory(new PropertyValueFactory<>("Cust_fullname"));
// Phone Number
TableColumn<Customer, String> phonenumColumn = new TableColumn<>("Phone");
phonenumColumn.setMinWidth(200);
phonenumColumn.setCellValueFactory(new PropertyValueFactory<>("Cust_phonenum"));
// Status
TableColumn<Customer, String> statusColumn = new TableColumn<>("Status");
statusColumn.setMinWidth(200);
statusColumn.setCellValueFactory(new PropertyValueFactory<>("Cust_status"));
TableView<Customer> custdetails_Table = new TableView<>();
custdetails_Table.setItems(ListCustInfo);
// No
TableColumn<Customer, Number> indexColumn = new TableColumn<Customer, Number>("No");
indexColumn.setSortable(false);
indexColumn.setCellValueFactory(column -> new ReadOnlyObjectWrapper<Number>(
custdetails_Table.getItems().indexOf(column.getValue()) + 1));
custdetails_Table.getColumns().addAll(indexColumn, fullnameColumn, phonenumColumn, statusColumn);
Button backButton = new Button("Back");
backButton.setOnAction(c -> admin_menuscreen());
VBox box = new VBox();
GridPane tableGrid = new GridPane();
GridPane buttonGrid = new GridPane();
HBox buttonBox = new HBox();
buttonBox.setAlignment(Pos.BOTTOM_CENTER);
buttonBox.getChildren().addAll(backButton);
buttonGrid.add(buttonBox, 0, 0);
box.getChildren().addAll(custdetails_Table);
tableGrid.add(box, 0, 0);
tableGrid.add(buttonBox, 0, 3, 3, 3);
Scene custdetailsScene = new Scene(tableGrid);
window.setScene(custdetailsScene);
window.show();
}
// Admin view all customer details action
@SuppressWarnings("unchecked")
private static void admin_viewshopdetials() throws IOException {
window.setTitle("Shop Details");
ArrayList<Shop> shops = readShopsFromFile();
ObservableList<Shop> shopList = FXCollections.observableArrayList();
for (int i = 0; i < shops.size(); i++) {
shopList.add(new Shop(shops.get(i).getShop_name(), shops.get(i).getShop_phonenumber(),
shops.get(i).getShop_managername(), shops.get(i).getShop_status()));
}
// Shop Name
TableColumn<Shop, String> shopnameColumn = new TableColumn<>("Name");
shopnameColumn.setMinWidth(200);
shopnameColumn.setCellValueFactory(new PropertyValueFactory<>("shop_name"));
// Phone Number
TableColumn<Shop, String> phonenumColumn = new TableColumn<>("Phone");
phonenumColumn.setMinWidth(200);
phonenumColumn.setCellValueFactory(new PropertyValueFactory<>("shop_phonenumber"));
// Manager name
TableColumn<Shop, String> managernameColumn = new TableColumn<>("Manager");
managernameColumn.setMinWidth(200);
managernameColumn.setCellValueFactory(new PropertyValueFactory<>("shop_managername"));
// Status
TableColumn<Shop, String> statusColumn = new TableColumn<>("Status");
statusColumn.setMinWidth(200);
statusColumn.setCellValueFactory(new PropertyValueFactory<>("shop_status"));
TableView<Shop> shopList_Table = new TableView<>();
shopList_Table.setItems(shopList);
// No
TableColumn<Shop, Number> indexColumn = new TableColumn<Shop, Number>("No");
indexColumn.setSortable(false);
indexColumn.setCellValueFactory(
column -> new ReadOnlyObjectWrapper<Number>(shopList_Table.getItems().indexOf(column.getValue()) + 1));
shopList_Table.getColumns().addAll(indexColumn, shopnameColumn, phonenumColumn, managernameColumn,
statusColumn);
Button backButton = new Button("Back");
backButton.setOnAction(c -> admin_menuscreen());
VBox box = new VBox();
GridPane tableGrid = new GridPane();
GridPane buttonGrid = new GridPane();
HBox buttonBox = new HBox();
buttonBox.setAlignment(Pos.BOTTOM_CENTER);
buttonBox.getChildren().addAll(backButton);
buttonGrid.add(buttonBox, 0, 0);
box.getChildren().addAll(shopList_Table);
tableGrid.add(box, 0, 0);
tableGrid.add(buttonBox, 0, 3, 3, 3);
Scene shopdetailsScene = new Scene(tableGrid);
window.setScene(shopdetailsScene);
window.show();
}
// Admin view the master visit history
@SuppressWarnings("unchecked")
private static void admin_viewmastervisit() throws IOException {
window.setTitle("Master Visit History");
ArrayList<CustomerVisitHistory> custvisithistory = readCustomerVisitHistoryFromFile();
ObservableList<CustomerVisitHistory> custvisithistoryList = FXCollections.observableArrayList();
for (int i = 0; i < custvisithistory.size(); i++) {
custvisithistoryList.add(new CustomerVisitHistory(custvisithistory.get(i).getCustomer_name(),
custvisithistory.get(i).getShop(), custvisithistory.get(i).getvisitdatetime()));
}
// Date
TableColumn<CustomerVisitHistory, String> DateColumn = new TableColumn<>("Date");
DateColumn.setMinWidth(200);
DateColumn.setCellValueFactory(new PropertyValueFactory<>("date"));
// Time
TableColumn<CustomerVisitHistory, String> TimeColumn = new TableColumn<>("Time");
TimeColumn.setMinWidth(200);
TimeColumn.setCellValueFactory(new PropertyValueFactory<>("time"));
// Customer name
TableColumn<CustomerVisitHistory, String> custnameColumn = new TableColumn<>("Customer");
custnameColumn.setMinWidth(200);
custnameColumn.setCellValueFactory(new PropertyValueFactory<>("customer_name"));
// Shop
TableColumn<CustomerVisitHistory, String> ShopColumn = new TableColumn<>("Shop");
ShopColumn.setMinWidth(200);
ShopColumn.setCellValueFactory(new PropertyValueFactory<>("shop"));
TableView<CustomerVisitHistory> visithistory_Table = new TableView<>();
visithistory_Table.setItems(custvisithistoryList);
// No
TableColumn<CustomerVisitHistory, Number> indexColumn = new TableColumn<CustomerVisitHistory, Number>("No");
indexColumn.setSortable(false);
indexColumn.setCellValueFactory(column -> new ReadOnlyObjectWrapper<Number>(
visithistory_Table.getItems().indexOf(column.getValue()) + 1));
visithistory_Table.getColumns().addAll(indexColumn, DateColumn, TimeColumn, custnameColumn,ShopColumn);
Button backButton = new Button("Back");
backButton.setOnAction(c -> admin_menuscreen());
VBox box = new VBox();
GridPane tableGrid = new GridPane();
GridPane buttonGrid = new GridPane();
HBox buttonBox = new HBox();
buttonBox.setAlignment(Pos.BOTTOM_CENTER);
buttonBox.getChildren().addAll(backButton);
buttonGrid.add(buttonBox, 0, 0);
box.getChildren().addAll(visithistory_Table);
tableGrid.add(box, 0, 0);
tableGrid.add(buttonBox, 0, 3, 3, 3);
Scene matervisitScene = new Scene(tableGrid);
window.setScene(matervisitScene);
window.show();
}
//Admin Flag Customer Screen
private static void admin_flagcus() throws IOException {
window.setTitle("Customer Flagging");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
VBox box = new VBox(10);
box.setSpacing(10.0);
Label title = new Label("Please select the option below");
title.setMaxWidth(Double.MAX_VALUE);
title.setAlignment(Pos.CENTER);
grid.add(title, 0, 0);
ChoiceBox<String> customerchoiceBox = new ChoiceBox<>();
ChoiceBox<String> statutschoiceBox = new ChoiceBox<>();
ArrayList<Customer> custinfo = readCustomerInfoFromFile();
for (int i = 0; i < custinfo.size(); i++) {
customerchoiceBox.getItems().add(custinfo.get(i).getCust_fullname());
}
statutschoiceBox.getItems().addAll("normal", "case");
Button proceedButton = new Button("Proceed");
proceedButton.setOnAction(c -> {
try {
StatusChanging(statutschoiceBox.getValue(), customerchoiceBox.getValue());
} catch (IOException e1) {
e1.printStackTrace();
}
});
Button backButton = new Button("back");
backButton.setOnAction(c -> admin_menuscreen());
box.setAlignment(Pos.BOTTOM_CENTER);
box.getChildren().addAll(customerchoiceBox, statutschoiceBox, proceedButton, backButton);
GridPane innergrid = new GridPane();
innergrid.setAlignment(Pos.CENTER);
innergrid.add(box, 0, 0);
grid.add(innergrid, 0, 3, 3, 3);
Scene checkinScene = new Scene(grid, 400, 600);
window.setScene(checkinScene);
window.show();
}
// Changing status
private static void StatusChanging(String status, String fullname) throws IOException {
ArrayList<Customer> CustInfo = readCustomerInfoFromFile();
ArrayList<CustomerVisitHistory> CustVisitHistory = readCustomerVisitHistoryFromFile();
ArrayList<Shop> shops = readShopsFromFile();
int selectedcust_index = -1;
for (int l = 0; l < CustInfo.size(); l++) {
if (CustInfo.get(l).getCust_fullname().equals(fullname)) {
selectedcust_index = l;
break;
}
}
ArrayList<Integer> flaggedcust_visithistory = new ArrayList<>(); // flagged customer's visit history index
// Writers for CustomerInfo.csv and shop.csv
BufferedWriter CustInfo_writer = new BufferedWriter(new FileWriter("CustomerInfo.csv"));
BufferedWriter ShopInfo_writer = new BufferedWriter(new FileWriter("shop.csv"));
if (status.equals("case")) {
// Searching for the visit history of the flagged customer for further changes
// of status
CustInfo.get(selectedcust_index).set_Customerstatus(status);
for (int i = 0; i < CustVisitHistory.size(); i++) {
if (CustVisitHistory.get(i).getCustomer_name().equals(fullname)) {
flaggedcust_visithistory.add(i);
}
}
// Searching for the shop that the flagged customer visited and set the shop
// status
for (int i = 0; i < shops.size(); i++) {
for (int j = 0; j < flaggedcust_visithistory.size(); j++) {
String shopname = shops.get(i).getShop_name();
String shopnamefromhistory = CustVisitHistory.get(flaggedcust_visithistory.get(j)).getShop();
if (shopname.equals(shopnamefromhistory)) {
shops.get(i).setshop_status(status);
}
}
}
// Searching for the close contact customer and change their status to close if
// they are in the one hour range before and after the flagged customer's visit
// time
for (int i = 0; i < CustVisitHistory.size(); i++) {
for (int j = 0; j < flaggedcust_visithistory.size(); j++) {
String shopnamefromhistory = CustVisitHistory.get(flaggedcust_visithistory.get(j)).getShop();
if (!CustVisitHistory.get(i).getCustomer_name().equals(fullname) && CustVisitHistory.get(i).getShop().equals(shopnamefromhistory)) {
LocalDateTime flaggedcust_visit = CustVisitHistory.get(flaggedcust_visithistory.get(j)).getvisitdatetime().withSecond(0);
LocalDateTime possibleclosecust_visit = CustVisitHistory.get(i).getvisitdatetime().withSecond(0);
int compareValue = possibleclosecust_visit.compareTo(flaggedcust_visit);
if(compareValue > 0)
{
Duration duration = Duration.between(flaggedcust_visit, possibleclosecust_visit);
if (duration.toMinutes() <= 60 && duration.toMinutes() >= 0 ) {
for (int k = 0; k < CustInfo.size(); k++) {
if (CustInfo.get(k).getCust_fullname()
.equals(CustVisitHistory.get(i).getCustomer_name())) {
CustInfo.get(k).set_Customerstatus("close");
}
}
}
}
else if (compareValue < 0)
{
Duration duration = Duration.between(possibleclosecust_visit, flaggedcust_visit);
if (duration.toMinutes() <= 60 && duration.toMinutes() >= 0) {
for (int k = 0; k < CustInfo.size(); k++) {
if (CustInfo.get(k).getCust_fullname()
.equals(CustVisitHistory.get(i).getCustomer_name())) {
CustInfo.get(k).set_Customerstatus("close");
}
}
}
}
}
}
}
} else if (status.equals("normal")) {
// Searching for the visit history of the flagged customer for further changes
// of status
CustInfo.get(selectedcust_index).set_Customerstatus(status);
for (int i = 0; i < CustVisitHistory.size(); i++) {
if (CustVisitHistory.get(i).getCustomer_name().equals(fullname)) {
flaggedcust_visithistory.add(i);
}
}
// Searching for the shop that the flagged customer visited and set the shop
// status
for (int i = 0; i < shops.size(); i++) {
for (int j = 0; j < flaggedcust_visithistory.size(); j++) {
String shopname = shops.get(i).getShop_name();
String shopnamefromhistory = CustVisitHistory.get(flaggedcust_visithistory.get(j)).getShop();
if (shopname.equals(shopnamefromhistory)) {
shops.get(i).setshop_status(status);
}
}
}
}
// Rewriting the customer's data into CustomerInfo.csv
for (int i = 0; i < CustInfo.size(); i++) {
CustInfo_writer.write(CustInfo.get(i).toCSVString());
CustInfo_writer.newLine();
}
// Rewriting the shop's data into shop.csv
for (int i = 0; i < shops.size(); i++) {
ShopInfo_writer.write(shops.get(i).toCSVString());
ShopInfo_writer.newLine();
}
// Closing the two writers after rewriting the csv files.
CustInfo_writer.close();
ShopInfo_writer.close();
popupBox("Flagged Succcess!", "The customer [" + fullname + "] " + "has been succesfully flagged " + status +".");
}
// Randomly generate a time in between two intervals "StarInclusive" and
// "endExclusive" with the zoneid = "UTC+0800" , lastly return the generated
// time as a LocalDateTime object
private static LocalDateTime between(LocalDateTime startInclusive, LocalDateTime endExclusive) {
ZoneId zoneid = ZoneId.ofOffset("UTC", ZoneOffset.ofHours(8));
long epoch_startSeconds = startInclusive.atZone(zoneid).toEpochSecond();
long epoch_endSeconds = endExclusive.atZone(zoneid).toEpochSecond();
long random = ThreadLocalRandom.current().nextLong(epoch_startSeconds, epoch_endSeconds);
return LocalDateTime.ofEpochSecond(random, 0, ZoneOffset.ofHours(8));
}
// Generating 30 random visit history
private static void generating30randomvisit() throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("CustomerVisitHistory.csv", true));
ArrayList<CustomerVisitHistory> CustomerVisitHistory = new ArrayList<>();
// Fetching the list of customer's name
ArrayList<Customer> CustInfo = readCustomerInfoFromFile();
String[] custnamelist = new String[CustInfo.size()];
for (int i = 0; i < CustInfo.size(); i++)
custnamelist[i] = CustInfo.get(i).getCust_fullname();
// Fetching the list of shop's name
ArrayList<Shop> shops = readShopsFromFile();
String[] shoplist = new String[shops.size()];
for (int i = 0; i < shops.size(); i++)
shoplist[i] = shops.get(i).getShop_name();
// LocalDateTime checkintime = LocalDateTime.now();
// Generating random 30 visit history
for (int i = 0; i < 30; i++) {
Random r = new Random();
// picking random index from the custermer namelist
int custnamerandom_idx = r.nextInt(custnamelist.length);
// picking random index from the shoplist
int shoprandom_idx = r.nextInt(shoplist.length);
// randomdatetime generator
LocalDateTime onedayago = LocalDateTime.now().minus(Duration.ofDays(1)); // time of one day ago depends on
// the local system time
LocalDateTime randomDateTime = between(onedayago, LocalDateTime.now()); // randomly generate time between
// yesterday of current system time
// and current system time
CustomerVisitHistory.add(new CustomerVisitHistory(custnamelist[custnamerandom_idx],
shoplist[shoprandom_idx], randomDateTime));
writer.write(CustomerVisitHistory.get(i).toCSVString());
writer.newLine();
}
writer.close();
}
// CUSTOMER
// Customer Login/Register Screen
private static void customer_LogRegScreen() throws IOException {
window.setTitle("Customer Login Register Screen");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
VBox buttonBox = new VBox();
buttonBox.setSpacing(10.0);
grid.setVgap(10);
grid.setHgap(12);
Label title = new Label("Please select the option below");
title.setMaxWidth(Double.MAX_VALUE);
title.setAlignment(Pos.CENTER);
grid.add(title, 0, 0);
Button loginButton = new Button("Login");
loginButton.setOnAction(e -> {
try {
customerLogin();
} catch (IOException e1) {
e1.printStackTrace();
}
});
Button registerButton = new Button("Register");
registerButton.setOnAction(e -> {
try {
customerRegister();
} catch (IOException e1) {
e1.printStackTrace();
}
});
Button exitButton = new Button("Back");
exitButton.setOnAction(e -> firstMenu(window));
buttonBox.setAlignment(Pos.BOTTOM_CENTER);
buttonBox.getChildren().addAll(loginButton, registerButton, exitButton);
GridPane innergrid = new GridPane();
innergrid.setAlignment(Pos.CENTER);
innergrid.add(buttonBox, 0, 0);
grid.add(innergrid, 0, 3, 1, 1);
loginScene = new Scene(grid, 400, 600);
window.setScene(loginScene);
window.show();
}
// Customer Login Screen
private static void customerLogin() throws IOException {
window.setTitle("Login");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setVgap(10);
grid.setHgap(12);
HBox buttonBox = new HBox();
buttonBox.setSpacing(10.0);
Label title = new Label("Login");
title.setMaxWidth(Double.MAX_VALUE);
title.setAlignment(Pos.CENTER);
grid.add(title, 0, 0);
TextField loginUserName = new TextField();
loginUserName.setPromptText("Username");
grid.add(loginUserName, 0, 1);
PasswordField loginPassword = new PasswordField();
loginPassword.setPromptText("Password");
grid.add(loginPassword, 0, 2);
loginUserName.setOnAction(c -> {
try {
LoginAction(loginUserName, loginPassword);
} catch (IOException e) {
e.printStackTrace();
}
});
loginPassword.setOnAction(c -> {
try {
LoginAction(loginUserName, loginPassword);
} catch (IOException e) {
e.printStackTrace();
}
});
Button proceedButton = new Button("Proceed");
proceedButton.setOnAction(c -> {
try {
LoginAction(loginUserName, loginPassword);
} catch (IOException e) {
e.printStackTrace();
}
});
Button backButton = new Button("Back");
backButton.setOnAction(c -> {
try {
customer_LogRegScreen();
} catch (IOException e) {
e.printStackTrace();
}
});
buttonBox.setAlignment(Pos.BOTTOM_CENTER);
buttonBox.getChildren().addAll(proceedButton, backButton);
GridPane innergrid = new GridPane();
innergrid.setAlignment(Pos.CENTER);
innergrid.add(buttonBox, 0, 0);
grid.add(innergrid, 0, 3, 1, 1);
loginScene = new Scene(grid, 400, 600);
window.setScene(loginScene);
window.show();
}
// Customer Login Action
private static void LoginAction(TextField loginUserName, PasswordField loginPassword) throws IOException {
ArrayList<Customer> CustInfo = readCustomerInfoFromFile();
String Luser = loginUserName.getText().trim().toLowerCase();
String Lpassword = loginPassword.getText().trim().toLowerCase();
Boolean usernamevalidation = true;
Boolean passwordvalidation = true;
for (int i = 0; i < CustInfo.size(); i++) {
if (!Luser.equals(CustInfo.get(i).getCust_username())) {
usernamevalidation = false;
} else {
usernamevalidation = true;
if (!Lpassword.equals(CustInfo.get(i).getCust_password())) {
passwordvalidation = false;
break;
} else {
passwordvalidation = true;
break;
}
}
}
if (usernamevalidation == false) {
popupBox("Login Error", "Invalid username");
loginPassword.clear();
loginUserName.clear();
} else if (passwordvalidation == false) {
popupBox("Login Error", "Invalid password");
loginPassword.clear();
loginUserName.clear();
}
if (usernamevalidation == true && passwordvalidation == true) {
popupBox("Login Successful!", "Logged in successfully!");
customer_Menuscreen(Luser);
}
}
// popupBox for any messages
private static void popupBox(String title, String message) {
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
Label label = new Label(message);
Button closeButton = new Button("Close");
closeButton.setOnAction(c -> window.close());
VBox layout = new VBox(10);
layout.getChildren().setAll(label, closeButton);
layout.setAlignment(Pos.CENTER);
Scene scene = new Scene(layout, 300, 200);
window.setScene(scene);
window.showAndWait();
}
private static void customerRegister() throws IOException {
window.setTitle("Register");
ArrayList<Customer> CustInfo = readCustomerInfoFromFile();
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(12);
HBox buttonBox = new HBox();
buttonBox.setSpacing(10.0);
Label title = new Label("Register");
title.setMaxWidth(Double.MAX_VALUE);
title.setAlignment(Pos.CENTER);
grid.add(title, 0, 0);
TextField regUserName = new TextField();
regUserName.setPromptText("Your Username");
grid.add(regUserName, 0, 1);
TextField regPassword = new TextField();
regPassword.setPromptText("Your Password");
grid.add(regPassword, 0, 2);
TextField regFullname = new TextField();
regFullname.setPromptText("Your Fullname");
grid.add(regFullname, 0, 3);
TextField regPhonenumber = new TextField();
regPhonenumber.setPromptText("Your Phone Number");
grid.add(regPhonenumber, 0, 4);
Button proceedButton = new Button("Proceed");
proceedButton.setOnAction(c -> {
Boolean RuserValid = true;
Boolean RpasswordValid = true;
Boolean RfullnameValid = true;
Boolean RphoneValid = true;
String Ruser = regUserName.getText();
String Rpassword = regPassword.getText();
String Rfullname = regFullname.getText();
ArrayList<String> registrationinfo = new ArrayList<String>();
for (int i = 0; i < CustInfo.size(); i++) {
if (Ruser.equals(CustInfo.get(i).getCust_username())) {
RuserValid = false;
regUserName.clear();
popupBox("Register Error", "Username exists");
} else {
RuserValid = true;
}
}
if (Ruser.length() == 0) {
RuserValid = false;
popupBox("Register Error", "Username is Empty");
}
if (Rpassword.length() == 0) {
RpasswordValid = false;
popupBox("Register Error", "Password is Empty");
}
if (Rfullname.length() == 0) {
RfullnameValid = false;
popupBox("Register Error", "Fullname is Empty");
}
try {
double validphonenumber = Double.parseDouble(regPhonenumber.getText());
RphoneValid = true;
} catch (NumberFormatException ex) {
RphoneValid = false;
regPhonenumber.clear();
popupBox("Register Error", "Invalid Phone Number Try again");
}
if (RuserValid == true && RpasswordValid == true && RfullnameValid == true && RphoneValid == true) {
registrationinfo.add(Ruser);
registrationinfo.add(regPassword.getText());
registrationinfo.add(regFullname.getText());
registrationinfo.add(regPhonenumber.getText());
registrationinfo.add("normal");
StringBuilder sb = new StringBuilder();
for (String element : registrationinfo) {