-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.java
611 lines (610 loc) · 33.4 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
import java.util.*;
import java.io.*;
class Admin {
private static final String file_directory = "/Files/";
private static ArrayList < ClientManager > clients = new ArrayList < ClientManager > ();
private String useradmin;
private String passadmin;
public Admin(String useradmin, String passadmin) {
this.useradmin = useradmin;
this.passadmin = passadmin;
}
public String getUseradmin() {
return useradmin;
}
public String getPassadmin() {
return passadmin;
}
public void setPassadmin(String passadmin) {
this.passadmin = passadmin;
}
public void adminlogin() {
Scanner esc = new Scanner(System.in);
String tryagain = "No";
try {
FileWriter writer = new FileWriter("AdminCredentials.txt");
writer.write("Username : " + getUseradmin() + '\n' + "Password : " + getPassadmin());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
do {
System.out.println("\n+===================================+");
System.out.println("|---------ISUFST BANK SYSTEM--------|");
System.out.println("|-----WELCOME TO OUR BANK SYSTEM----|");
System.out.println("+===================================+\n");
System.out.println("+-----------------------------------+");
System.out.println("|----|LOG IN YOUR ADMIN ACCOUNT|----|");
System.out.println("+-----------------------------------+");
System.out.print("Enter your username : ");
String username = esc.nextLine();
System.out.print("Enter your password : ");
String password = esc.nextLine();
if (username.equals(getUseradmin()) && password.equals(getPassadmin())) {
System.out.println("\nLOGGED IN SUCCESSFULLY!");
adminpage();
break;
} else {
System.out.print("\nIncorrect username or password!\nDo you want to try again? (Yes/No) : ");
tryagain = esc.nextLine();
}
} while (tryagain.equalsIgnoreCase("Yes") || tryagain.equalsIgnoreCase("y"));
}
public void adminpage() {
Scanner esc = new Scanner(System.in);
int choice;
try {
do {
System.out.println("\n+===================================+");
System.out.println("|---------ISUFST BANK SYSTEM--------|");
System.out.println("|-----WELCOME TO OUR BANK SYSTEM----|");
System.out.println("+===================================+\n");
System.out.println("+-----------------------------------+");
System.out.println("|---!WELCOME ADMIN|CHOOSE A TASK!---|");
System.out.println("+-----------------------------------+");
System.out.println("|[1] Add Client |");
System.out.println("|[2] List Client |");
System.out.println("|[3] Delete Client |");
System.out.println("|[4] Change Admin Password |");
System.out.println("|[5] Go Back To Previous Menu |");
System.out.println("|[0] Exit System |");
System.out.println("+-----------------------------------+");
System.out.print(">>> ");
choice = esc.nextInt();
switch (choice) {
case 1:
addClients();
break;
case 2:
listClients();
break;
case 3:
deleteClient();
break;
case 4:
changeAdminPassword();
break;
case 5:
return;
case 0:
System.out.println("Thank you for using our system.\nComeback next time!\nExiting System...");
System.exit(0);
break;
}
} while (choice != 0);
} catch (InputMismatchException i) {
System.out.println("\nNOTICE : Input number only!\n");
} catch (Exception e) {
System.out.println("\nAn Error Occured!\n");
}
}
class ClientManager {
private String userClient;
public String getUserClient() {
return userClient;
}
public void setUserClient(String newUserClient) {
this.userClient = newUserClient;
}
private String passClient;
public String getPassClient() {
return passClient;
}
public void setPassClient(String newPassClient) {
this.passClient = newPassClient;
}
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String newLastName) {
this.lastName = newLastName;
}
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String newFirstName) {
this.firstName = newFirstName;
}
private String middleName;
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String newMiddleName) {
this.middleName = newMiddleName;
}
private String address;
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
this.address = newAddress;
}
private long contactNumber;
public long getContactNumber() {
return contactNumber;
}
public void setContactNumber(long newContactNumber) {
this.contactNumber = newContactNumber;
}
private int initialBalance;
public int getInitialBalance() {
return initialBalance;
}
public void setInitialBalance(int newInitialBalance) {
this.initialBalance = newInitialBalance;
}
public ClientManager(String userClient, String passClient, String lastName, String firstName, String middleName, String address, long contactNumber, int initialBalance) {
this.userClient = userClient;
this.passClient = passClient;
this.lastName = lastName;
this.firstName = firstName;
this.middleName = middleName;
this.address = address;
this.contactNumber = contactNumber;
this.initialBalance = initialBalance;
}
public String toString() {
return "Username: " + getUserClient() +
"\nPassword: " + getPassClient() +
"\nLast Name: " + getLastName() +
"\nFirst Name: " + getFirstName() +
"\nMiddle Name: " + getMiddleName() +
"\nAddress: " + getAddress() +
"\nContact Number: " + getContactNumber() +
"\nInitial Balance: " + getInitialBalance();
}
}
public void addClients() {
Scanner esc = new Scanner(System.in);
System.out.println("\n+===================================+");
System.out.println("|---------ISUFST BANK SYSTEM--------|");
System.out.println("|-----WELCOME TO OUR BANK SYSTEM----|");
System.out.println("+===================================+\n");
System.out.println("-------------------------------------");
System.out.println("|----------|ADD A CLIENT!|-----------|");
System.out.println("-------------------------------------");
System.out.print("\nUsername : ");
String username = esc.nextLine();
boolean userExists = false;
for (ClientManager client: clients) {
if (client.getUserClient().equals(username)) {
userExists = true;
break;
}
}
if (userExists) {
System.out.println("\nUser already exists. Unable to add client.\n");
} else {
System.out.print("Password : ");
String password = esc.nextLine();
System.out.print("Last Name : ");
String lastname = esc.nextLine();
System.out.print("First Name : ");
String firstname = esc.nextLine();
System.out.print("Middle Name : ");
String middlename = esc.nextLine();
System.out.print("Address : ");
String address = esc.nextLine();
System.out.print("Contact Number : ");
long contactnumber = esc.nextLong();
System.out.print("Initial Balance : ");
int initialbalance = esc.nextInt();
ClientManager cl = new ClientManager(username, password, lastname, firstname, middlename, address, contactnumber, initialbalance);
clients.add(cl);
System.out.println("\nClients Added Successfully");
try {
FileWriter fw = new FileWriter("/Files/" + username + ".txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Username : " + cl.getUserClient() + '\n');
bw.write("Password : " + cl.getPassClient() + '\n');
bw.write("Last Name : " + cl.getLastName() + '\n');
bw.write("First Name : " + cl.getFirstName() + '\n');
bw.write("Middle Name : " + cl.getMiddleName() + '\n');
bw.write("Address : " + cl.getAddress() + '\n');
bw.write("Contact Number : " + cl.getContactNumber() + '\n');
bw.write("Initial Balance : " + cl.getInitialBalance() + '\n');
bw.newLine();
bw.close();
fw.close();
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage() + "\n");
}
}
}
public void listClients() {
if (clients.size() > 0) {
for (int i = 0; i < clients.size(); i++) {
System.out.println("+-----------------------------------+");
System.out.println(clients.get(i).toString());
System.out.println("+-----------------------------------+");
}
} else {
System.out.println("No clients found!\n");
}
}
public void deleteClient() {
Scanner esc = new Scanner(System.in);
System.out.print("Enter the username you want to delete : ");
String username = esc.nextLine();
boolean clientFound = false;
for (int i = 0; i < clients.size(); i++) {
ClientManager client = clients.get(i);
if (client.getUserClient().equals(username)) {
clients.remove(i);
System.out.println("Client with username " + username + " has been deleted.");
File deleteFile = new File("/Files/" + client.getUserClient() + ".txt");
deleteFile.delete();
clientFound = true;
break;
}
}
if (!clientFound) {
System.out.println("Client with username " + username + " was not found.");
}
}
public void changeAdminPassword() {
Scanner esc = new Scanner(System.in);
System.out.print("Enter your current password: ");
String currPass = esc.nextLine();
if (currPass.equals(getPassadmin())) {
System.out.print("Enter your new password: ");
String newPass = esc.nextLine();
setPassadmin(newPass);
try {
FileWriter writer = new FileWriter("AdminCredentials.txt");
writer.write("Username: " + getUseradmin() + "\nPassword: " + getPassadmin());
writer.close();
System.out.println("Password updated successfully!");
System.out.println("Your password is now change to : " + getPassadmin());
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("\nYour current password is incorrect!\n");
}
}
public void clientLogin() {
Scanner esc = new Scanner(System.in);
String yesNo = "No";
int choice;
do {
System.out.println("\n+===================================+");
System.out.println("|---------ISUFST BANK SYSTEM--------|");
System.out.println("|-----WELCOME TO OUR BANK SYSTEM----|");
System.out.println("+===================================+\n");
System.out.println("-------------------------------------");
System.out.println("|----!LOG IN YOUR CLIENT ACCOUNT!---|");
System.out.println("-------------------------------------");
System.out.print("\nEnter your username: ");
String username = esc.nextLine();
System.out.print("Enter your password: ");
String password = esc.nextLine();
try {
File file = new File(file_directory + username + ".txt");
Scanner fileScanner = new Scanner(file);
String clientUser = fileScanner.nextLine().split(": ")[1];
String clientPass = fileScanner.nextLine().split(": ")[1];
if (username.equals(clientUser) && password.equals(clientPass)) {
System.out.println("\nLOGGED IN SUCCESSFULLY");
while (true) {
System.out.println("\n+===================================+");
System.out.println("|---------ISUFST BANK SYSTEM--------|");
System.out.println("|-----WELCOME TO OUR BANK SYSTEM----|");
System.out.println("+===================================+\n");
System.out.println("+-----------------------------------+");
System.out.println("|--|WELCOME CLIENT|CHOOSE A TASK|---|");
System.out.println("+-----------------------------------+");
System.out.println("|[1] Check Balance |");
System.out.println("|[2] Withdraw |");
System.out.println("|[3] Deposit |");
System.out.println("|[4] Edit Password |");
System.out.println("|[5] View Profile |");
System.out.println("|[6] Edit Profile |");
System.out.println("|[7] Go Back To Previous Menu |");
System.out.println("|[0] Exit System |");
System.out.println("+-----------------------------------+");
System.out.print(">>> ");
choice = esc.nextInt();
esc.nextLine();
switch (choice) {
case 1:
fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
if (line.startsWith("Initial Balance : ")) {
String initialBalance = line.split(": ")[1];
System.out.println("\nHELLO, " + username.toUpperCase());
System.out.println("Your current balance is: " + initialBalance);
break;
}
}
break;
case 2:
System.out.print("\nEnter the amount you want to withdraw: ");
int amount = esc.nextInt();
esc.nextLine();
int minWithdrawal = 100;
fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String ln = fileScanner.nextLine();
if (ln.startsWith("Initial Balance : ")) {
String initialBalance = ln.split(": ")[1];
int intBal = Integer.parseInt(initialBalance);
if (amount < minWithdrawal) {
System.out.println("\nWithdrawal failed! Please withdraw atleast : " + minWithdrawal);
} else {
if (intBal >= amount) {
int newBal = intBal - amount;
System.out.println("\nWithdraw Successful!");
System.out.println("Your new balance is: " + newBal);
try {
File tempFile = new File(file_directory + "temp.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("Initial Balance : ")) {
writer.write("Initial Balance : " + newBal);
} else {
writer.write(line);
}
writer.newLine();
}
reader.close();
writer.close();
file.delete();
tempFile.renameTo(file);
} catch (IOException e) {
System.out.println("An error occurred!\n");
}
} else {
System.out.println("Insufficient Balance!\n");
}
break;
}
}
}
break;
case 3:
System.out.print("\nEnter the amount you want to deposit: ");
int depositAmount = esc.nextInt();
esc.nextLine();
int minDeposit = 100;
fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
if (line.startsWith("Initial Balance : ")) {
String initialBalance = line.split(" : ")[1];
int intBal = Integer.parseInt(initialBalance);
if (depositAmount > minDeposit) {
int newBal = intBal + depositAmount;
System.out.println("\nDeposit Successful!");
System.out.println("Your new balance is: " + newBal);
try {
File tempFile = new File(file_directory + "temp.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String tempLine;
while ((tempLine = reader.readLine()) != null) {
if (tempLine.startsWith("Initial Balance : ")) {
writer.write("Initial Balance : " + newBal);
} else {
writer.write(tempLine);
}
writer.newLine();
}
reader.close();
writer.close();
file.delete();
tempFile.renameTo(file);
} catch (IOException e) {
System.out.println("An error occurred!\n");
}
} else {
System.out.println("\nPlease deposit at least : " + minDeposit + "\n");
break;
}
break;
}
}
break;
case 4:
System.out.print("\nEnter your new password: ");
String newClientPass = esc.nextLine();
try {
File tempFile = new File("/Files/temp.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("Password : ")) {
writer.write("Password : " + newClientPass);
} else {
writer.write(line);
}
writer.newLine();
}
reader.close();
writer.close();
file.delete();
tempFile.renameTo(file);
System.out.println("\nPassword updated successfully!");
System.out.println("Your new password is : " + newClientPass);
} catch (IOException e) {
System.out.println("An error occurred!\n");
}
break;
case 5:
fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
if (line.startsWith("Last Name") || line.startsWith("First Name") ||
line.startsWith("Middle Name") || line.startsWith("Address") ||
line.startsWith("Contact Number")) {
System.out.println("+-----------------------------------+");
System.out.println(line);
System.out.println("+-----------------------------------+");
}
}
break;
case 6:
try {
File tempFile = new File(file_directory + "temp.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String tempLine;
while ((tempLine = reader.readLine()) != null) {
if (tempLine.startsWith("Last Name : ")) {
System.out.println("\nCurrent last name: " + tempLine.split(": ")[1]);
System.out.print("Do you want to edit the last name? (Yes/No): ");
String editChoice = esc.nextLine();
if (editChoice.equalsIgnoreCase("Yes") || editChoice.equalsIgnoreCase("Y")) {
System.out.print("Enter your new last name: ");
String lastName = esc.nextLine();
writer.write("Last Name : " + lastName);
} else {
writer.write(tempLine);
}
} else if (tempLine.startsWith("First Name : ")) {
System.out.println("\nCurrent first name: " + tempLine.split(": ")[1]);
System.out.print("Do you want to edit the first name? (Yes/No): ");
String editChoice = esc.nextLine();
if (editChoice.equalsIgnoreCase("Yes") || editChoice.equalsIgnoreCase("Y")) {
System.out.print("Enter your new first name: ");
String firstName = esc.nextLine();
writer.write("First Name : " + firstName);
} else {
writer.write(tempLine);
}
} else if (tempLine.startsWith("Middle Name : ")) {
System.out.println("\nCurrent middle name: " + tempLine.split(": ")[1]);
System.out.print("Do you want to edit the middle name? (Yes/No): ");
String editChoice = esc.nextLine();
if (editChoice.equalsIgnoreCase("Yes") || editChoice.equalsIgnoreCase("Y")) {
System.out.print("Enter your new middle name: ");
String middleName = esc.nextLine();
writer.write("Middle Name : " + middleName);
} else {
writer.write(tempLine);
}
} else if (tempLine.startsWith("Address : ")) {
System.out.println("\nCurrent address: " + tempLine.split(": ")[1]);
System.out.print("Do you want to edit the address? (Yes/No): ");
String editChoice = esc.nextLine();
if (editChoice.equalsIgnoreCase("Yes") || editChoice.equalsIgnoreCase("Y")) {
System.out.print("Enter your new address: ");
String newAddress = esc.nextLine();
writer.write("Address : " + newAddress);
} else {
writer.write(tempLine);
}
} else if (tempLine.startsWith("Contact Number : ")) {
System.out.println("\nCurrent contact number: " + tempLine.split(": ")[1]);
System.out.print("Do you want to edit the contact number? (Yes/No): ");
String editChoice = esc.nextLine();
if (editChoice.equalsIgnoreCase("Yes") || editChoice.equalsIgnoreCase("Y")) {
System.out.print("Enter your new contact number: ");
String newContactNumber = esc.nextLine();
writer.write("Contact Number : " + newContactNumber);
} else {
writer.write(tempLine);
}
} else {
writer.write(tempLine);
}
writer.newLine();
}
reader.close();
writer.close();
file.delete();
tempFile.renameTo(file);
System.out.println("\nProfile updated successfully!");
} catch (IOException e) {
System.out.println("An error occurred!\n");
}
break;
case 7:
return;
case 0:
System.out.println("\nThank you for using our system.\nCome back next time!\nExiting System...");
System.exit(0);
break;
default:
System.out.println("\nSelect from the list only!\n");
break;
}
}
} else {
System.out.print("\nInvalid password!\nDo you want to try again? (Yes/No): ");
yesNo = esc.nextLine();
}
fileScanner.close();
} catch (FileNotFoundException e) {
System.out.println("\nInvalid username or You don't have an account yet.\nPlease contact the administrator!\n");
}
} while (yesNo.equalsIgnoreCase("Yes") || yesNo.equalsIgnoreCase("Y"));
}
}
public class Main {
private static Admin admin;
public static void main(String[] args) {
Scanner esc = new Scanner(System.in);
int choice;
try {
do {
System.out.println("\n+===================================+");
System.out.println("|---------ISUFST BANK SYSTEM--------|");
System.out.println("|-----WELCOME TO OUR BANK SYSTEM----|");
System.out.println("+===================================+\n");
System.out.println("+-----------------------------------+");
System.out.println("|----------!CHOOSE A TASK!----------|");
System.out.println("+-----------------------------------+");
System.out.println("|[1] Admin |");
System.out.println("|[2] Client |");
System.out.println("|[0] Exit Sytem |");
System.out.println("+-----------------------------------+");
System.out.print(">>> ");
choice = esc.nextInt();
switch (choice) {
case 1:
admin.adminlogin();
break;
case 2:
admin.clientLogin();
break;
case 0:
System.out.println("Thank you for using our system.\nComeback next time!\nExiting System...");
System.exit(0);
break;
}
} while (choice != 0);
} catch (InputMismatchException i) {
System.out.println("\nNOTICE : Input number only!\n");
main(args);
} catch (Exception e) {
System.out.println("\nAn Error Occured!\n");
}
}
}