-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankSystem.java
More file actions
418 lines (364 loc) · 15.9 KB
/
Copy pathBankSystem.java
File metadata and controls
418 lines (364 loc) · 15.9 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
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class BankSystem {
// user class to store common fields like username and password
public static class User {
protected String username;
protected String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
// account class extends user and adds fields like balance, transaction history, etc.
public static class account extends User {
private double balance;
private ArrayList<String> transactionHistory; // stores transaction logs
private int failedLoginAttempts; // tracks failed login attempts
private boolean isLocked; // indicates if the account is locked
public account(String username, String password) {
super(username, password); // calls the constructor of the user class
this.balance = 0.0;
this.transactionHistory = new ArrayList<>();
this.failedLoginAttempts = 0;
this.isLocked = false;
}
public double gatBalance() {
return balance;
}
public boolean isLocked() {
return isLocked;
}
public void incrementFailedLoginAttempts() {
failedLoginAttempts++;
if (failedLoginAttempts >= 3) { // locks the account after 3 failed attempts
isLocked = true;
}
}
public void resetFailedLoginAttempts() {
failedLoginAttempts = 0; // resets failed login attempts
}
public void unlockAccount() {
isLocked = false; // unlocks the account
resetFailedLoginAttempts();
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
transactionHistory.add("Deposited: $" + amount); // logs the deposit
}
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
transactionHistory.add("Withdrew: $" + amount); // logs the withdrawal
return true;
}
return false;
}
public void addTransfer(String message) {
transactionHistory.add(message); // logs a transfer
}
public void viewTransactionHistory() {
if (transactionHistory.isEmpty()) {
System.out.println("No transactions found.");
} else {
System.out.println("Transaction History:");
for (String transaction : transactionHistory) {
System.out.println(transaction);
}
}
}
@Override
public String toString() {
return "Username: " + username + ", Balance: $" + balance;
}
}
static ArrayList<account> accounts = new ArrayList<>(); // stores all accounts
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
while (true) {
displayMainMenu(); // shows the main menu
try {
System.out.print("Choice: ");
int choice = scnr.nextInt();
if (choice == 1) {
handleLogin(scnr); // handles user login
} else if (choice == 2) {
handleAccountCreation(scnr); // handles account creation
} else if (choice == 3) {
handleAdminLogin(scnr); // handles admin login
} else if (choice == 4) {
System.out.println("\nThank you for using our bank system");
break; // exits the program
} else {
System.out.println("\nPLEASE ENTER ONE OF THE CHOICES!!! (1,2,3,4)\n");
}
} catch (InputMismatchException e) {
System.out.println("\nPLEASE ENTER ONE OF THE CHOICES!!!\n");
scnr.next(); // clears invalid input
}
}
}
public static void displayMainMenu() {
// displays the main menu options
System.out.println("WELCOME TO YOUR BANK SYSTEM");
System.out.println();
System.out.println("1. Login");
System.out.println("2. Create an Account");
System.out.println("3. Admin Login");
System.out.println("4. Exit");
}
public static void handleLogin(Scanner scnr) {
// handles user login
System.out.println("Login: ");
System.out.print("Enter your username: ");
String usernameLogin = scnr.next();
System.out.print("Enter your password: ");
String passwordLogin = scnr.next();
account loggedIn = findAccountByUsername(usernameLogin);
if (loggedIn == null) {
System.out.println("Invalid username! Returning to main menu.");
return;
}
if (loggedIn.isLocked()) {
System.out.println("This account is locked due to too many failed login attempts. Please contact an admin to unlock it.");
return;
}
if (!loggedIn.getPassword().equals(passwordLogin)) {
loggedIn.incrementFailedLoginAttempts();
int attemptsLeft = 3 - loggedIn.failedLoginAttempts; // calculates remaining attempts
if (loggedIn.isLocked()) {
System.out.println("Your account has been locked due to too many failed login attempts.");
} else {
System.out.println("Invalid password! You have " + attemptsLeft + " attempt(s) left.");
}
return;
}
loggedIn.resetFailedLoginAttempts(); // resets failed attempts on successful login
System.out.println("Login successful! Welcome, " + loggedIn.getUsername());
handleAccountMenu(scnr, loggedIn); // opens the account menu
}
public static void handleAccountCreation(Scanner scnr) {
// handles account creation
while (true) {
System.out.println("Enter new Username: ");
String username = scnr.next();
if (findAccountByUsername(username) != null) {
System.out.println("Username already exists. Please choose a different username.");
continue;
}
System.out.println("Enter new Password: ");
System.out.println("Password must be at least 10 characters long, have a number, have a letter, and have a special character (.!@#$%^&*?)");
String password = scnr.next();
if (isPasswordComplex(password)) {
System.out.println("Successfully created password!!");
account acc1 = new account(username, password);
accounts.add(acc1); // adds the new account to the list
break;
} else {
System.out.println("Password is not complicated enough");
}
}
}
public static void handleAccountMenu(Scanner scnr, account loggedIn) {
// handles the account menu for logged-in users
while (true) {
System.out.println("\nAccount Menu:");
System.out.println("1. Check Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Transfer");
System.out.println("5. View Transaction History");
System.out.println("6. Delete Account");
System.out.println("7. Logout");
System.out.print("Choice: ");
int action = scnr.nextInt();
if (action == 1) {
System.out.printf("Balance: $%.2f\n", loggedIn.gatBalance());
} else if (action == 2) {
System.out.print("Enter deposit amount: ");
double amount = scnr.nextDouble();
loggedIn.deposit(amount); // deposits money
System.out.println("Deposit successful!");
} else if (action == 3) {
System.out.print("Enter withdrawal amount: ");
double amount = scnr.nextDouble();
if (loggedIn.withdraw(amount)) { // withdraws money
System.out.println("Withdrawal successful!");
} else {
System.out.println("Insufficient balance.");
}
} else if (action == 4) {
System.out.print("Enter recipient username: ");
String recipientName = scnr.next();
account recipient = findAccountByUsername(recipientName);
if (recipient == null || recipient == loggedIn) {
System.out.println("Invalid recipient.");
continue;
}
System.out.print("Enter amount to transfer: ");
double amount = scnr.nextDouble();
if (loggedIn.withdraw(amount)) { // transfers money
recipient.deposit(amount);
loggedIn.addTransfer("Transferred $" + amount + " to " + recipient.getUsername());
recipient.addTransfer("Received $" + amount + " from " + loggedIn.getUsername());
System.out.println("Transfer successful!");
} else {
System.out.println("Insufficient balance for transfer.");
}
} else if (action == 5) {
loggedIn.viewTransactionHistory(); // views transaction history
} else if (action == 6) {
System.out.print("Are you sure you want to delete your account? Type 'yes' to confirm: ");
String confirmation = scnr.next();
if (confirmation.equalsIgnoreCase("yes")) {
accounts.remove(loggedIn); // deletes the account
System.out.println("Your account has been deleted. Logging out...");
break;
} else {
System.out.println("Account deletion canceled.");
}
} else if (action == 7) {
System.out.println("Logging out...");
break;
} else {
System.out.println("Invalid option. Try again.");
}
}
}
public static boolean isPasswordComplex(String password) {
// checks if the password meets complexity requirements
boolean digit = false;
boolean letter = false;
boolean length = password.length() >= 10;
boolean spChar = false;
String specialChars = ".!@#$%^&*?";
for (int i = 0; i < password.length(); i++) {
char character = password.charAt(i);
if (Character.isDigit(character)) {
digit = true;
}
if (Character.isLetter(character)) {
letter = true;
}
if (specialChars.contains(String.valueOf(character))) {
spChar = true;
}
}
return digit && letter && spChar && length;
}
public static account findAccountByUsername(String username) {
// finds an account by username
for (account acc : accounts) {
if (acc.getUsername().equals(username)) {
return acc;
}
}
return null;
}
public static void handleAdminLogin(Scanner scnr) {
// handles admin login
System.out.println("Admin Login:");
System.out.print("Enter admin username: ");
String adminUsername = scnr.next();
System.out.print("Enter admin password: ");
String adminPassword = scnr.next();
// hardcoded admin credentials
if (adminUsername.equals("admin") && adminPassword.equals("admin123")) {
System.out.println("Admin login successful!");
handleAdminMenu(scnr); // opens the admin menu
} else {
System.out.println("Invalid admin credentials! Returning to main menu.");
}
}
public static void handleAdminMenu(Scanner scnr) {
// handles the admin menu
while (true) {
System.out.println("\nAdmin Menu:");
System.out.println("1. View All Accounts");
System.out.println("2. Unlock an Account");
System.out.println("3. Delete an Account");
System.out.println("4. Display Top Balances");
System.out.println("5. Logout");
System.out.print("Choice: ");
int choice = scnr.nextInt();
if (choice == 1) {
viewAllAccounts(); // views all accounts
} else if (choice == 2) {
unlockAccount(scnr); // unlocks an account
} else if (choice == 3) {
deleteAccount(scnr); // deletes an account
} else if (choice == 4) {
System.out.print("Enter the number of top accounts to display: ");
int n = scnr.nextInt();
displayTopBalances(n); // displays top balances
} else if (choice == 5) {
System.out.println("Logging out of admin mode...");
break;
} else {
System.out.println("Invalid option. Try again.");
}
}
}
public static void viewAllAccounts() {
// displays all accounts
System.out.println("\nAll Accounts:");
if (accounts.isEmpty()) {
System.out.println("No accounts found.");
} else {
for (account acc : accounts) {
System.out.println(acc);
}
}
}
public static void unlockAccount(Scanner scnr) {
// unlocks a locked account
System.out.print("Enter the username of the account to unlock: ");
String username = scnr.next();
account acc = findAccountByUsername(username);
if (acc == null) {
System.out.println("Account not found.");
} else if (!acc.isLocked()) {
System.out.println("Account is not locked.");
} else {
acc.unlockAccount();
System.out.println("Account unlocked successfully.");
}
}
public static void deleteAccount(Scanner scnr) {
// deletes an account
System.out.print("Enter the username of the account to delete: ");
String username = scnr.next();
account acc = findAccountByUsername(username);
if (acc == null) {
System.out.println("Account not found.");
} else {
accounts.remove(acc);
System.out.println("Account deleted successfully.");
}
}
public static void displayTopBalances(int n) {
// displays the top n accounts by balance
if (accounts.isEmpty()) {
System.out.println("No accounts available.");
return;
}
// creates an array of accounts to sort
account[] accountArray = accounts.toArray(new account[0]);
// sorts the array by balance in descending order
java.util.Arrays.sort(accountArray, (a, b) -> Double.compare(b.gatBalance(), a.gatBalance()));
// displays the top n accounts
System.out.println("Top " + n + " Accounts by Balance:");
for (int i = 0; i < Math.min(n, accountArray.length); i++) {
System.out.println((i + 1) + ". " + accountArray[i].getUsername() + " - $" + accountArray[i].gatBalance());
}
}
}