-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
276 lines (249 loc) · 10.6 KB
/
Main.java
File metadata and controls
276 lines (249 loc) · 10.6 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
import java.util.Scanner;
public class Main {
private static String adminUsername = "admin";
private static String adminPassword = "admin3103";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("=== Banking System ===");
System.out.println("1. Modern GUI (Swing)");
System.out.println("2. Command Line Interface");
System.out.print("Enter choice: ");
int UIChoice = scanner.nextInt();
if (UIChoice == 1) {
javax.swing.SwingUtilities.invokeLater(() -> new ModernBankingSystemGUI());
}
else if (UIChoice == 2) {
runCLI(scanner);
} else {
System.out.println("Invalid option selected!");
System.out.println("Exiting...");
}
if (UIChoice == 3) {
scanner.close();
}
}
private static void runCLI(Scanner scanner) {
Bank bank = new Bank();
boolean exit = false;
while (!exit) {
System.out.println("\n===== Banking System Menu =====");
System.out.println("1. Login as Admin");
System.out.println("2. Create Account");
System.out.println("3. Deposit");
System.out.println("4. Withdraw");
System.out.println("5. Check Balance");
System.out.println("6. Transfer Funds");
System.out.println("7. Apply for Loan");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
if (adminLogin(scanner)) {
boolean adminExit = false;
while (!adminExit) {
System.out.println("\n===== Admin Menu =====");
System.out.println("1. Approve Loan");
System.out.println("2. Reject Loan");
System.out.println("3. List All Loans");
System.out.println("4. List All Accounts");
System.out.println("5. Close Account");
System.out.println("6. Generate Report");
System.out.println("7. Logout");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
int adminChoice = scanner.nextInt();
switch (adminChoice) {
case 1:
approveLoan(bank, scanner);
break;
case 2:
rejectLoan(bank, scanner);
break;
case 3:
bank.listAllLoans();
break;
case 4:
bank.listAllAccounts();
break;
case 5:
closeAccount(bank, scanner);
break;
case 6:
bank.generateReport();
break;
case 7:
adminExit = true;
break;
case 8:
adminExit = true;
exit = true;
System.out.println("Thank you for using the banking system!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
break;
case 2:
createAccount(bank, scanner);
break;
case 3:
deposit(bank, scanner);
break;
case 4:
withdraw(bank, scanner);
break;
case 5:
checkBalance(bank, scanner);
break;
case 6:
transferFunds(bank, scanner);
break;
case 7:
applyForLoan(bank, scanner);
break;
case 8:
exit = true;
System.out.println("Thank you for using the banking system!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static boolean adminLogin(Scanner scanner) {
System.out.print("Enter admin username: ");
String username = scanner.next();
System.out.print("Enter admin password: ");
String password = scanner.next();
if (username.equals(adminUsername) && password.equals(adminPassword)) {
System.out.println("Login successful!");
return true;
} else {
System.out.println("Invalid username or password.");
return false;
}
}
private static void createAccount(Bank bank, Scanner scanner) {
System.out.print("Enter account number: ");
int accNum = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter account type: ");
String accType = scanner.nextLine();
System.out.print("Enter customer name: ");
String name = scanner.nextLine();
System.out.print("Enter customer email: ");
String email = scanner.nextLine();
System.out.print("Enter customer phone: ");
String phone = scanner.nextLine();
Customer newCustomer = new Customer(accNum, name, email, phone);
bank.addCustomer(newCustomer);
BankAccount newAccount = new BankAccount(accNum, accType, newCustomer);
bank.addAccount(newAccount);
newAccount.createAccount();
}
private static void deposit(Bank bank, Scanner scanner) {
System.out.print("Enter account number: ");
int depositAcc = scanner.nextInt();
BankAccount depAccount = bank.findAccount(depositAcc);
if (depAccount != null) {
System.out.print("Enter amount to deposit: ");
double depAmount = scanner.nextDouble();
if (depAmount > 0) {
depAccount.deposit(depAmount);
} else {
System.out.println("Deposit amount must be positive.");
}
}
}
private static void withdraw(Bank bank, Scanner scanner) {
System.out.print("Enter account number: ");
int withdrawAcc = scanner.nextInt();
BankAccount withAccount = bank.findAccount(withdrawAcc);
if (withAccount != null) {
System.out.println("Current balance is: " + withAccount.checkBalance());
System.out.print("Enter amount to withdraw: ");
double withAmount = scanner.nextDouble();
if (withAmount > 0) {
withAccount.withdraw(withAmount);
System.out.println("Remaining balance is: " + withAccount.checkBalance());
} else {
System.out.println("Withdrawal amount must be positive.");
}
}
}
private static void checkBalance(Bank bank, Scanner scanner) {
System.out.print("Enter account number: ");
int balanceAcc = scanner.nextInt();
BankAccount balAccount = bank.findAccount(balanceAcc);
if (balAccount != null) {
System.out.println("Balance: " + balAccount.checkBalance());
}
}
private static void transferFunds(Bank bank, Scanner scanner) {
System.out.print("Enter source account number: ");
int sourceAcc = scanner.nextInt();
System.out.print("Enter target account number: ");
int targetAcc = scanner.nextInt();
System.out.print("Enter amount to transfer: ");
double transferAmount = scanner.nextDouble();
BankAccount sourceAccount = bank.findAccount(sourceAcc);
BankAccount targetAccount = bank.findAccount(targetAcc);
if (sourceAccount != null && targetAccount != null) {
if (transferAmount > 0) {
sourceAccount.transferFunds(targetAccount, transferAmount);
} else {
System.out.println("Transfer amount must be positive.");
}
}
}
private static void closeAccount(Bank bank, Scanner scanner) {
System.out.print("Enter account number to close: ");
int closeAcc = scanner.nextInt();
BankAccount closeAccount = bank.findAccount(closeAcc);
if (closeAccount != null) {
closeAccount.closeAccount();
bank.removeAccount(closeAcc);
}
}
private static void approveLoan(Bank bank, Scanner scanner) {
System.out.print("Enter loan ID to approve: ");
int loanId = scanner.nextInt();
Loan loan = bank.findLoan(loanId);
if (loan != null) {
bank.approveLoan(loan);
} else {
System.out.println("Loan not found!");
}
}
private static void rejectLoan(Bank bank, Scanner scanner) {
System.out.print("Enter loan ID to reject: ");
int loanId = scanner.nextInt();
Loan loan = bank.findLoan(loanId);
if (loan != null) {
bank.rejectLoan(loan);
} else {
System.out.println("Loan not found!");
}
}
private static void applyForLoan(Bank bank, Scanner scanner) {
System.out.print("Enter your account number: ");
int accountNumber = scanner.nextInt();
BankAccount account = bank.findAccount(accountNumber);
if (account != null) {
System.out.print("Enter loan amount: ");
double loanAmount = scanner.nextDouble();
System.out.print("Enter interest rate: ");
double interestRate = scanner.nextDouble();
System.out.print("Enter loan term (in months): ");
int loanTerm = scanner.nextInt();
Loan newLoan = new Loan(account, loanAmount, interestRate, loanTerm);
newLoan.applyForLoan();
bank.addLoans(newLoan);
} else {
System.out.println("Account not found!");
}
}
}