This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.cpp
563 lines (466 loc) · 12.8 KB
/
transactions.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//#define _CRT_SECURE_NO_WARNINGS
//#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<cstring>
using namespace std;
#pragma region Structure Definition
struct Account
{
int index_number;
char account_id[5];
char name[20];
char password[10];
double balance;
};
struct TransactionRecord
{
int transaction_id;
char sender_account_id[5];
char receiver_account_id[5];
double amount;
};
#pragma endregion
#pragma region Function Definitions
void serialize_single_account(Account * account);
void serialize_single_transaction(TransactionRecord * transaction);
Account * deserialize_account();
TransactionRecord * deserialize_transactions();
void display_account(Account * account);
void accounts_list(Account * account);
void display_transactions(TransactionRecord * transactions);
void add_account();
void add_transaction();
void add_transaction_after_deserialize();
int set_transaction_id(int current_id);
int get_max_id();
char * get_id_by_name(char * name);
double get_balance_by_id(char * id);
bool main_menu();
void display_accounts_all_transactions(char * name);
void clients_transactions_menu();
double accounts_send_transactions_sum(char * account_id);
double accounts_recieve_transactions_sum(char * account_id);
const char * status_in_operation(char * account_id);
#pragma endregion
#pragma region Global Variables
const int ACCOUNT_MAX_SIZE = 50;
Account * accounts = new Account[ACCOUNT_MAX_SIZE];
const int TRANSACTION_MAX_SIZE = 100;
TransactionRecord * transaction_records = new TransactionRecord[TRANSACTION_MAX_SIZE];
int index = 1;
int transactions_id = 1;
#pragma endregion
#pragma region Main
int main()
{
bool check = true;
while (check)
{
check = main_menu();
}
//system("pause");
return 0;
}
#pragma endregion
#pragma region Function Implementation
void serialize_single_account(Account * account)
{
FILE * fp = fopen("Accounts", "ab");
int accout_size = sizeof(Account);
char * binary_data;
if (fp != NULL) {
binary_data = (char *)account;
for (int i = 0; i < accout_size; i++) {
putc(*binary_data++, fp);
}
fclose(fp);
}
cout << "File successfuly created" << endl;
}
void serialize_single_transaction(TransactionRecord * transaction)
{
FILE * fp = fopen("Transactions", "ab");
int transactions_size = sizeof(TransactionRecord);
char * binary_data;
if (fp != NULL)
{
binary_data = (char *)transaction;
for (int i = 0; i < transactions_size; i++)
{
putc(*binary_data++, fp);
}
fclose(fp);
}
}
Account * deserialize_account()
{
FILE * fp = fopen("Accounts", "rb");
char * binary_data;
int iterator = 0;
Account * ptr = (Account *)malloc(sizeof(Account) * ACCOUNT_MAX_SIZE);
binary_data = (char *)ptr;
while ((iterator = getc(fp)) != EOF)
{
*binary_data = iterator;
binary_data++;
}
fclose(fp);
return ptr;
}
TransactionRecord * deserialize_transactions()
{
FILE * fp = fopen("Transactions", "rb");
char * binary_data;
int iterator = 0;
TransactionRecord * ptr = (TransactionRecord *)malloc(sizeof(TransactionRecord) * TRANSACTION_MAX_SIZE);
binary_data = (char *)ptr;
while ((iterator = getc(fp)) != EOF)
{
*binary_data = iterator;
binary_data++;
}
fclose(fp);
return ptr;
}
void display_account(Account * account)
{
account = deserialize_account();
for (int i = 0; i < account->index_number; i++)
{
cout << "ID: " << account->account_id << endl;
cout << "NAME: " << account->name << endl;
cout << "PASSWORD: " << account->password << endl;
cout << "BALANCE: " << account->balance << endl;
cout << "=========================" << endl;
account++;
}
}
void accounts_list(Account * account)
{
account = deserialize_account();
for (int i = 0; i < account->index_number; i++)
{
cout << account->index_number << ") ID: " << account->account_id << " | NAME: " << account->name << endl;
account++;
}
}
void display_transactions(TransactionRecord * transactions)
{
transactions = deserialize_transactions();
for (int i = 0; i < TRANSACTION_MAX_SIZE; i++)
{
if (transactions->transaction_id > 0)
{
cout << "TRANSACTION ID: " << transactions->transaction_id << endl;
cout << "SENDER ID: " << transactions->sender_account_id << endl;
cout << "RECEIVER ID: " << transactions->receiver_account_id << endl;
cout << "AMOUNT: " << transactions->amount << endl;
cout << "==========================" << endl;
transactions++;
}
else
{
return;
}
}
}
void add_account()
{
cout << "Enter the following data to create a new account" << endl;
while (true)
{
accounts->index_number = index;
index++;
cout << "Enter ID: ";
cin >> accounts->account_id;
cout << "Enter NAME: ";
cin >> accounts->name;
cout << "Enter PASSWORD: ";
cin >> accounts->password;
cout << "Enter INITIAL VALUE: ";
cin >> accounts->balance;
serialize_single_account(accounts);
cout << "Do you want to add another account? [Y] - YES, [N] - NO" << endl;
char command;
cin >> command;
if (command == 'Y' || command == 'y')
{
display_account(accounts);
}
else if (command == 'N' || command == 'n')
{
cout << "Return to MAIN MENU >>>" << endl;
break;
}
else
{
cout << "Wrong command code. Return to MAIN MENU >>>" << endl;
break;
}
}
}
void add_transaction()
{
TransactionRecord * transaction = new TransactionRecord();
accounts = deserialize_account();
cout << "Clients list: " << endl;
accounts_list(accounts);
cout << "Enter the following data to create a new transaction" << endl;
while (true)
{
transaction_records->transaction_id = transactions_id;
transactions_id++;
cout << "Enter SENDER ID: ";
cin >> transaction_records->sender_account_id;
cout << "Enter RECEIVER ID: ";
cin >> transaction_records->receiver_account_id;
cout << "Enter AMOUNT: ";
cin >> transaction_records->amount;
double sender_balance = get_balance_by_id(transaction_records->sender_account_id);
double receiver_balance = get_balance_by_id(transaction_records->receiver_account_id);
sender_balance -= transaction_records->amount;
receiver_balance += transaction_records->amount;
cout << "OK, transaction successfully completed" << endl;
serialize_single_transaction(transaction_records);
serialize_single_account(accounts);
cout << "Do you want to add another transaction? [Y] - YES, [N] - NO" << endl;
char command;
cin >> command;
if (command == 'N' || command == 'n')
{
cout << "OK, OPERATION COMPLETED" << endl;
break;
}
else if (command == 'Y' || command == 'y')
{
display_transactions(transaction_records);
}
else
{
cout << "WRONG COMMAND CODE, YOU SHALL NOT PASS!" << endl;
}
}
}
void add_transaction_after_deserialize()
{
transaction_records = deserialize_transactions();
accounts = deserialize_account();
cout << "Clients list: " << endl;
accounts_list(accounts);
cout << "Enter the following data to create a new transaction" << endl;
while (true)
{
transaction_records->transaction_id = set_transaction_id(transactions_id);
transactions_id++;
cout << "Enter SENDER ID: ";
cin >> transaction_records->sender_account_id;
cout << "Enter RECEIVER ID: ";
cin >> transaction_records->receiver_account_id;
cout << "Enter AMOUNT: ";
cin >> transaction_records->amount;
double sender_balance = get_balance_by_id(transaction_records->sender_account_id);
double receiver_balance = get_balance_by_id(transaction_records->receiver_account_id);
sender_balance -= transaction_records->amount;
receiver_balance += transaction_records->amount;
cout << "OK, transaction successfully completed" << endl;
serialize_single_transaction(transaction_records);
serialize_single_account(accounts);
cout << "Do you want to add another transaction? [Y] - YES, [N] - NO" << endl;
char command;
cin >> command;
if (command == 'N' || command == 'n')
{
cout << "OK, OPERATION COMPLETED" << endl;
break;
}
else if (command == 'Y' || command == 'y')
{
display_transactions(transaction_records);
}
else
{
cout << "WRONG COMMAND CODE, YOU SHALL NOT PASS!" << endl;
}
}
}
int get_max_id()
{
TransactionRecord * transaction_records = deserialize_transactions();
int max_id = INT_MIN;
for (int i = 0; i < TRANSACTION_MAX_SIZE; i++)
{
if (transaction_records->transaction_id > max_id)
{
max_id = transaction_records->transaction_id;
}
transaction_records++;
}
return max_id;
}
int set_transaction_id(int current_id)
{
int id = get_max_id();
if (transactions_id == 1)
{
return ++id;
}
else
{
return current_id;
}
}
char * get_id_by_name(char * name)
{
accounts = deserialize_account();
string help;
for (int i = 0; i < ACCOUNT_MAX_SIZE; i++)
{
help = accounts->name;
if (help == name)
{
return accounts->account_id;
}
accounts++;
}
}
double get_balance_by_id(char * id)
{
accounts = deserialize_account();
string help;
for (int i = 0; i < ACCOUNT_MAX_SIZE; i++)
{
help = accounts->account_id;
if (help == id)
{
return accounts->balance;
}
accounts++;
}
}
double accounts_send_transactions_sum(char * account_id)
{
TransactionRecord * transaction_records = deserialize_transactions();
int size = get_max_id();
double sender_sum = 0;
for (int i = 0; i < size; i++)
{
string sender_id = transaction_records->sender_account_id;
if (account_id == sender_id)
{
sender_sum += transaction_records->amount;
}
transaction_records++;
}
return sender_sum;
}
double accounts_recieve_transactions_sum(char * account_id)
{
TransactionRecord * transaction_records = deserialize_transactions();
int size = get_max_id();
double receiver_sum = 0;
for (int i = 0; i < size; i++)
{
string receiver_id = transaction_records->receiver_account_id;
if (account_id == receiver_id)
{
receiver_sum += transaction_records->amount;
}
transaction_records++;
}
return receiver_sum;
}
const char * status_in_operation(char * account_id)
{
TransactionRecord * transaction_records = deserialize_transactions();
string checking_id = account_id;
if (checking_id == transaction_records->receiver_account_id)
{
return "Receiver";
}
else
{
return "Sender";
}
}
void display_accounts_all_transactions(char * name)
{
TransactionRecord * transaction_records = deserialize_transactions();
char * id = new char[5];
id = get_id_by_name(name);
string compare_id = id;
cout << "Below are all " << name << "'s transactions" << endl;
double initial_balance = get_balance_by_id(get_id_by_name(name));
double balance = initial_balance - accounts_send_transactions_sum(id)+
accounts_recieve_transactions_sum(id);
cout << name << "'s initial balance: " << initial_balance << endl;
cout << name << "'s current balance: " << balance << endl;
for (int i = 0; i < TRANSACTION_MAX_SIZE; i++)
{
if (transaction_records->transaction_id > 0)
{
if (compare_id == transaction_records->sender_account_id ||
compare_id == transaction_records->receiver_account_id)
{
cout << "Transaction's id: " << transaction_records->transaction_id << endl;
cout << "Status in operation: " << status_in_operation(id) << endl;
cout << "Transaction's amount: " << transaction_records->amount << endl;
cout << "=================================" << endl;
}
transaction_records++;
}
else
{
return;
}
}
}
void clients_transactions_menu()
{
cout << "Enter the account's name: ";
char name[25];
cin >> name;
display_accounts_all_transactions(name);
}
bool main_menu()
{
cout << "--------MAIN MENU--------" << endl;
cout << "Choose command code" << endl;
cout << "1 - Add a new client (with serialization)" << endl;
cout << "2 - Display a clients base (deserialization)" << endl;
cout << "3 - Add a new transaction (with serialization)" << endl;
cout << "4 - Add a new transaction after closing program" << endl;
cout << "5 - Display a transactioins base (deserializtion)" << endl;
cout << "6 - Display client's all transactions info" << endl;
cout << "0 - Exit" << endl;
int command;
cout << "Your command code: ";
cin >> command;
switch (command)
{
case 0:
return false;
case 1:
add_account();
break;
case 2:
accounts_list(accounts);
break;
case 3:
add_transaction();
break;
case 4:
add_transaction_after_deserialize();
break;
case 5:
display_transactions(transaction_records);
break;
case 6:
clients_transactions_menu();
break;
default:
break;
}
}
#pragma endregion