-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAC-information.cpp
More file actions
162 lines (135 loc) · 3.65 KB
/
BankAC-information.cpp
File metadata and controls
162 lines (135 loc) · 3.65 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
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
class BankAccount {
private:
int accountNumber;
string name;
double balance;
public:
BankAccount(int accNum, string accHolder, double initialBalance) {
accountNumber = accNum;
name = accHolder;
balance = initialBalance;
}
int getAccountNumber() const {
return accountNumber;
}
string getName() const {
return name;
}
double getBalance() const {
return balance;
}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "Deposit successful!\n";
} else {
cout << "Invalid deposit amount!\n";
}
}
void withdraw(double amount) {
if (amount <= balance && amount > 0) {
balance -= amount;
cout << "Withdrawal successful!\n";
} else {
cout << "Insufficient balance or invalid amount!\n";
}
}
void display() const {
cout << "Account Number: " << accountNumber << endl;
cout << "Account Holder: " << name << endl;
cout << fixed << setprecision(2);
cout << "Current Balance: $" << balance << endl;
}
};
vector<BankAccount> accounts;
BankAccount* findAccount(int accNum) {
for (auto& acc : accounts) {
if (acc.getAccountNumber() == accNum)
return &acc;
}
return nullptr;
}
void createAccount() {
int accNum;
string name;
double initialBalance;
cout << "Enter account number: ";
cin >> accNum;
cin.ignore(); // Clear buffer
cout << "Enter account holder name: ";
getline(cin, name);
cout << "Enter initial balance: $";
cin >> initialBalance;
accounts.push_back(BankAccount(accNum, name, initialBalance));
cout << "Account created successfully!\n\n";
}
void viewAccount() {
int accNum;
cout << "Enter account number to view: ";
cin >> accNum;
BankAccount* acc = findAccount(accNum);
if (acc) {
acc->display();
} else {
cout << "Account not found.\n";
}
cout << endl;
}
void depositToAccount() {
int accNum;
double amount;
cout << "Enter account number to deposit into: ";
cin >> accNum;
BankAccount* acc = findAccount(accNum);
if (acc) {
cout << "Enter amount to deposit: $";
cin >> amount;
acc->deposit(amount);
} else {
cout << "Account not found.\n";
}
cout << endl;
}
void withdrawFromAccount() {
int accNum;
double amount;
cout << "Enter account number to withdraw from: ";
cin >> accNum;
BankAccount* acc = findAccount(accNum);
if (acc) {
cout << "Enter amount to withdraw: $";
cin >> amount;
acc->withdraw(amount);
} else {
cout << "Account not found.\n";
}
cout << endl;
}
int main() {
int choice;
do {
cout << "======= Bank Account Management =======\n";
cout << "1. Create New Account\n";
cout << "2. View Account Details\n";
cout << "3. Deposit\n";
cout << "4. Withdraw\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cout << "---------------------------------------\n";
switch (choice) {
case 1: createAccount(); break;
case 2: viewAccount(); break;
case 3: depositToAccount(); break;
case 4: withdrawFromAccount(); break;
case 5: cout << "Thank you! Exiting...\n"; break;
default: cout << "Invalid choice. Try again.\n\n"; break;
}
} while (choice != 5);
return 0;
}