-
Notifications
You must be signed in to change notification settings - Fork 1
/
bankingTrial.cpp
410 lines (382 loc) · 11.6 KB
/
bankingTrial.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
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;
class customer
{
public:
char customerName[100];
int customerId;
int customerAge;
int customerAddress;
int customerPhoneNumber;
//methods getDetails(), updateDetails()
void getDetails();
};
void customer::getDetails()
{
cout<<"Enter customer Id"<<endl;
cin>>customerId;
cout<<"Enter customer Name"<<endl;
cin.ignore();
cin.getline(customerName, 100);
cout<<"Enter the customer age"<<endl;
cin>>customerAge;
cout<<"enter phoneNumber"<<endl;
cin>>customerPhoneNumber;
cout<<"Enter customer Address"<<endl;
cin>>customerAddress;
cout<<"user details have been captured, kindly proceed to create account"<<endl;
}
// void customer::updateDetails()//member function of customer
// //i will not use this for updating details of customer
// {
// cout<<"enter account number \n";
// cin>>customerId;
// cout<<"Enter customer name \n";
// cin.getline(customerName, 100);
// cout<<"Enter the type of account "<<" Saving account or Family Account"<<endl;
//}
class accounts : public customer//main inheritance - getDetails();
{
public:
int accountNumber;
int balance;
char accountType[20];
//function belonging to the account class
void openAccount();
void closeAccount(int num);
void updateAccount(int checkNum);
void displayDetails();
int returnAccountNumber() const
{
return accountNumber;
}
};
void accounts::openAccount()
{
cout<<"Enter the Account Number\n";
cin>>accountNumber;
//myfile<<accountNumber;
cout<<"Enter the amount you wish to deposit\n";
cin>>balance;
cout<<"Enter the type of account for the user \n"<<"Saving account or Family Account"<<endl;
cin.ignore();
cin.getline(accountType, 20);
cout<<"You acccount is successfully created\n";
cout<<endl<<endl;
}
void accounts::updateAccount(int checkNum)
{
accounts accountObject;
//the account number is provided when updateAccount is called
//check if account exists
fstream inFile;
inFile.open("accountDetails.dat", ios::in | ios::out | ios::binary);
if (!inFile)
{
cout<<"An error occured while accessing the file\n";
}
//read data from file
while(inFile.read(reinterpret_cast<char *>(&accountObject), sizeof(accounts)))
{
if (accountObject.returnAccountNumber() == checkNum)
{
cout<<"Account user has been found \n";
cout<<"the user details for the account number are : \n";
cout<<"Account Number : "<<accountObject.accountNumber<<endl;
cout<<"Customer Id : "<<accountObject.customerId<<endl;
cout<<"Account User Name : "<<accountObject.customerName<<endl;
cout<<"Account customerAddress : "<<accountObject.customerAddress<<endl;
cout<<"Account user phone number "<<accountObject.customerPhoneNumber<<endl;
//PROCEED TO CHANGE USER ACCOUNT DETAILS
cout<<"PROCEED TO CHANGE USER ACCOUNT DETAILS \n";
cout<<"Enter the new user name \n";
cin.ignore();
cin.getline(accountObject.customerName, 100);
cout<<"Enter new user home address \n";
cin>>accountObject.customerAddress;
cout<<"Enter new user phone number \n";
cin>>accountObject.customerPhoneNumber;
cout<<"Enter the type of account you wish to change to \n Savings Account \n Family Account"<<endl;
cin>>accountObject.accountType;
//how to change data from file
inFile.seekp(-static_cast<streamoff>(sizeof(accountObject)), ios::cur);
inFile.write(reinterpret_cast<char *>(&accountObject), sizeof(accountObject));
}
}
cout<<"User Account Details have been changed successfully\n";
}
void accounts::displayDetails()
{
cout<<"Account Number : "<<accountNumber<<endl;
cout<<"Account User Name : "<<customerName<<endl;
cout<<"Account type : "<<accountType<<endl;
cout<<"Account Balance : "<<balance<<endl;
cout<<endl;
cout<<endl;
}
void accounts::closeAccount(int num)
{
// int accountNumber; int balance char accountType[20];
accounts accountsObject;
fstream inFile;
fstream outFile;
inFile.open("accountDetails.dat", ios::in | ios::binary);
if(!inFile)
{
cout<<"the file does not exist !!! Check another time\n";
}
//create a temp file for storing data without the user specified
outFile.open("temp.dat",ios::out | ios::binary);
while(inFile.read(reinterpret_cast<char *>(&accountsObject), sizeof(accounts)))
{
if(accountsObject.returnAccountNumber()!= num)
{
outFile.write((char *)(&accountsObject), sizeof(accounts));
}
}
inFile.close();
outFile.close();
remove("accountDetails.dat");
rename("temp.dat", "accountDetails.dat");
cout<<"Account has been succesfully removed\n";
cout<<endl;
}
class transaction : public accounts//inherit attributes and methods from accounts class
{
public:
//declare methods functions
void makeDeposit(int checkNumber);
void makeWithdraw(int checkNumber);
void getTransaction(int checkNumber);
};
void transaction::getTransaction(int checkNumber)
{
transaction transactionObject;
bool condition = false;
int prevBalance, currBalance;
int result;
fstream inFile;
inFile.open("accountDetails.dat", ios::in | ios::binary);//read data from original
fstream outFile;
outFile.open("transaction.dat",ios::app| ios::binary);//get details and compare specific changes
while(inFile.read(reinterpret_cast<char *>(&transactionObject), sizeof(transaction)))//copy first
{
outFile.write((char *)(&transactionObject), sizeof(transaction));
if (transactionObject.returnAccountNumber() == checkNumber)
{
currBalance = transactionObject.balance;
}
}
fstream readOut;
readOut.open("transaction.dat", ios::in | ios::binary);
while(readOut.read(reinterpret_cast<char *>(&transactionObject), sizeof(transaction)))
{
if (transactionObject.returnAccountNumber() == checkNumber)
{
prevBalance = transactionObject.balance;
}
}
if (prevBalance > currBalance)
{
result = prevBalance - currBalance;
cout<<"The user withdrew : "<<result<<endl;
}
else if (prevBalance < currBalance)
{
result = currBalance - prevBalance;
cout<<"The user deposited : "<<result<<endl;
}
outFile.close();
inFile.close();
}
void transaction::makeDeposit(int checkNumber)
{
transaction transactionObject;
int amount;
bool condition = false;
//check if account number exitst - read file and compare
fstream inFile;
inFile.open("accountDetails.dat", ios::in | ios::out | ios::binary);
//check existance of file
if (!inFile)
{
cout<<"Error !! No file was currently found, try again later\n";
}
while(inFile.read(reinterpret_cast<char *>(&transactionObject), sizeof(transaction)));
{
//multi-leve inheritance of user customerName//try
if (transactionObject.returnAccountNumber() == checkNumber)
{
condition = true;
cout<<"accountFound and proceed to deposit \n";
cout<<"The user account Name is : "<<transactionObject.customerName;
cout<<"Current user account balance : "<<transactionObject.balance<<endl;
cout<<"Enter the amount you wish to deposit \n";
cin>>amount;
transactionObject.balance = transactionObject.balance + amount;
cout<<"New user balance :"<<transactionObject.balance<<endl;
inFile.seekp(-static_cast<streamoff>(sizeof(transactionObject)), ios::cur);
inFile.write(reinterpret_cast<char *>(&transactionObject), sizeof(transaction));
}
}
if (!condition)
{
cout<<"No user account with that number\n";
}
}
void transaction::makeWithdraw(int checkNumber)
{
transaction transactionObject;
bool condition = false;
int amount, realNumber;
//check whether account exist
//read data from file for comparisons
fstream inFile;
inFile.open("accountDetails.dat", ios::in | ios::out | ios::binary);
//check for file existance
if (!inFile)
{
cout<<"Error!! the file does not exist\n";
}
while(inFile.read(reinterpret_cast<char *>(&transactionObject), sizeof(transaction)))
{
if (transactionObject.returnAccountNumber() == checkNumber)
{
condition = true;
cout<<"Account has been found\n";
cout<<"The user account Name is : "<<transactionObject.customerName<<endl;
cout<<"Current user account balance is : "<<transactionObject.balance<<endl;
cout<<"Enter amount to withdraw : \n";
cin>>amount;
if (amount < transactionObject.balance)
{
cout<<"You can proceed to withdraw \n";
transactionObject.balance = transactionObject.balance - amount;
cout<<"You have successfully withdrawn : "<<amount<<endl;
cout<<"Current balance : "<<transactionObject.balance<<endl;
inFile.seekp(-static_cast<streamoff>(sizeof(transactionObject)), ios::cur);
inFile.write(reinterpret_cast<char *>(&transactionObject), sizeof(transaction));
// realNumber = transactionObject.balance;
// cout<<"Test for real Number : "<<realNumber<<endl;
}
else
{
cout<<"You do not have the required funds to proceed with these transaction\n";
}
}
}
if(!condition)
{
cout<<"The account has not been found\n";
}
}
void writeAccount();
void closeAccount(int);
void displayAccountDetails(int);
int main()
{
transaction transactionObject;
accounts accountObject;
int choice;
//PREVENT READ CHARACTERS
while (choice != 8)
{
cout<<"WELCOME TO CRYPTO BANKING SYSTEM"<<endl;
cout<<"TO GET STARTED, CHOOSE ONE OF THE OPTIONS BELOW"<<endl;
cout<<"=================MAIN MENU=================="<<endl;
cout<<"1: CREATE ACCOUNT"<<endl;
cout<<"2: UPDATE ACCOUNT"<<endl;
cout<<"3: DISPLAY ACCOUNT"<<endl;
cout<<"4: WITHDRAW MONEY"<<endl;
cout<<"5: DEPOSIT MONEY"<<endl;
cout<<"6: CLOSE ACCOUNT"<<endl;
cout<<"7: GET TRANSACTION"<<endl;
cout<<"8: Exit"<<endl;
cout<<endl;
cout<<"Choose the options from the above list\n";
cin>>choice;
int accountNum;
switch (choice)
{
case 1:
system("clear");
writeAccount();//create account
break;
case 2:
system("clear");
cout<<"Enter account number\n";
cin>>accountNum;
accountObject.updateAccount(accountNum);//update account details
break;
case 3:
system("clear");
cout<<"Enter your account number\n";
cin>>accountNum;
displayAccountDetails(accountNum);//display user details
break;
case 4:
system("clear");
cout<<"Enter accout number\n";
cin>>accountNum;
transactionObject.makeWithdraw(accountNum);//withdraw function
break;
case 5:
system("clear");
cout<<"Enter account Number\n";
cin>>accountNum;
transactionObject.makeDeposit(accountNum);//deposit function
break;
case 6:
system("clear");
cout<<"Enter account number\n";
cin>>accountNum;
accountObject.closeAccount(accountNum);//close account
break;
case 7:
system("clear");
cout<<"Enter the account Number \n";
cin>>accountNum;
transactionObject.getTransaction(accountNum);
}
}
cout<<"Thank you for using Crypto Bank. We hope to see you soon\n";
return (0);
}
void writeAccount()
{
accounts accountObject;
fstream outFile;
outFile.open("accountDetails.dat", ios::app | ios::binary);
accountObject.getDetails();
accountObject.openAccount();
outFile.write(reinterpret_cast<char *>(&accountObject), sizeof(accounts));
outFile.close();
}
void displayAccountDetails(int num)
{
accounts accountsObject;
bool condition = false;
fstream inFile;
inFile.open("accountDetails.dat", ios::in | ios::binary);
if(!inFile)
{
cout<<"Error :: the file does not exist!! \n";
}
cout<<"BANK ACCOUNT DETAILS FOR : \n";
while(inFile.read(reinterpret_cast<char *>(&accountsObject), sizeof(accounts)))
{
if (accountsObject.returnAccountNumber() == num)
{
accountsObject.displayDetails();
condition = true;
}
}
inFile.close();
if (condition = false)
{
cout<<"The account number does not exist\n";
}
}