-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
317 lines (249 loc) · 8.54 KB
/
main.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
class InsufficientFunds{};
class Account
{
public:
Account() = default;
Account(std::string firstName, std::string lastName, double balance);
long getAccountNumber() { return accountNumber; }
std::string getFirstName() { return firstName; }
std::string getLastName() { return lastName; }
double getBalance(){ return balance; }
void Deposit(double amount);
void Withdraw(double amount);
static void setLastAccountNumber(long accountNumber);
static long getLastAccountNumber();
friend std::ofstream & operator << (std::ofstream& ofs, Account& account);
friend std::ifstream & operator >> (std::ifstream& ifs, Account& account);
friend std::ostream & operator << (std::ostream& os, Account& account);
private:
long accountNumber;
std::string firstName;
std::string lastName;
double balance;
static long nextAccountNumber;
};
long Account::nextAccountNumber = 0;
class Bank
{
public:
Bank();
Account OpenAccount(std::string firstName, std::string lastName, double balance);
Account BalanceEnquiry(long accountNumber);
Account Deposit(long accountNumber, double amount);
Account Withdraw(long accountNumber, double amount);
void closeAccount(long accountNumber);
void showAllAccounts();
~Bank();
private:
std::map<long, Account> accounts;
};
int main(void)
{
Bank bank;
Account account;
int selection = 0;
std::string firstName, lastName;
long accountNumber = 0;
double balance = 0.0;
double amount = 0.0;
std::cout << "***Banking System***" << std::endl;
do
{
std::cout << "\n\tSelect one option below" << std::endl;
std::cout << "\t1. Open an Account" << std::endl;
std::cout << "\t2. Balance Enquiry" << std::endl;
std::cout << "\t3. Deposit" << std::endl;
std::cout << "\t4. Withdrawl" << std::endl;
std::cout << "\t5. Close an Account" << std::endl;
std::cout << "\t6. Show All Accounts" << std::endl;
std::cout << "\t7. Quit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> selection;
switch (selection) {
case 1:
std::cout << "Enter your first name: ";
std::cin >> firstName;
std::cout << "Enter your last name: ";
std::cin >> lastName;
std::cout << "Enter your initial amount: ";
std::cin >> balance;
account = bank.OpenAccount(firstName, lastName, balance);
std::cout << "\n" << "Congradulations account is Created" << std::endl;
std::cout << account;
break;
case 2:
std::cout << "Enter Account Number: ";
std::cout << accountNumber << std::endl;
account = bank.BalanceEnquiry(accountNumber);
std::cout << "\n" << "Your account Details" << std::endl;
std::cout << account;
break;
case 3:
std::cout << "Enter Account Number: ";
std::cin >> accountNumber;
std::cout << "Enter Balance: ";
std::cin >> amount;
account = bank.Deposit(accountNumber, amount);
std::cout << std::endl << "Amount is Deposited" << std::endl;
std::cout << account;
break;
case 4:
std::cout << "Enter Account Number: ";
std::cin >> accountNumber;
std::cout << "Enter Balance: ";
std::cin >> amount;
account = bank.Withdraw(accountNumber, amount);
std::cout << "\n" << "Amount Withdrawn" << std::endl;
std::cout << account;
break;
case 5:
std::cout << "Enter Account Number: ";
std::cin >> accountNumber;
bank.closeAccount(accountNumber);
std::cout << "\n" << "Account is Closed" << std::endl;
std::cout << account;
break;
case 6:
bank.showAllAccounts();
break;
case 7:
break;
default:
std::cout << "\nPlease select correct choice";
exit(0);
}
} while (selection != 7);
return 0;
}
Account::Account(std::string firstName, std::string lastName, double balance)
{
++nextAccountNumber;
accountNumber = nextAccountNumber;
this->firstName = firstName;
this->lastName = lastName;
this->balance = balance;
}
void Account::Deposit(double amount)
{
balance += amount; // balance = balance + amount
}
void Account::Withdraw(double amount)
{
if (balance - amount < 0)
{
throw InsufficientFunds();
}
balance -= amount; // balance = balance - amount
}
void Account::setLastAccountNumber(long accountNumber)
{
nextAccountNumber = accountNumber;
}
long Account::getLastAccountNumber()
{
return nextAccountNumber;
}
std::ofstream & operator << (std::ofstream& ofs, Account& account)
{
ofs << account.firstName << std::endl;
ofs << account.lastName << std::endl;
ofs << account.accountNumber << std::endl;
ofs << account.balance << std::endl;
return ofs;
}
std::ifstream & operator >> (std::ifstream& ifs, Account& account)
{
ifs >> account.firstName;
ifs >> account.lastName;
ifs >> account.accountNumber;
ifs >> account.balance;
return ifs;
}
std::ostream & operator << (std::ostream& os, Account& account)
{
os << "First Name: " << account.firstName << std::endl;
os << "Last Name: " << account.lastName << std::endl;
os << "Account Number " << account.accountNumber << std::endl;
os << "Balance " << account.balance << std::endl;
return os;
}
Bank::Bank()
{
Account account;
std::ifstream infile;
infile.open("Bank.data");
if (!infile)
{
std::cout << "Error in Opening" << std::endl;
return;
}
while (!infile.eof())
{
infile >> account;
accounts.insert(std::pair<long, Account>(account.getAccountNumber(), account));
}
Account::setLastAccountNumber(account.getAccountNumber());
infile.close();
}
Account Bank::OpenAccount(std::string firstName, std::string lastName, double balance)
{
std::ofstream outfile;
Account account(firstName, lastName, balance);
accounts.insert(std::pair<long, Account>(account.getAccountNumber(), account));
outfile.open("Bank.data", std::ios::trunc);
std::map<long, Account>::iterator itr;
for (itr = accounts.begin(); itr != accounts.end(); ++itr)
{
outfile << itr->second;
}
outfile.close();
return account;
}
Account Bank::BalanceEnquiry(long accountNumber)
{
std::map<long, Account>::iterator itr = accounts.find(accountNumber);
return itr->second;
}
Account Bank::Deposit(long accountNumber, double amount)
{
std::map<long, Account>::iterator itr = accounts.find(accountNumber);
itr->second.Deposit(amount);
return itr->second;
}
Account Bank::Withdraw(long accountNumber, double amount)
{
std::map<long, Account>::iterator itr = accounts.find(accountNumber);
itr->second.Withdraw(amount);
return itr->second;
}
void Bank::closeAccount(long accountNumber)
{
std::map<long, Account>::iterator itr = accounts.find(accountNumber);
std::cout << "Account Deleted " << itr->second;
accounts.erase(accountNumber);
}
void Bank::showAllAccounts()
{
std::map<long, Account>::iterator itr;
for (itr = accounts.begin(); itr != accounts.end(); ++itr)
{
std::cout << "Account " << itr->first << "\n" << itr->second << std::endl;
}
}
Bank::~Bank()
{
std::ofstream outfile;
outfile.open("Bank.data", std::ios::trunc);
std::map<long, Account>::iterator itr;
for (itr = accounts.begin(); itr != accounts.end(); ++itr)
{
outfile << itr->second;
}
outfile.close();
}