-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccountSimulation.java
More file actions
196 lines (173 loc) · 7.82 KB
/
Copy pathBankAccountSimulation.java
File metadata and controls
196 lines (173 loc) · 7.82 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
import java.util.Scanner;
/**
* BankAccountSimulation class contains the main method to run a console-based bank account simulation.
* It provides an interactive menu for users to create accounts, perform banking transactions,
* and view balance/transaction histories.
*/
public class BankAccountSimulation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Account activeAccount = null;
System.out.println("=================================================");
System.out.println(" WELCOME TO BANK ACCOUNT SIMULATION ");
System.out.println("=================================================");
boolean running = true;
while (running) {
System.out.println("\n---------------- MAIN MENU ----------------");
System.out.println("1. Create New Bank Account");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Check Balance");
System.out.println("5. View Transaction History");
System.out.println("6. Apply Interest (Savings Account Only)");
System.out.println("7. Exit");
System.out.print("Choose an option (1-7): ");
int choice = readIntegerInput(scanner);
switch (choice) {
case 1:
activeAccount = createAccountMenu(scanner);
break;
case 2:
if (checkAccountExists(activeAccount)) {
System.out.print("Enter deposit amount: Rs. ");
double amount = readDoubleInput(scanner);
activeAccount.deposit(amount);
}
break;
case 3:
if (checkAccountExists(activeAccount)) {
System.out.print("Enter withdrawal amount: Rs. ");
double amount = readDoubleInput(scanner);
activeAccount.withdraw(amount);
}
break;
case 4:
if (checkAccountExists(activeAccount)) {
activeAccount.displayBalance();
}
break;
case 5:
if (checkAccountExists(activeAccount)) {
activeAccount.showTransactionHistory();
}
break;
case 6:
if (checkAccountExists(activeAccount)) {
// Check if the active account is a SavingsAccount (Polymorphism)
if (activeAccount instanceof SavingsAccount) {
SavingsAccount savings = (SavingsAccount) activeAccount;
savings.applyInterest();
} else {
System.out.println("Error: Interest can only be applied to a Savings Account.");
System.out.println("Current active account is a Standard Account.");
}
}
break;
case 7:
System.out.println("\nThank you for using Bank Account Simulation! Goodbye.");
running = false;
break;
default:
System.out.println("Invalid option. Please select a number between 1 and 7.");
}
}
scanner.close();
}
/**
* Menu to guide the user through creating a new account (Standard or Savings).
*/
private static Account createAccountMenu(Scanner scanner) {
System.out.println("\n--- Create Account ---");
System.out.println("Select Account Type:");
System.out.println("1. Standard Account");
System.out.println("2. Savings Account (Enforces Rs. 500 Min Balance + Interest)");
System.out.print("Enter choice (1-2): ");
int type = readIntegerInput(scanner);
if (type != 1 && type != 2) {
System.out.println("Invalid choice. Aborting account creation.");
return null;
}
System.out.print("Enter Account Number: ");
String accNum = scanner.nextLine().trim();
while (accNum.isEmpty()) {
System.out.print("Account number cannot be empty. Enter Account Number: ");
accNum = scanner.nextLine().trim();
}
System.out.print("Enter Account Holder Name: ");
String accHolder = scanner.nextLine().trim();
while (accHolder.isEmpty()) {
System.out.print("Account holder name cannot be empty. Enter Account Holder Name: ");
accHolder = scanner.nextLine().trim();
}
System.out.print("Enter Initial Deposit: Rs. ");
double initialDeposit = readDoubleInput(scanner);
while (initialDeposit < 0) {
System.out.print("Initial deposit cannot be negative. Enter Initial Deposit: Rs. ");
initialDeposit = readDoubleInput(scanner);
}
Account newAccount;
if (type == 1) {
newAccount = new Account(accNum, accHolder, initialDeposit);
System.out.println("\nStandard Account successfully created!");
} else {
// For SavingsAccount, we can also customize interest rate or use default
System.out.print("Enter annual interest rate (e.g. 3.5 for 3.5%, or press Enter to use default 3.5%): ");
String rateInput = scanner.nextLine().trim();
if (rateInput.isEmpty()) {
newAccount = new SavingsAccount(accNum, accHolder, initialDeposit);
} else {
try {
double rate = Double.parseDouble(rateInput) / 100.0;
if (rate < 0) {
System.out.println("Invalid interest rate. Using default 3.5%.");
newAccount = new SavingsAccount(accNum, accHolder, initialDeposit);
} else {
newAccount = new SavingsAccount(accNum, accHolder, initialDeposit, rate);
}
} catch (NumberFormatException e) {
System.out.println("Invalid number format. Using default 3.5%.");
newAccount = new SavingsAccount(accNum, accHolder, initialDeposit);
}
}
System.out.println("\nSavings Account successfully created!");
}
newAccount.displayBalance();
return newAccount;
}
/**
* Check if an active account exists. Displays an error if not.
*/
private static boolean checkAccountExists(Account account) {
if (account == null) {
System.out.println("Error: No active account found. Please create an account first (Option 1).");
return false;
}
return true;
}
/**
* Safely reads an integer input, handling invalid text inputs to prevent program crash.
*/
private static int readIntegerInput(Scanner scanner) {
while (true) {
try {
String input = scanner.nextLine().trim();
return Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.print("Invalid input. Please enter a valid integer option: ");
}
}
}
/**
* Safely reads a double input, handling invalid text inputs to prevent program crash.
*/
private static double readDoubleInput(Scanner scanner) {
while (true) {
try {
String input = scanner.nextLine().trim();
return Double.parseDouble(input);
} catch (NumberFormatException e) {
System.out.print("Invalid input. Please enter a valid decimal number: Rs. ");
}
}
}
}