-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankApp.java
More file actions
195 lines (161 loc) · 6.76 KB
/
BankApp.java
File metadata and controls
195 lines (161 loc) · 6.76 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
import java.util.*;
public class BankApp {
private static int nextAccountId = 101;
private static Scanner scanner = new Scanner(System.in);
private static Bank activeBank = null;
private static Map<String, Account> accountRegistry = new HashMap<>();
public static void main(String[] args) {
System.out.println("====================================");
System.out.println(" WELCOME TO BANK MANAGER ");
System.out.println("====================================");
setupBank();
runBankingSystem();
}
private static void setupBank() {
System.out.println("\nLet's set up your bank!");
System.out.println("Choose Bank Type:");
System.out.println("1. Nationalized Bank (Government Owned)");
System.out.println("2. Cooperative Bank (Community Owned)");
System.out.print("Enter your choice (1-2): ");
int choice = readIntegerInput(1, 2);
System.out.print("\nEnter Bank Name: ");
String bankName = scanner.nextLine().trim();
System.out.print("Enter Branch Name: ");
String branchName = scanner.nextLine().trim();
if (choice == 1) {
activeBank = new NationalizedBank(bankName, branchName);
System.out.println("\nNationalized Bank '" + bankName + "' created successfully!");
} else {
activeBank = new CooperativeBank(bankName, branchName);
System.out.println("\nCooperative Bank '" + bankName + "' created successfully!");
}
}
private static void runBankingSystem() {
boolean running = true;
while (running) {
System.out.println("\n================================");
System.out.println(" BANKING MENU ");
System.out.println("================================");
System.out.println("1. Open New Account");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Display Bank Information");
System.out.println("5. Exit System");
System.out.print("\nEnter your choice (1-5): ");
int choice = readIntegerInput(1, 5);
switch (choice) {
case 1:
openNewAccount();
break;
case 2:
processDeposit();
break;
case 3:
processWithdrawal();
break;
case 4:
activeBank.displayBankInfo();
break;
case 5:
System.out.println("\nThank you for using the Banking System. Goodbye!");
running = false;
break;
}
if (running) {
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
}
}
}
private static void openNewAccount() {
System.out.println("\n---- OPEN NEW ACCOUNT ----");
System.out.println("Choose Account Type:");
System.out.println("1. Savings Account (for personal savings)");
System.out.println("2. Current Account (for business transactions)");
System.out.println("3. Loan Account (for borrowing funds)");
System.out.print("Enter your choice (1-3): ");
int accountType = readIntegerInput(1, 3);
System.out.print("\nEnter Account Holder Name: ");
String holderName = scanner.nextLine().trim();
String accountNumber = "ACC" + nextAccountId++;
Account newAccount = null;
switch (accountType) {
case 1:
newAccount = new SavingsAccount(accountNumber, holderName, 0);
break;
case 2:
newAccount = new CurrentAccount(accountNumber, holderName, 0);
break;
case 3:
newAccount = new LoanAccount(accountNumber, holderName, 0);
break;
}
activeBank.openAccount(newAccount);
accountRegistry.put(accountNumber, newAccount);
System.out.println("\nAccount created successfully!");
System.out.println("Your Account Number: " + accountNumber);
System.out.println("Please make a note of this for future transactions.");
}
private static void processDeposit() {
System.out.println("\n---- DEPOSIT MONEY ----");
if (accountRegistry.isEmpty()) {
System.out.println("No accounts have been created yet. Please open an account first.");
return;
}
Account account = findAccount();
if (account == null) return;
System.out.print("Enter amount to deposit (₹): ");
double amount = readPositiveAmount();
account.deposit(amount);
}
private static void processWithdrawal() {
System.out.println("\n---- WITHDRAW MONEY ----");
if (accountRegistry.isEmpty()) {
System.out.println("No accounts have been created yet. Please open an account first.");
return;
}
Account account = findAccount();
if (account == null) return;
System.out.print("Enter amount to withdraw (₹): ");
double amount = readPositiveAmount();
account.withdraw(amount);
}
private static Account findAccount() {
System.out.print("Enter Account Number: ");
String accountNumber = scanner.nextLine().trim();
Account account = accountRegistry.get(accountNumber);
if (account == null) {
System.out.println("Account not found. Please check the account number.");
return null;
}
return account;
}
private static int readIntegerInput(int min, int max) {
int value;
while (true) {
try {
String input = scanner.nextLine().trim();
value = Integer.parseInt(input);
if (value >= min && value <= max) break;
else System.out.print("Please enter a number between " + min + " and " + max + ": ");
} catch (NumberFormatException e) {
System.out.print("Invalid input. Please enter a number: ");
}
}
return value;
}
private static double readPositiveAmount() {
double amount;
while (true) {
try {
String input = scanner.nextLine().trim();
amount = Double.parseDouble(input);
if (amount > 0) break;
else System.out.print("Please enter a positive amount: ");
} catch (NumberFormatException e) {
System.out.print("Invalid input. Please enter a valid amount: ");
}
}
return amount;
}
}