-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSavingsAccount.java
More file actions
1536 lines (1285 loc) · 59.2 KB
/
Copy pathSavingsAccount.java
File metadata and controls
1536 lines (1285 loc) · 59.2 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
//TODO USING SAVINGS.CSV IT'S A WIP
//Savings Account: is where you hold on to your money and in a while you can get interest for holding that money within a specific time period.
//this is used for the random generator.
import java.io.BufferedReader; //Random is used for the random ID generator
import java.io.BufferedWriter;
import java.io.IOException; //This helps us write data to the CSV file.
import java.nio.file.Files; //catch errors if anything silly happens.
import java.nio.file.Path; //to make it easier to access the files read and write functions. Our HasSavings is a static so we need static methods to make the code work. Files work hand to hand with path objects instead of using Strings we could use that which makes it platform independent.
import java.nio.file.StandardCopyOption; //Array list is needed when we don't know the size of an array or when we resize an array if you see SavingsIDexists I used array list to capture all the columns and use it to compare with the current savings ID with the savings ID in the current array list. Something like this psuedocode currentsavingsID = currentarraylistsavings.
import java.nio.file.StandardOpenOption;
import java.time.LocalDate; //This function is used to find the file you want for example I'm using this to find my Savings.csv
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList; //we don't want to overwrite when we create a savings ID account we want to append.
import java.util.List;
import java.util.Random; //is used to read line by line
import java.util.Scanner;
public class SavingsAccount {
/**
* * Variables **
*/
private double savingsbalance = 0; // TODO this is our savings balance which will start in a range if the customer first creates their account in about 100-300 $
private final double minBalanceFee = 15; // minimum balance fee is 15 dollars.
private final double monthlyFee = 12; // TODO monthly fee is 12 dollars.
private final double yearlyFee = 48; // TODO 48 dollars.
//private static csvFile file; // csvFile equals to the path of the CSV file.
//private static csvFile FeeCheck; // savingsFee will equal to CSVpathFee
private String userID; // current userID lets say they registered or they were recent the constructor we'll need userID as a verification method
private String SavingsID; //savings ID is a unique verification method to see if the user has a savings account or not.
private final double interestamount = 0.0042; //interest is 5%
private String phoneNumber; // store a users phone number.
private String driversLicense;
private String birthcertificate;
private boolean isEmployee = false;
private LocalDate datecreation;
private boolean hasSavings;
//early closure account variables:
private static final int MIN_DAY = 0;
private static final int MAX_DAY = 180;
private static final double MAX_FEE_LIMIT = 50;
private static final double MIN_FEE = 5;
/* Static Final variables */
private static final Path csvPath = Path.of("Savings.csv"); // fine the path for the CSV file
private static final Path csvPathFee = Path.of("SavingsFeeCheck.csv"); // measuring monthly
private static final Path csvCustomerInfo = Path.of("customerinfo.csv");
private static final Path csvEmployeecsv = Path.of("employeecards.csv");
private static final Path csvEmployeesavingscsv = Path.of("EmployeeSavings.csv");
private static final long MAX = 199_999_999_999L; // This is the maximum for the savings number generator.
// //1000000000000 is Savings Account UNIQUE ID this is only for
// savings.
private static final long MIN = 100_000_000_000L; // minimum for the random number generator
private static final double minimumbalance = 100;
private static final double maximumbalance = 300;
private final List<String> transactionHistory = new ArrayList<>();
private void logTransaction(String type, double amount) {
String entry = String.format(
"%s | %s | $%.2f | bal=%.2f",
LocalDate.now(),
type,
amount,
savingsbalance
);
transactionHistory.add(entry);
}
// Instant now = Instant.now(); example of how to use Instance the
// variable(object) equals to the current time only once meaning time is moving
// while the variable is only equal to a non incrementing time this is useful
// for when we want to start initiating fee 24 hour charge.
// ZonedDateTime nowEastern = ZonedDateTime.now(EASTERN_ZONE);
// TODO long number = MIN + (long)(rand.nextDouble() * (MAX - MIN + 1));
// try catch exceptions if the csvfile fails to load
/*static {
try {
file = new csvFile(csvPath);
FeeCheck = new csvFile(csvPathFee);
} catch (IOException e) {
e.printStackTrace();
}
}*/
public SavingsAccount() // when creating
{
savingsbalance = minimumbalance; // make savings start at 100 to start with a default starting value.
}
public SavingsAccount(String userID, double savingsamount) // Work in progress.
{
this.userID = userID;
savingsbalance = savingsamount;// TODO I need to create a method that checks in the CSV file, if the userID has
// a savings account or not if not then request back to them they don't have it.
// Request the User if he wants to create a savings account.
}
/*
* CHECK IF THE USER HAS A SAVINGS ACCOUNT THE RETURN FUNCTION WILL BE THE FINAL
* SIGNAL, use this inside createSavings or existingSavings
*/
public static boolean userIDExists (String userID) throws IOException { //hasMoney or user ID //lets use this as a chance to create a boolean employee.
Path csvEmployeeornot = isEmployee(userID) ? csvEmployeesavingscsv : csvPath;
try (BufferedReader reader = Files.newBufferedReader(csvEmployeeornot)) {
reader.readLine(); //skip the header
String line; //String line not initialized yet
while ((line = reader.readLine()) != null) {
String[] columnsplit = line.split(",", -1); //split line into 3 columns instead of one huge string because we don't want that.
if (columnsplit.length > 0 && columnsplit[0].trim().equals(userID.trim())) { //trim is useful for comparing data when white space exists what it does is removes those white spaces.
return true; //return true because userID exists
}
}
}
return false;
}
public static boolean isEmployee(String EmployeeID) throws IOException { //This method checks if there is the userID in employee.csv if that appears there then this is an employee MoneyMarket account.
try (BufferedReader reader = Files.newBufferedReader(csvEmployeecsv)) {
reader.readLine(); // skip header
String line;
while ((line = reader.readLine()) != null) {
ArrayList<String> data = csvParsing.parseLine(line);
if (line.trim().isEmpty()) {
continue;
}
if (!data.isEmpty() && data.get(0).equals(EmployeeID)) { //read column 0 and lines that match with the employeeID
return true;
}
}
}
return false; // not found
}
public static boolean SavingsIDExistsfeefile(String SavingsID) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(csvPathFee)) {
reader.readLine(); // skip header
String line;
while ((line = reader.readLine()) != null) {
String[] columnsplit = line.split(",", -1);
if (columnsplit.length == 0) {
continue; // skip malformed lines
}if (columnsplit[0].trim().equals(SavingsID.trim())) {
return true; // userID found
}
}
}
return false; // not found
}
//write to csv
public static boolean writefeeUser(String savingsID, String DriversLicense, String BirthCertificate) throws IOException {
Path temp = Files.createTempFile("temp", ".csv");
LocalDate today = LocalDate.now();
boolean found = false;
try (BufferedReader reader = Files.newBufferedReader(csvPathFee); BufferedWriter writer = Files.newBufferedWriter(temp)) {
String header = reader.readLine();
if (header != null) {
writer.write(header);
writer.newLine();
}
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",", -1);
if (data.length > 0 && data[0].equals(savingsID)) {
found = true;
writer.newLine();
continue;
}
writer.write(line);
writer.newLine();
}
// If account not found → initialize tracking row
if (!found) {
writer.write(String.join(",",
savingsID,
today.toString(),
today.toString(),
today.toString(),
today.toString(),
DriversLicense,
BirthCertificate,
today.toString(),
"ACTIVE",
""));
writer.newLine();
}
}
Files.move(temp, csvPathFee, StandardCopyOption.REPLACE_EXISTING);
return !found;
}
public static void writeSavingsCSV(String userID, String SavingsId, double newbalance, boolean isEmployee) throws IOException //make a automatic writing system.
{
Path csvpick = isEmployee ? csvEmployeesavingscsv : csvPath;
Path temp = Files.createTempFile("temp", ".csv");
try (BufferedReader read = Files.newBufferedReader(csvpick); BufferedWriter writetemp = Files.newBufferedWriter(temp)) {
String line;
while ((line = read.readLine()) != null) {
String[] datacur = line.split(",", -1);
if (datacur[0].trim().equals(userID)) {
datacur[2] = String.valueOf(newbalance);
writetemp.write(String.join(",", datacur));
} else {
writetemp.write(line);
}
writetemp.newLine();
}
}
Files.move(temp, csvpick, StandardCopyOption.REPLACE_EXISTING);
}
public static char ReadCustomerinfolastnamechar(int column, String userID) throws IOException {
try (BufferedReader read = Files.newBufferedReader(csvCustomerInfo)) {
String line;
while ((line = read.readLine()) != null) {
String[] datacur = line.split(",", -1);
if (datacur[0].trim().equals(userID)) { //column 0 which is userID
/*for(String Value:datacur){
System.out.println(Value + ", ");
}*/
char ch = datacur[column].charAt(0);
return ch;
}
}
}
return '\0';
}
public static String ReadCustomerinfo(int column, String userID) throws IOException {
try (BufferedReader read = Files.newBufferedReader(csvCustomerInfo)) {
String line;
while ((line = read.readLine()) != null) {
String[] datacur = line.split(",", -1);
if (datacur[0].trim().equals(userID)) { //column 0 which is userID
/*for(String Value:datacur){
System.out.println(Value + ", ");
}*/
if (column < 0 || column > datacur.length) {
return null;
}
return datacur[column].trim();
}
}
}
return null;
}
public static void writeCustomerinfo(double balance, String userID) throws IOException //make a automatic writing system.
{
Path temp = Files.createTempFile("temp", ".csv");
try (BufferedReader read = Files.newBufferedReader(csvCustomerInfo); BufferedWriter writetemp = Files.newBufferedWriter(temp)) {
String line;
while ((line = read.readLine()) != null) {
String[] datacur = line.split(",", -1);
if (datacur[0].trim().equals(userID)) {
for (String Value : datacur) {
System.out.println(Value + ", ");
}
datacur[9] = String.valueOf(balance);
writetemp.write(String.join(",", datacur));
} else {
writetemp.write(line);
}
writetemp.newLine();
}
}
Files.move(temp, csvCustomerInfo, StandardCopyOption.REPLACE_EXISTING);
}
/*unused code public static boolean isValiduserID(String userID){
if(userID == null || userID.isEmpty())
{
return false;
}
for(char c : userID.toCharArray())
{
if(!Character.isDigit(c))
{
return false;
}
}
return true;
}
*/
public static SavingsAccount createSavingsAccount(String userID, double savingsamount) throws IOException {//
if (userIDExists(userID)) {
System.out.println("You already have an account.");
return null;
}
if (birthCertificate(userID)) {
System.out.println("Birth certificate verified successfully.");
}
if (savingsamount <= 0 || userID == null || userID.isEmpty()) {
System.out.println("Error: Type a proper amount.");
return null;
}
boolean isEmployee_t = isEmployee(userID);
String SavingsID = RandomIDGenerator(isEmployee_t);
String phoneNum = getPhonenumber(userID);
LocalDate today = LocalDate.now(); //date creation for the account.
String date = today.toString();
String socialSecurity = getSocialSecurity(userID);
String BC = RandomIDBirthCertificate(userID);
String DL = RandomIDGeneratorDriversLicense(userID, isEmployee_t); //DL is drivers license by the way.
SavingsAccount account = null; //added null so nothing bad can happen such as unitialization.
if (savingsamount == minimumbalance) {
account = new SavingsAccount();
account.userID = userID;
account.setSavings(savingsamount);
account.setSavingsID(SavingsID);
account.isEmployee = isEmployee_t;
account.birthcertificate = BC;
account.driversLicense = DL;
account.hasSavings = true;
} else if (savingsamount <= maximumbalance && savingsamount > minimumbalance) {
account = new SavingsAccount(userID, savingsamount);
account.setSavingsID(SavingsID);
account.setSavings(savingsamount);
account.hasSavings = true;
account.isEmployee = isEmployee_t;
account.birthcertificate = BC;
account.driversLicense = DL;
} else {
System.out.println("The Savings amount has to be in the range of 100-300");
return null;
}
Path currentCSV = isEmployee(userID) ? csvEmployeesavingscsv : csvPath;
try (BufferedWriter bw = Files.newBufferedWriter(currentCSV, StandardOpenOption.APPEND)) {
bw.write(userID + "," + SavingsID + "," + account.savingsbalance);
bw.newLine(); // make a new line when written.
}
writeCustomerinfo(savingsamount, account.getuserID());
writefeeUser(account.SavingsID, account.driversLicense, account.birthcertificate);
return account;
}
public static SavingsAccount OpenSavingsAccount(String userID) throws IOException {
if (userIDExists(userID)) {
Path isEmployee = (isEmployee(userID)) ? csvEmployeesavingscsv : csvPath;
try (BufferedReader readlines = Files.newBufferedReader(isEmployee)) {
readlines.readLine();
String currentline;
while ((currentline = readlines.readLine()) != null) {
String[] currentdata = currentline.split(",", -1);
if (currentdata[0].trim().equals(userID)) {
SavingsAccount account = new SavingsAccount();
account.userID = userID;
account.savingsbalance = Double.parseDouble(currentdata[2]);
account.SavingsID = currentdata[1];
account.isEmployee = isEmployee(userID);
account.update();
return account;
}
}
}
System.out.println("You're logged in");
return null;
} else {
System.out.println("Account doesn't exist, create it.");
}
return null;
}
public SavingsAccount closeSavings() throws IOException {
if (!userIDExists(userID)) {
System.out.println("Account doesn't exist.");
return null;
}
String result = updateNegativeBalance();
if (result.equals("CLOSED")) {
removeSavings(this.userID, this.SavingsID, this.isEmployee);
System.out.println("Account already closed due to debt violation.");
return null;
}
if (result.equals("NEGATIVE")) {
System.out.println("Cannot close: outstanding debt not cleared.");
return this;
}
if (result.equals("ACTIVE")) {
double fee = getEarlyClosureFee();
// NO FEE CASE → auto close
if (fee == 0) {
removeSavings(this.userID, this.SavingsID, this.isEmployee);
System.out.println("Account closed successfully (no fee).");
return null;
}
// FEE CASE → ask user
System.out.println("Early closure fee: $" + fee);
System.out.println("Pay fee to close account? (yes/no)");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.equalsIgnoreCase("no")) {
return this;
}
if (!input.equalsIgnoreCase("yes")) {
System.out.println("Invalid input.");
return this;
}
if (!earlyClosureMoneyMarketAccount()) {
return this;
}
removeSavings(this.userID, this.SavingsID, this.isEmployee);
System.out.println("Account closed successfully.");
return null;
}
return this;
}
public double getEarlyClosureFee() throws IOException {
LocalDate today = LocalDate.now();
long daysOpen = ChronoUnit.DAYS.between(getDateCreated(), today);
return earlyClosureCalculator(daysOpen);
}
public boolean earlyClosureMoneyMarketAccount() throws IOException {
Scanner scanner = new Scanner(System.in);
LocalDate today = LocalDate.now();
long daysOpen = ChronoUnit.DAYS.between(getDateCreated(), today);
try {
double fee = earlyClosureCalculator(daysOpen);
double balance = getSavings();
if (balance < fee) {
System.out.println("Insufficient funds to cover early closure fee.");
return false;
}
setSavings(balance - fee);
System.out.printf("Early closure fee applied: $%.2f%n", fee);
System.out.printf("Remaining balance: $%.2f%n", getSavings());
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
return true;
}
private static double earlyClosureCalculator(long days) {
if (days < 0) {
throw new IllegalArgumentException("Days cannot be negative");
}
double fee;
if (days <= MAX_DAY / 2) {
fee = 50.0;
} else if (days <= MAX_DAY) {
fee = 20.0;
} else {
fee = 0.0;
}
if (fee > 0) {
fee = Math.max(MIN_FEE, Math.min(fee, MAX_FEE_LIMIT));
}
return fee;
}
public static boolean removeSavings(String userID, String moneyID, boolean isEmployee) throws IOException { //DO NOT use this this is used for closed money as a helper
boolean success = false;
boolean successfee = false;
Path temp = Files.createTempFile("temp", ".csv");
Path csvPick = isEmployee ? csvEmployeesavingscsv: csvPath;
try (BufferedReader reader = Files.newBufferedReader(csvPick); BufferedWriter writer = Files.newBufferedWriter(temp)) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",", -1);
if (data[0].equals(userID) && data[1].equals(moneyID)) {
success = true;
continue;
}
writer.write(line);
writer.newLine();
}
}
Files.move(temp, csvPick, StandardCopyOption.REPLACE_EXISTING);
Path tempFee = Files.createTempFile("tempfee", ".csv");
try (BufferedReader reader = Files.newBufferedReader(csvPathFee); BufferedWriter writer = Files.newBufferedWriter(tempFee)) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",", -1);
if (data[0].equals(moneyID)) {
successfee = true;
continue;
}
writer.write(line);
writer.newLine();
}
}
Files.move(tempFee, csvPathFee, StandardCopyOption.REPLACE_EXISTING);
return success;
}
public static boolean birthCertificate(String userID) throws IOException {
int COL_FIRSTNAME = 1;
int COL_LASTNAME = 2;
int COL_DOB = 4;
String firstNameFromCSV = ReadCustomerinfo(COL_FIRSTNAME, userID);
String lastNameFromCSV = ReadCustomerinfo(COL_LASTNAME, userID);
String DOBfromcsv = ReadCustomerinfo(COL_DOB, userID);
String firstName;
String lastName;
Scanner scanner = new Scanner(System.in);
//FIRST NAME
while (true) {
System.out.print("Enter first name: ");
firstName = scanner.nextLine();
if (firstName.equalsIgnoreCase("exit")) {
System.out.println("Exiting birth certificate verification.");
return false;
}
if (firstNameFromCSV != null
&& firstName.equalsIgnoreCase(firstNameFromCSV)) {
break;
}
System.out.println("This is not your first name. Please try again.");
}
//LAST NAME
while (true) {
System.out.print("Enter last name: ");
lastName = scanner.nextLine();
if (lastName.equalsIgnoreCase("exit")) {
System.out.println("Exiting birth certificate verification.");
return false;
}
if (lastNameFromCSV != null
&& lastName.equalsIgnoreCase(lastNameFromCSV)) {
break;
}
System.out.println("Invalid last name. Please try again.");
}
//DOB FROM CSV
LocalDate expectedDob = null;
if (DOBfromcsv != null && !DOBfromcsv.isEmpty()) {
DateTimeFormatter csvFormatter
= DateTimeFormatter.ofPattern("[M/d/yyyy][d/M/yyyy][yyyy-MM-dd]");
expectedDob = LocalDate.parse(DOBfromcsv, csvFormatter);
}
//INPUT DOB
while (true) {
System.out.print("Enter date of birth (yyyy-MM-dd, dd-MM-yyyy, MM/dd/yyyy) or type 'exit': ");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("exit")) {
System.out.println("Exiting birth certificate verification.");
return false;
}
LocalDate dob = null;
DateTimeFormatter[] formats = new DateTimeFormatter[]{
DateTimeFormatter.ofPattern("yyyy-MM-dd"),
DateTimeFormatter.ofPattern("dd-MM-yyyy"),
DateTimeFormatter.ofPattern("MM/dd/yyyy")
};
for (DateTimeFormatter f : formats) {
try {
dob = LocalDate.parse(input, f);
break;
} catch (Exception ignored) {
}
}
if (dob == null) {
System.out.println("Invalid date format. Try again.");
continue;
}
if (dob.isAfter(LocalDate.now())) {
System.out.println("Date cannot be in the future.");
continue;
}
if (expectedDob != null && !dob.equals(expectedDob)) {
System.out.println("Date of birth does not match our records. Please try again.");
continue;
}
break;
}
return true;
}
public static String getSocialSecurity(String userID) throws IOException { // Grab phone number by searching through CSV
try (BufferedReader readfile = Files.newBufferedReader(csvCustomerInfo)) {
String line;
while ((line = readfile.readLine()) != null) {
boolean valid = true;
String[] dataline = line.split(",", -1);
if (dataline.length > 0 && dataline[0].trim().equals(userID)) {
String socialSecurity = dataline.length > 3 ? dataline[3] : ""; //social security is in the fourth column.
socialSecurity = socialSecurity.replaceAll("[^0-9]", "");
if (socialSecurity.length() == 9) { //it has to be equal to 9 otherwise return a null to interrupt account creation
for (int i = 0; i < socialSecurity.length(); i++) { //just in case replaceall missed something.
if (!Character.isDigit(socialSecurity.charAt(i))) { //this is copied from phone number code.
valid = false;
break;//break out of here and return a null few lines down if there is a character inside a socialsecurity number
}
}
if (valid) { //valid helps us here by checking if characters are in the digits or not
return socialSecurity;
}
}
}
}
}
return null;
}
public static String getPhonenumber(String userID) throws IOException { // Grab phone number by searching through CSV
try (BufferedReader readfile = Files.newBufferedReader(csvCustomerInfo)) {
String line;
while ((line = readfile.readLine()) != null) {
boolean valid = true;
String[] dataline = line.split(",", -1);
if (dataline.length > 0 && dataline[0].trim().equals(userID)) {
String phonenum = dataline.length > 6 ? dataline[6] : "";
phonenum = phonenum.replaceAll("[^0-9]", "");
if (phonenum.length() == 10) {
for (int i = 0; i < phonenum.length(); i++) {
if (!Character.isDigit(phonenum.charAt(i))) {
valid = false;
break;
}
}
if (valid) {
return phonenum;
}
}
}
}
}
return null;
}
public static String RandomIDGeneratorDriversLicense(String userID, boolean isEmployee) throws IOException {
Random rand = new Random();
char firstchar = ReadCustomerinfolastnamechar(2, userID);
String DriversID;
do {
if (firstchar == '\0') {
return null;
}
firstchar = Character.toUpperCase(firstchar);
StringBuilder string = new StringBuilder();
string.append(firstchar);
for (int i = 0; i < 14; i++) {
string.append(rand.nextInt(10));
}
DriversID = string.toString();
} while (DriversLicenseExists(DriversID, isEmployee));
return DriversID;
}
public static String RandomIDBirthCertificate(String userID) throws IOException {
Random rand = new Random();
boolean isEmployee_t = isEmployee(userID);
String BC = "BC-";
String birthCertificate;
do {
StringBuilder string = new StringBuilder();
string.append(BC);
for (int i = 0; i < 14; i++) {
string.append(rand.nextInt(10));
}
birthCertificate = string.toString();
} while (birthCertificateExists(birthCertificate, isEmployee_t));
return birthCertificate;
}
public static String RandomIDGenerator(boolean isEmployee) throws IOException // make the randomIDGenerator a static so it doesn't belong to an object but an standard ID generator for savings ids
{
Random rand = new Random(); // rand can generate random numbers
String ID; // make ID String so we can easily manipulate it in CSV like reading or writing
// it.
do {
long number = (MIN) + (long) (rand.nextDouble() * (MAX - MIN + 1)); //the random generator that is in the range of min-max
ID = String.valueOf(number); // convert number to String so ID can equal to that string.
} while (savingsIDExists(ID, isEmployee)); // Check if there is any Savings ID like it in the CSV file.
return ID;
}
public static boolean savingsIDExists(String SavingsID, boolean isEmployee) throws IOException { //SavingsID is used in the random generator so it wouldn't generate the same Savings ID as another persons Savings ID.
Path isEmployee_t = isEmployee ? csvEmployeesavingscsv : csvPath;
try (BufferedReader reader = Files.newBufferedReader(isEmployee_t)) {
reader.readLine(); // this line skips the header for example (userID,SavingsID,savings)
String line;
while ((line = reader.readLine()) != null) { // as line doesn't equal to NULL (end of file) continue.
String[] currentdata_to_col = line.split(",", -1);
if (currentdata_to_col.length > 1 && currentdata_to_col[1].equals(SavingsID)) {
return true;
}
}
}
return false; // return false if there is no savings ID equal to another savings ID
}
public static boolean birthCertificateExists(String BCID, boolean isEmployee) throws IOException { //Generate a unique birth certificate ID. No same ID distributed.
Path Csvchoice;
if (isEmployee) {
Csvchoice = csvEmployeesavingscsv;
} else {
Csvchoice = csvPath;
}
try (BufferedReader reader = Files.newBufferedReader(Csvchoice)) {
reader.readLine(); // this line skips the header for example (userID,MoneyID,MoneyMarket,DriversLicense)
String line;
while ((line = reader.readLine()) != null) { // as line doesn't equal to NULL (end of file) continue.
String[] currentdata_to_col = line.split(",", -1);
if (currentdata_to_col.length > 5 && currentdata_to_col[5].equals(BCID)) {
return true;
}
}
}
return false; // return false if there is no MoneyMarket ID equal to another MoneyMarket ID
}
public static boolean DriversLicenseExists(String DriversID, boolean isEmployee) throws IOException { //Generate a unique drivers ID. No same ID distributed.
Path choiceCSV;
if (isEmployee) {
choiceCSV = csvEmployeesavingscsv;
} else {
choiceCSV = csvPath;
}
try (BufferedReader reader = Files.newBufferedReader(choiceCSV)) {
reader.readLine(); // this line skips the header for example (userID,MoneyID,MoneyMarket,DriversLicense)
String line;
while ((line = reader.readLine()) != null) { // as line doesn't equal to NULL (end of file) continue.
String[] currentdata_to_col = line.split(",", -1);
if (currentdata_to_col.length > 3 && currentdata_to_col[3].equals(DriversID)) {
return true;
}
}
}
return false; // return false if there is no MoneyMarket ID equal to another MoneyMarket ID
}
//START Both getSavings() and setSavings() are used for debugging.
public double getSavings() {
return savingsbalance;
}
public void setSavings(double savings) {//field to the parameter savings.
savingsbalance = savings;
}
//END
public void setSavingsID(String SavingsID) {
this.SavingsID = SavingsID;
}
public double depositSavings(double depositamt) throws IOException {
if (depositamt > 0) {
writeSavingsCSV(userID, SavingsID, savingsbalance, isEmployee);
logTransaction("DEPOSIT", depositamt);
return savingsbalance += depositamt;
} else {
System.out.println("Deposit has to be a positive.");
}
return savingsbalance;
}
public String getuserID() { //testing to see userID string
return userID;
}
public String getSavingsID() {
return SavingsID;
}
public LocalDate getDateCreated() throws IOException {
try(BufferedReader read = Files.newBufferedReader(csvPathFee))
{
LocalDate today = LocalDate.now();
String line;
while((line = read.readLine()) != null){
String[] data = line.split(",", -1);
if(data[0].trim().equals(SavingsID)){
datecreation = readSafeLocalDate(data, 7, today);
return datecreation;
}
}
}
return LocalDate.now();
}
//******************************* */
// ************Withdraw system**************/
public boolean withdrawSavings(double amt) throws IOException // withdraw system that records the amount. //right now this isn't use in the code at all.
{
if (amt <= 0) {
System.out.println("Account can't be less than or equal to 0, so please choose a higher value.");
return false;
}
if (amt > savingsbalance) {
System.out.println("The amount for withdraw is too high");
return false; // deny negative withdraw
} else {
savingsbalance -= amt;
System.out.println("Savings has been successfully withdrew.");
writeSavingsCSV(userID, SavingsID, savingsbalance, isEmployee);
logTransaction("WITHDRAW", amt);
return true;
}
}
public double transfer(CheckingAccount.Account fromAccount, Scanner scanner, boolean fromsource, double externalValue) throws IOException {
while (true) {
System.out.print(" Amount to transfer: $");
if (!scanner.hasNextDouble()) {
System.out.println("Enter a proper number.");
scanner.next();
continue;
}
double amount = scanner.nextDouble();
scanner.nextLine();
if (amount <= 0) {
System.out.println("No negative amounts or 0.");
continue;
}
if (fromsource && fromAccount != null) {
if (!fromAccount.isActive) {
System.out.println("Checking Account is inactive.");
return externalValue;
}
if (amount > fromAccount.balance) {
System.out.println("Insufficient funds in checking");
continue;
}
fromAccount.balance -= amount;
savingsbalance += amount;
writeSavingsCSV(userID, SavingsID, savingsbalance, isEmployee);
logTransaction("TRANSFER IN", amount);
fromAccount.addTransaction("Transfer out for Savings", amount);
fromAccount.updateFlags();
System.out.printf(" Moved $%.2f: savings ($%.2f) <- checking %s ($%.2f)%n",
amount, getSavings(), fromAccount.accountID, fromAccount.balance);
} else if (fromsource && fromAccount == null) {
if (amount > externalValue) {
System.out.println("Insufficient external funds.");
continue;
}
externalValue -= amount;
savingsbalance += amount;
logTransaction("TRANSFER IN", amount);
writeSavingsCSV(userID, SavingsID, savingsbalance, isEmployee);
System.out.printf(" Moved $%.2f: savings ($%.2f) → Source: ($%.2f)%n",
amount, savingsbalance, externalValue);
} else if (fromAccount != null) {
if (!fromAccount.isActive) {
System.out.println("Account is inactive.");
return externalValue;
}
if (amount > savingsbalance) {
System.out.println("Insufficient funds.");
continue;
}
savingsbalance -= amount;
fromAccount.balance += amount;
writeSavingsCSV(userID, SavingsID, savingsbalance, isEmployee);
fromAccount.addTransaction("Transfer in from Savings", amount);
logTransaction("TRANSFER OUT", amount);
fromAccount.updateFlags();
System.out.printf(" Moved $%.2f: savings ($%.2f) -> checking %s ($%.2f)%n",
amount, getSavings(), fromAccount.accountID, fromAccount.balance);
} //from SAVINGS to External value.
else {
if (amount > savingsbalance) {
System.out.println("Insufficient funds.");
continue;
}
savingsbalance -= amount;
externalValue += amount;
logTransaction("TRANSFER OUT", amount);
System.out.printf(" Moved $%.2f: savings ($%.2f) <- Source: ($%.2f)%n",
amount, savingsbalance, externalValue);
}
break;
}
return externalValue;
}
public void update() throws IOException {