1
+ #include < iostream>
2
+ #include < fstream>
3
+ #include < cstdlib>
4
+ #include < vector>
5
+ #include < map>
6
+ using namespace std ;
7
+ #define MIN_BALANCE 500
8
+
9
+ class InsufficientFunds {};
10
+
11
+ class Account {
12
+ private:
13
+ long accountNumber;
14
+ string firstName;
15
+ string lastName;
16
+ float balance;
17
+ static long NextAccountNumber;
18
+ public:
19
+ Account (){}
20
+ Account (string fname,string lname,float balance);
21
+ long getAccNo (){return accountNumber;}
22
+ string getFirstName (){return firstName;}
23
+ string getLastName (){return lastName;}
24
+ float getBalance (){return balance;}
25
+
26
+ void Deposit (float amount);
27
+ void Withdraw (float amount);
28
+ static void setLastAccountNumber (long accountNumber);
29
+ static long getLastAccountNumber ();
30
+ friend ofstream & operator <<(ofstream &ofs,Account &acc);
31
+ friend ifstream & operator >>(ifstream &ifs,Account &acc);
32
+ friend ostream & operator <<(ostream &os,Account &acc);
33
+ };
34
+
35
+ long Account::NextAccountNumber=0 ;
36
+
37
+ class Bank {
38
+ private:
39
+ map<long ,Account> accounts;
40
+ public:
41
+ Bank ();
42
+ Account OpenAccount (string fname,string lname,float balance);
43
+ Account BalanceEnquiry (long accountNumber);
44
+ Account Deposit (long accountNumber,float amount);
45
+ Account Withdraw (long accountNumber,float amount);
46
+ void CloseAccount (long accountNumber);
47
+ void ShowAllAccounts ();
48
+ ~Bank ();
49
+ };
50
+
51
+ int main (){
52
+ Bank b;
53
+ Account acc;
54
+
55
+ int choice;
56
+ string fname,lname;
57
+ long accountNumber;
58
+ float balance;
59
+ float amount;
60
+ cout<<" ***Banking System***" <<endl;
61
+ do {
62
+ cout<<" \n\t Select one option below " ;
63
+ cout<<" \n\t 1 Open an Account" ;
64
+ cout<<" \n\t 2 Balance Enquiry" ;
65
+ cout<<" \n\t 3 Deposit" ;
66
+ cout<<" \n\t 4 Withdrawal" ;
67
+ cout<<" \n\t 5 Close an Account" ;
68
+ cout<<" \n\t 6 Show All Accounts" ;
69
+ cout<<" \n\t 7 Quit" ;
70
+ cout<<" \n Enter your choice: " ;
71
+ cin>>choice;
72
+
73
+ switch (choice){
74
+ case 1 :
75
+ cout<<" Enter First Name: " ;
76
+ cin>>fname;
77
+ cout<<" Enter Last Name: " ;
78
+ cin>>lname;
79
+ cout<<" Enter initil Balance: " ;
80
+ cin>>balance;
81
+ acc=b.OpenAccount (fname,lname,balance);
82
+ cout<<endl<<" Congradulation Account is Created" <<endl;
83
+ cout<<acc;
84
+ break ;
85
+ case 2 :
86
+ cout<<" Enter Account Number:" ;
87
+ cin>>accountNumber;
88
+ acc=b.BalanceEnquiry (accountNumber);
89
+ cout<<endl<<" Your Account Details" <<endl;
90
+ cout<<acc;
91
+ break ;
92
+ case 3 :
93
+ cout<<" Enter Account Number:" ;
94
+ cin>>accountNumber;
95
+ cout<<" Enter Balance:" ;
96
+ cin>>amount;
97
+ acc=b.Deposit (accountNumber, amount);
98
+ cout<<endl<<" Amount is Deposited" <<endl;
99
+ cout<<acc;
100
+ break ;
101
+ case 4 :
102
+ cout<<" Enter Account Number:" ;
103
+ cin>>accountNumber;
104
+ cout<<" Enter Balance:" ;
105
+ cin>>amount;
106
+ acc=b.Withdraw (accountNumber, amount);
107
+ cout<<endl<<" Amount Withdrawn" <<endl;
108
+ cout<<acc;
109
+ break ;
110
+ case 5 :
111
+ cout<<" Enter Account Number:" ;
112
+ cin>>accountNumber;
113
+ b.CloseAccount (accountNumber);
114
+ cout<<endl<<" Account is Closed" <<endl;
115
+ cout<<acc;
116
+ case 6 :
117
+ b.ShowAllAccounts ();
118
+ break ;
119
+ case 7 :
120
+ break ;
121
+ default :
122
+ cout<<" \n Enter corret choice" ;
123
+ exit (0 );
124
+ }
125
+ }while (choice!=7 );
126
+
127
+ return 0 ;
128
+ }
129
+
130
+ Account::Account (string fname,string lname,float balance){
131
+ NextAccountNumber++;
132
+ accountNumber=NextAccountNumber;
133
+ firstName=fname;
134
+ lastName=lname;
135
+ this ->balance =balance;
136
+ }
137
+
138
+ void Account::Deposit (float amount){
139
+ balance+=amount;
140
+ }
141
+
142
+ void Account::Withdraw (float amount){
143
+ if (balance-amount<MIN_BALANCE)
144
+ throw InsufficientFunds ();
145
+ balance-=amount;
146
+ }
147
+ void Account::setLastAccountNumber (long accountNumber){
148
+ NextAccountNumber=accountNumber;
149
+ }
150
+
151
+ long Account::getLastAccountNumber (){
152
+ return NextAccountNumber;
153
+ }
154
+
155
+ ofstream & operator <<(ofstream &ofs,Account &acc){
156
+ ofs<<acc.accountNumber <<endl;
157
+ ofs<<acc.firstName <<endl;
158
+ ofs<<acc.lastName <<endl;
159
+ ofs<<acc.balance <<endl;
160
+ return ofs;
161
+ }
162
+
163
+ ifstream & operator >>(ifstream &ifs,Account &acc){
164
+ ifs>>acc.accountNumber ;
165
+ ifs>>acc.firstName ;
166
+ ifs>>acc.lastName ;
167
+ ifs>>acc.balance ;
168
+ return ifs;
169
+ }
170
+
171
+ ostream & operator <<(ostream &os,Account &acc){
172
+ os<<" First Name:" <<acc.getFirstName ()<<endl;
173
+ os<<" Last Name:" <<acc.getLastName ()<<endl;
174
+ os<<" Account Number:" <<acc.getAccNo ()<<endl;
175
+ os<<" Balance:" <<acc.getBalance ()<<endl;
176
+ return os;
177
+ }
178
+
179
+ Bank::Bank (){
180
+
181
+ Account account;
182
+ ifstream infile;
183
+ infile.open (" Bank.data" );
184
+ if (!infile){
185
+ // cout<<"Error in Opening! File Not Found!!"<<endl;
186
+ return ;
187
+ }
188
+
189
+ while (!infile.eof ()){
190
+ infile>>account;
191
+ accounts.insert (pair<long ,Account>(account.getAccNo (),account));
192
+ }
193
+
194
+ Account::setLastAccountNumber (account.getAccNo ());
195
+
196
+ infile.close ();
197
+ }
198
+
199
+ Account Bank::OpenAccount (string fname,string lname,float balance){
200
+ ofstream outfile;
201
+ Account account (fname,lname,balance);
202
+ accounts.insert (pair<long ,Account>(account.getAccNo (),account));
203
+
204
+ outfile.open (" Bank.data" , ios::trunc);
205
+
206
+ map<long ,Account>::iterator itr;
207
+
208
+ for (itr=accounts.begin ();itr!=accounts.end ();itr++){
209
+ outfile<<itr->second ;
210
+ }
211
+
212
+ outfile.close ();
213
+ return account;
214
+ }
215
+
216
+ Account Bank::BalanceEnquiry (long accountNumber){
217
+ map<long ,Account>::iterator itr=accounts.find (accountNumber);
218
+ return itr->second ;
219
+ }
220
+
221
+ Account Bank::Deposit (long accountNumber,float amount){
222
+ map<long ,Account>::iterator itr=accounts.find (accountNumber);
223
+ itr->second .Deposit (amount);
224
+ return itr->second ;
225
+ }
226
+
227
+ Account Bank::Withdraw (long accountNumber,float amount){
228
+ map<long ,Account>::iterator itr=accounts.find (accountNumber);
229
+ itr->second .Withdraw (amount);
230
+ return itr->second ;
231
+ }
232
+
233
+ void Bank::CloseAccount (long accountNumber){
234
+ map<long ,Account>::iterator itr=accounts.find (accountNumber);
235
+ cout<<" Account Deleted" <<itr->second ;
236
+ accounts.erase (accountNumber);
237
+ }
238
+
239
+ void Bank::ShowAllAccounts (){
240
+ map<long ,Account>::iterator itr;
241
+
242
+ for (itr=accounts.begin ();itr!=accounts.end ();itr++){
243
+ cout<<" Account " <<itr->first <<endl<<itr->second <<endl;
244
+ }
245
+ }
246
+
247
+ Bank::~Bank (){
248
+ ofstream outfile;
249
+ outfile.open (" Bank.data" , ios::trunc);
250
+
251
+ map<long ,Account>::iterator itr;
252
+
253
+ for (itr=accounts.begin ();itr!=accounts.end ();itr++){
254
+ outfile<<itr->second ;
255
+ }
256
+ outfile.close ();
257
+ }
0 commit comments