-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbankingSystem.cpp
More file actions
182 lines (164 loc) · 4.37 KB
/
Copy pathbankingSystem.cpp
File metadata and controls
182 lines (164 loc) · 4.37 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Account class to manage individual accounts
class Account
{
private:
int accountNumber;
string accountHolderName;
double balance;
public:
// Constructor
Account(int accNumber, string accHolderName, double initialBalance)
: accountNumber(accNumber), accountHolderName(accHolderName), balance(initialBalance) {}
// Deposit money into the account
void deposit(double amount)
{
if (amount > 0)
{
balance += amount;
cout << "Deposited: " << amount << endl;
}
else
{
cout << "Deposit amount must be positive!" << endl;
}
}
// Withdraw money from the account
void withdraw(double amount)
{
if (amount > 0 && amount <= balance)
{
balance -= amount;
cout << "Withdrew: " << amount << endl;
}
else
{
cout << "Insufficient balance or invalid amount!" << endl;
}
}
// Display account details
void display() const
{
cout << "Account Number: " << accountNumber << endl;
cout << "Account Holder: " << accountHolderName << endl;
cout << "Balance: " << balance << endl;
}
// Get account number
int getAccountNumber() const { return accountNumber; }
};
// Bank class to manage multiple accounts
class Bank
{
private:
vector<Account> accounts;
public:
// Add a new account to the bank
void addAccount(const Account &account)
{
accounts.push_back(account);
cout << "Account added successfully!" << endl;
}
// Find an account by account number
Account *findAccount(int accNumber)
{
for (auto &account : accounts)
{
if (account.getAccountNumber() == accNumber)
{
return &account;
}
}
return nullptr;
}
// Deposit money into an account
void deposit(int accNumber, double amount)
{
Account *account = findAccount(accNumber);
if (account)
{
account->deposit(amount);
}
else
{
cout << "Account not found!" << endl;
}
}
// Withdraw money from an account
void withdraw(int accNumber, double amount)
{
Account *account = findAccount(accNumber);
if (account)
{
account->withdraw(amount);
}
else
{
cout << "Account not found!" << endl;
}
}
// Display all accounts
void displayAllAccounts() const
{
for (const auto &account : accounts)
{
account.display();
cout << "------------------------" << endl;
}
}
};
// Main function to demonstrate the banking system
int main()
{
Bank bank;
// Create and add some accounts
bank.addAccount(Account(101, "John Doe", 5000.00));
bank.addAccount(Account(102, "Jane Smith", 3000.00));
// Main loop
while (true)
{
cout << "Banking System Menu:" << endl;
cout << "1. Display All Accounts" << endl;
cout << "2. Deposit Money" << endl;
cout << "3. Withdraw Money" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
if (choice == 1)
{
bank.displayAllAccounts();
}
else if (choice == 2)
{
int accNumber;
double amount;
cout << "Enter account number: ";
cin >> accNumber;
cout << "Enter amount to deposit: ";
cin >> amount;
bank.deposit(accNumber, amount);
}
else if (choice == 3)
{
int accNumber;
double amount;
cout << "Enter account number: ";
cin >> accNumber;
cout << "Enter amount to withdraw: ";
cin >> amount;
bank.withdraw(accNumber, amount);
}
else if (choice == 4)
{
break;
}
else
{
cout << "Invalid choice, please try again." << endl;
}
}
return 0;
}