-
Notifications
You must be signed in to change notification settings - Fork 22
/
Bank_Account_Management_System.cpp
290 lines (241 loc) · 8.16 KB
/
Bank_Account_Management_System.cpp
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
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <ctime>
#include <limits>
#include <algorithm>
using namespace std;
// Forward declarations
class Account;
class Transaction;
class Bank;
// Transaction class to store transaction history
class Transaction {
private:
string type;
double amount;
double balanceAfter;
string timestamp;
public:
Transaction(string type, double amount, double balanceAfter)
: type(type), amount(amount), balanceAfter(balanceAfter) {
time_t now = time(0);
timestamp = ctime(&now);
timestamp.pop_back(); // Remove newline character
}
// Getter methods
string getType() const { return type; }
double getAmount() const { return amount; }
double getBalanceAfter() const { return balanceAfter; }
string getTimestamp() const { return timestamp; }
};
// Account class to manage individual accounts
class Account {
private:
static int nextAccountNumber;
int accountNumber;
string accountHolder;
double balance;
string pin;
vector<Transaction> transactions;
public:
Account(string holder, string pin)
: accountHolder(holder), balance(0.0), pin(pin) {
accountNumber = nextAccountNumber++;
}
// Getter methods
int getAccountNumber() const { return accountNumber; }
string getAccountHolder() const { return accountHolder; }
double getBalance() const { return balance; }
bool verifyPin(const string& inputPin) const {
return pin == inputPin;
}
bool deposit(double amount) {
if (amount <= 0) return false;
balance += amount;
transactions.push_back(Transaction("Deposit", amount, balance));
return true;
}
bool withdraw(double amount) {
if (amount <= 0 || amount > balance) return false;
balance -= amount;
transactions.push_back(Transaction("Withdrawal", amount, balance));
return true;
}
void displayTransactionHistory() const {
cout << "\nTransaction History for Account #" << accountNumber << endl;
cout << string(60, '-') << endl;
cout << left << setw(20) << "Date/Time"
<< left << setw(12) << "Type"
<< right << setw(12) << "Amount"
<< right << setw(16) << "Balance" << endl;
cout << string(60, '-') << endl;
for (const auto& transaction : transactions) {
cout << left << setw(20) << transaction.getTimestamp()
<< left << setw(12) << transaction.getType()
<< right << setw(12) << fixed << setprecision(2) << transaction.getAmount()
<< right << setw(16) << transaction.getBalanceAfter() << endl;
}
cout << string(60, '-') << endl;
}
};
// Static member initialization
int Account::nextAccountNumber = 1001;
// Bank class to manage all accounts
class Bank {
private:
vector<Account> accounts;
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void waitForEnter() {
cout << "\nPress Enter to continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
public:
void createAccount() {
clearScreen();
cout << "\n=== Create New Account ===" << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string name, pin;
cout << "Enter account holder name: ";
getline(cin, name);
while (true) {
cout << "Enter 4-digit PIN: ";
getline(cin, pin);
if (pin.length() == 4 && all_of(pin.begin(), pin.end(), ::isdigit)) {
break;
}
cout << "Invalid PIN. Please enter exactly 4 digits." << endl;
}
Account account(name, pin);
accounts.push_back(account);
cout << "\nAccount created successfully!" << endl;
cout << "Your account number is: " << account.getAccountNumber() << endl;
waitForEnter();
}
Account* findAccount(int accountNumber) {
for (auto& account : accounts) {
if (account.getAccountNumber() == accountNumber) {
return &account;
}
}
return nullptr;
}
void performTransaction(bool isDeposit) {
clearScreen();
cout << "\n=== " << (isDeposit ? "Deposit" : "Withdrawal") << " ===" << endl;
int accountNumber;
string pin;
double amount;
cout << "Enter account number: ";
cin >> accountNumber;
Account* account = findAccount(accountNumber);
if (!account) {
cout << "Account not found!" << endl;
waitForEnter();
return;
}
cout << "Enter PIN: ";
cin >> pin;
if (!account->verifyPin(pin)) {
cout << "Incorrect PIN!" << endl;
waitForEnter();
return;
}
cout << "Enter amount: $";
cin >> amount;
bool success;
if (isDeposit) {
success = account->deposit(amount);
} else {
success = account->withdraw(amount);
}
if (success) {
cout << "\nTransaction successful!" << endl;
cout << "New balance: $" << fixed << setprecision(2) << account->getBalance() << endl;
} else {
cout << "\nTransaction failed!" << endl;
if (!isDeposit) {
cout << "Insufficient funds or invalid amount." << endl;
} else {
cout << "Invalid amount." << endl;
}
}
waitForEnter();
}
void checkBalance() {
clearScreen();
cout << "\n=== Check Balance ===" << endl;
int accountNumber;
string pin;
cout << "Enter account number: ";
cin >> accountNumber;
Account* account = findAccount(accountNumber);
if (!account) {
cout << "Account not found!" << endl;
waitForEnter();
return;
}
cout << "Enter PIN: ";
cin >> pin;
if (!account->verifyPin(pin)) {
cout << "Incorrect PIN!" << endl;
waitForEnter();
return;
}
cout << "\nAccount Holder: " << account->getAccountHolder() << endl;
cout << "Current Balance: $" << fixed << setprecision(2) << account->getBalance() << endl;
account->displayTransactionHistory();
waitForEnter();
}
void displayMenu() {
while (true) {
clearScreen();
cout << "\n=== Bank Account Management System ===" << endl;
cout << "1. Create New Account" << endl;
cout << "2. Deposit" << endl;
cout << "3. Withdraw" << endl;
cout << "4. Check Balance" << endl;
cout << "5. Exit" << endl;
cout << "\nEnter your choice (1-5): ";
int choice;
cin >> choice;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
}
switch (choice) {
case 1:
createAccount();
break;
case 2:
performTransaction(true); // deposit
break;
case 3:
performTransaction(false); // withdraw
break;
case 4:
checkBalance();
break;
case 5:
cout << "\nThank you for using our banking system!" << endl;
return;
default:
cout << "\nInvalid choice. Please try again." << endl;
waitForEnter();
}
}
}
};
int main() {
Bank bank;
bank.displayMenu();
return 0;
}