-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
433 lines (336 loc) · 12.8 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int MAX = 9999999999;
class Account
{
private:
static int CurAccNo;
bool open = true;
int AccNo = ++CurAccNo;
double balance = 0;
string gender = "N/A";
string first_name, second_name;
public:
Account(){}
Account(string, string, int);
void deposit(double);
void withdraw(double);
void transfer(Account, double);
static void setCurAccNo(int No){
if(No > 0)
CurAccNo = No;
}
int getAccNo();
double getBalance();
string getGender();
string getFirstName();
string getSecondName();
void setBalance(double);
void setGender(string);
void setFirstName(string);
void setSecondName(string);
void close();
bool isOpen();
friend istream& operator >> (istream& in, Account &acc);
friend ostream& operator << (ostream& out, Account &acc);
friend ifstream& operator >> (ifstream& in, Account &acc);
friend ofstream& operator << (ofstream& out, Account &acc);
};
int Account::CurAccNo = 0;
Account::Account(string first_name, string second_name, int initial_balance)
{
setFirstName(first_name);
setSecondName(second_name);
balance = initial_balance;
}
void Account::deposit(double value){balance += value;}
void Account::withdraw(double value){balance -= value;}
void Account::transfer(Account other, double value){balance -= value ;other.balance += value;}
int Account::getAccNo(){return AccNo;}
double Account::getBalance(){return balance;}
string Account::getGender(){return gender;}
string Account::getFirstName(){return first_name;}
string Account::getSecondName(){return second_name;}
void Account::setBalance(double balance){if(balance >= 0) this -> balance = balance;}
void Account::setGender(string gender){if(gender == "M" || gender == "F") this -> gender = gender;}
void Account::setFirstName(string first_name){if(first_name != "") this -> first_name = first_name;}
void Account::setSecondName(string second_name){if(second_name != "") this -> second_name = second_name;}
void Account::close(){open = false;}
bool Account::isOpen(){return open;}
istream& operator >> (istream& in, Account &acc){
string first_name, second_name;
string gender;
double balance;
cout << "Enter First Name: "; cin >> first_name;
cout << "Enter Second Name: "; cin >> second_name;
cout << "Enter Gender (M / F): "; cin >> gender;
cout << "Enter Initial Balance: "; cin >> balance;
acc.setFirstName(first_name);
acc.setSecondName(second_name);
acc.setGender(gender);
acc.setBalance(balance);
return in;
}
ostream& operator << (ostream& out, Account &acc){
out << "First Name: " << acc.getFirstName() << endl;
out << "Second Name: " << acc.getSecondName() << endl;
out << "Gender: " << acc.getGender() << endl;
out << "Current Balance: " << acc.getBalance() << endl;
out << "Account Number: " << acc.getAccNo() << endl;
out << "Status: " << (acc.isOpen()? "Open" : "Closed") << endl;
return out;
}
ifstream& operator >> (ifstream& in, Account &acc){
string first_name, second_name;
string gender;
int AccNo; bool open;
double balance;
in >> first_name >> second_name;
in >> gender;
in >> balance >> AccNo;
in >> open;
acc.setFirstName(first_name);
acc.setSecondName(second_name);
acc.setGender(gender);
acc.setBalance(balance);
acc.open = open;
acc.AccNo = AccNo;
return in;
}
ofstream& operator << (ofstream& out, Account &acc){
out << acc.getFirstName() << endl;
out << acc.getSecondName() << endl;
out << acc.getGender() << endl;
out << acc.getBalance() << endl;
out << acc.getAccNo() << endl;
out << acc.isOpen();
return out;
}
bool isEmpty(ifstream& pFile)
{
return pFile.peek() == ifstream::traits_type::eof();
}
bool Valid(unsigned int AccNo, vector<Account> Accounts){
if(AccNo <= Accounts.size() && AccNo > 0 && Accounts[AccNo].isOpen())
return true;
return false;
}
int main()
{
vector<Account> Accounts;
ofstream otrans("Transactions.txt", ios::app);
//ios::app means you want to open a file in the append mode (for writing data to a file after the last byte)
ofstream ofs("BankingSystem.txt", ios::app);
ifstream ifs("BankingSystem.txt");
vector<int> AccNos;
vector<string> types;
vector<double> values;
while(!ifs.eof() && !isEmpty(ifs)){
Account acc;
ifs >> acc;
Accounts.push_back(acc);
}
Account::setCurAccNo(Accounts.size());
while(true){
cout << "|=============================|" << endl;
cout << "| WELCOME TO |" << endl;
cout << "| ABC BANK |" << endl;
cout << "|=============================|" << endl;
cout << endl;
cout << "What Would You Like To Do?:" << endl;
cout << " 1. Open Account" << endl;
cout << " 2. Close Account" << endl;
cout << " 3. Show All Accounts" << endl;
cout << " 4. Deposit in Account" << endl;
cout << " 5. Withdraw in Account" << endl;
cout << " 6. Transfer to an Account" << endl;
cout << " 7. Show Balance" << endl;
cout << " 8. Show All Transactions" << endl;
cout << " 9. Quit" << endl;
cout << endl;
string option;
cout << "Please Enter Your Option: "; cin >> option;
if(option == "1"){
Account acc;
cin >> acc;
if(Accounts.size() != 0)
ofs << endl;
Accounts.push_back(acc);
ofs << acc;
cout << "Account Opened Successfully!" << endl;
cout << "Your Account Number is " << acc.getAccNo() << endl;
}
else if(option == "2"){
int AccNo;
cout << "Enter Account Number: "; cin >> AccNo;
AccNo--;
if(Valid(AccNo, Accounts)){
cout << "Account Closed Successfully!" << endl;
Accounts[AccNo].close();
}
else
cout << "There Was an Error, Please Try Again!" << endl;
}
else if(option == "3"){
cout << endl;
for(unsigned int i = 0; i < Accounts.size(); i++)
cout << Accounts[i] << endl;
}
else if(option == "4"){
int AccNo;
double value;
cout << "Enter Account Number: "; cin >> AccNo;
cout << "Enter Deposit Amount: "; cin >> value;
AccNo--;
if(Valid(AccNo, Accounts)){
if(value < MAX){
cout << value << " Deposited to " << Accounts[AccNo].getFirstName() << "'s Account Successfully!" << endl;
Accounts[AccNo].deposit(value);
cout << "Balance is " << Accounts[AccNo].getBalance() << endl;
AccNos.push_back(AccNo);
types.push_back("Deposit");
values.push_back(value);
}
else
cout << "Overflow!" << endl;
}
else
cout << "There Was an Error, Please Try Again!" << endl;
}
else if(option == "5"){ //Withdraw in Account
int AccNo;
double value;
cout << "Enter Account Number: "; cin >> AccNo;
cout << "Enter Withdrawal Amount: "; cin >> value;
AccNo--;
if(Valid(AccNo, Accounts)){
if(Accounts[AccNo].getBalance() - value >= 0){
cout << value << " Withdrawn from " << Accounts[AccNo].getFirstName() << "'s Account Successfully!" << endl;
Accounts[AccNo].withdraw(value);
cout << "Balance is " << Accounts[AccNo].getBalance() << endl;
AccNos.push_back(AccNo);
types.push_back("Withdrawal");
values.push_back(value);
}
else
cout << "Not Enough Balance" << endl;
}
else
cout << "There Was an Error, Please Try Again!" << endl;
}
else if(option == "6"){ //Transfer to an Account
int AccNo, AccNoOther;
double value;
cout << "Enter Account Number: "; cin >> AccNo;
cout << "Enter Other Account Number: "; cin >> AccNoOther;
cout << "Enter Transfer Amount: "; cin >> value;
AccNo--;
if(Valid(AccNo, Accounts) && Valid(AccNoOther, Accounts)){
if(Accounts[AccNoOther].getBalance() + value > MAX)
cout << "Overflow" << endl;
else {
if(Accounts[AccNo].getBalance() - value >= 0){
cout << value << " Transfer to " << Accounts[AccNoOther].getFirstName() << "'s Account Successfully!" << endl;
Accounts[AccNo].transfer(Accounts[AccNoOther], value);
cout << Accounts[AccNo].getFirstName() << "'s Balance is " << Accounts[AccNo].getBalance() << endl;
cout << Accounts[AccNoOther].getFirstName() << "'s Balance is " << Accounts[AccNo].getBalance() << endl;
}
else
cout << "Not Enough Balance" << endl;
}
}
else
cout << "There Was an Error, Please Try Again!" << endl;
}
else if(option == "7"){
int AccNo;
cout << "Enter Account Number: "; cin >> AccNo;
AccNo--;
cout << endl;
if(Valid(AccNo, Accounts))
cout << Accounts[AccNo];
else
cout << "There Was an Error, Please Try Again!" << endl;
}
else if(option == "8"){
int AccNo;
cout << "Enter Account Number: "; cin >> AccNo;
AccNo--;
if(Valid(AccNo, Accounts)){
ifstream itrans("Transactions.txt");
vector<double> deposits;
vector<double> withdrawals;
string x = "Deposits";
while(!itrans.eof() && !isEmpty(itrans)){
int TransAcc;
string type;
double value;
itrans >> TransAcc >> type >> value;
if(TransAcc == AccNo){
if(type == "Deposit")
deposits.push_back(value);
if(type == "Withdrawal")
withdrawals.push_back(value);
}
}
for(unsigned int i = 0; i < AccNos.size(); i++){
int TransAcc = AccNos[i];
string type = types[i];
double value = values[i];
if(TransAcc == AccNo){
if(type == "Deposit")
deposits.push_back(value);
if(type == "Withdrawal")
withdrawals.push_back(value);
}
}
cout << "=======================================" << endl;
cout << "Withdrawal | Deposits" << endl;
cout << "=======================================" << endl;
cout << endl;
for(unsigned int i = 0; i < max(deposits.size(), withdrawals.size()); i++){
int s = 0;
if(i < withdrawals.size()){
string x = to_string(withdrawals[i]);
cout << x;
s = x.size();
}
string x = "Deposits ";
for(unsigned int _ = 0; _ < x.size() - s; _++)
cout << " ";
cout << "| ";
if(i < deposits.size()){
string x = to_string(deposits[i]);
cout << x;
}
cout << endl;
}
}
else
cout << "There Was an Error, Please Try Again!" << endl;
}
else if(option == "9"){
break;
}
else {
cout << "Please Enter a Valid Option" << endl;
}
cout << endl;
system("pause");
system("cls");
}
ofstream ofsn("BankingSystem.txt");
for(unsigned int i = 0; i < Accounts.size(); i++){
if(i != 0)
ofsn << "\n";
ofsn << Accounts[i];
}
for(unsigned int i = 0; i < AccNos.size(); i++){
if(i != 0)
otrans << "\n";
otrans << AccNos[i] << " " << types[i] << " " << values[i] << endl;
}
}