-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.cpp
500 lines (450 loc) · 12 KB
/
account.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
#include "account.h"
#include "saving.h"
#include "card.h"
#include "transaction.h"
#include"datas.h"
#include <ostream>
#include <QDate>
#include <QString>
#include <QRandomGenerator>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
account::account()
{
}
account::account(int _typeOfAccount, QString _num, QDate _date, int _balance, int _status, QJsonArray _allUsres)
{
this->balance = _balance;
this->num = _num;
this->typeOfAccount = _typeOfAccount;
this->date = _date;
this->status = _status;
this->accCard=nullptr;
for(int i = 0; i < _allUsres.size(); i++)
this->allUsers.push_back(_allUsres[i].toString());
}
/*
account::account(int _typeOfAccount,int _balance)
{
this->typeOfAccount = _typeOfAccount;
this->balance = _balance;
status=0;
setDate();
setNum();
}
void account::moneyTransfer(QString fr, QString to, int trans_amount)
{
//TO DO: check existance of "fr" and "to" and also check balance of fr to reduce money from in files
// TO DO: note that if typOfAccount = 3(longTerm) update typOfAccount to 1(shortTermPersonal)
this->balance = balance - trans_amount;
transaction temp(fr,to,trans_amount);
transactions.append(temp);
//TO DO: write transcation to file
accCard=nullptr;
}*/
int account::transfer(QString to, int trans_amount)
{
for (int i = 0; i < datas::getUsers().size(); i++)
{
for(int j = 0; j < datas::getUsers()[i].getAccounts().size(); j++)
{
if(datas::getUsers()[i].getAccounts()[j].getNum() == to && datas::getUsers()[i].getAccounts()[j].getBalance() < trans_amount)
{
//QMessageBox msgBox_balance;
//msgBox_balance.setText("موجودی کافی نیست");
//msgBox_balance.setInformativeText("لطفا حساب خود را شارژ نمایید، سپس مجددا تلاش کنید")
//msgBox_balance.exec();
return 0;
}
if(datas::getUsers()[i].getAccounts()[j].getNum() == to && datas::getUsers()[i].getAccounts()[j].getBalance() >= trans_amount)
{
this->balance = getBalance() - trans_amount;
datas::getUsers()[i].getAccounts()[j].balance = datas::getUsers()[i].getAccounts()[j].getBalance() + trans_amount;
if(this->typeOfAccount == 3)
{
this->typeOfAccount = 1;
}
this->addTransaction(transaction(this->getNum(), to, trans_amount, 0, 0));
datas::getUsers()[i].getAccounts()[j].addTransaction(transaction(this->getNum(), to, trans_amount, 0, 1));
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
if(datas::getUsers()[i].getAccounts()[j].getType()==2)
{
datas::getUsers()[i].getAccounts()[j].updateAllUsers();
}
//QMessageBox msgBox_transaction;
//msgBox_transaction.setText("تراکنش با موفقیت انجام شد");
//msgBox_transaction.exec();
//SENDING EMAIL!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return 1;
}
}
}
return 2;
}
int account::getBalance()
{
return balance;
}
/*
QVector<transaction> account::getTransaction()
{
//TO DO: read from file: read last 10 transactions
QVector<transaction>::iterator ptr;
for (ptr = transactions.begin(); ptr < transactions.end(); ptr++)
{
//TO DO: print last 10 transactions
}
return transactions; //////not complit
}
*/
void account::setStatus(int _status)
{
this->status = _status;
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
void account::setNum()
{
std::uniform_int_distribution<qlonglong> AccNumDistribution(10000000000000, 99999999999999);
qlonglong value = AccNumDistribution(*QRandomGenerator::global());
QString _num = QString::number(value);
this->num=_num;
}
void account::setDate()
{
QDate _date = QDate::currentDate();
this->date=_date;
}
QString account::getTypeAsString()
{
if(typeOfAccount==0)
{
return "حساب سپرده قرض الحسنه پس انداز";
}
else if(typeOfAccount==1)
{
return "حساب سپرده سرمایه گذاری کوتاه مدت";
}
else if(typeOfAccount==2)
{
return "حساب سپرده سرمایه گذاری حقوقی";
}
else if(typeOfAccount == 3)
{
return "حساب سپرده گذاری بلند مدت";
}
else
return "";
}
QString account::getBalanceAsString()
{
QString stringBalance = QString::number(balance);
return stringBalance;
}
QString account::getStatusAsString()
{
if(status==0)
{
return "تایید نشده";
}
else if(status==1)
{
return "تایید شده";
}
else if(status==2)
{
return "مسدود شده";
}
else
{
return "";
}
}
bool account::hasCard()
{
if(accCard==nullptr)
{
return 0;
}
else
{
return 1;
}
}
int account::getStatus()
{
return status;
}
int account::getType()
{
return typeOfAccount;
}
/*
int account::transfer(QString _toAccount, int _value, QString _password, QString _cvv2, QString _month, QString _year)
{
return 0;
}
*/
QVector<transaction> &account::gettransaction()
{
return transactions;
}
void account::getCard()
{
this->setCardInfo(card());
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
QString account::generatePassword()
{
std::uniform_int_distribution<int> PassDistribution(100000, 999999);
int pasval = PassDistribution(*QRandomGenerator::global());
QString _password = QString::number(pasval);
setPsswordInAccount(_password);
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
datas::writeToFile();
return _password;
}
void account::setPsswordInAccount(QString new_generated_password)
{
this->accCard->setPassword(new_generated_password);
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
/*QString account::generatePassword()
{
return "125689";
}*/
account::account(int _typeOfAccount, int _balance,QVector<QString> _allusers)
{
this->typeOfAccount = _typeOfAccount;
this->balance = _balance;
setDate();
setNum();
status=0;
allUsers=_allusers;
accCard=nullptr;
}
/*
void account::moneyTransfer(QString to, int trans_amount)
{
//TO DO: check existance of "fr" and "to" and also check balance of fr to reduce money from in files
// TO DO: note that if typOfAccount = 3(longTerm) update typOfAccount to 1(shortTermPersonal)
//this->balance = balance - trans_amount;
//transaction temp(fr, to, trans_amount, withCard, receive);
//transactions.append(temp);
//TO DO: write transcation to file
}
*/
int account::getTypeOfAccount() const
{
return typeOfAccount;
}
QString account::getNum() const
{
return num;
}
QString account::getDateString() const
{
return date.toString("yyyy-MM-dd");
}
int account::getStatus() const
{
return status;
}
QVector<QString> account::getAllUsers() const
{
return allUsers;
}
card account::getAccCard() const
{
return *(accCard);
}
QVector<transaction> account::getTransaction()
{
return transactions;
}
bool account::operator<(const account &a2) const
{
return this->num < a2.num;
}
void account::setCardInfo(card _card)
{
this->accCard = new card(_card.getNumber(), _card.getPassword(), _card.getCvv2(), _card.getDate(), _card.getStatus());
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
void account::addTransaction(transaction _transaction)
{
this->transactions.push_back(_transaction);
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
QDate account::getDate() const
{
return date;
}
card *account::getaccountcard()
{
return accCard;
}
void account::block()
{
status=2;
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
void account::unblock()
{
status=1;
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
/*void account::profitability()
{
if(this->typeOfAccount == 1)
{
//QDate dlast = getLastLog();
QDate d1 = getDate();
QDate d2 = QDate::currentDate();
int duration = d1.daysTo(d2);
int dur_last = dlast.daysTo(d2);
int md = dur_last / 30;
if(duration == 30)
{
int profit = balance * (0.05);
this->balance = balance + profit;
}
else if(duration > 30)
{
//int profit = balance * (1.05 pwd(md));
this->balance = balance + profit;
}
}
if(this->typeOfAccount == 3)
{
QDate dlast = getLastLog();
QDate d1 = getDate();
QDate d2 = QDate::currentDate();
int duration = d1.daysTo(d2);
int dur_last = dlast.daysTo(d2);
int md = dur_last / 90;
if(duration == 90)
{
int profit = balance * (0.15);
this->balance = balance + profit;
}
else if(duration > 90)
{
int profit = balance * (0.15 * md);
this->balance = balance + profit;
}
}
}
*/
void account::deletecard()
{
delete accCard;
accCard=nullptr;
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
void account::profitability()
{
if(this->typeOfAccount == 1)
{
//QDate d1 = getDate();
QDate d1 = QDate::currentDate();
QDate& d2 = datas::getLastUse();
int duration = d2.daysTo(d1);
int md = duration / 30;
//int duration = d2.daysTo(d3);
if(duration >= 30)
{
this->balance = balance * (pow(1.05,md));
}
else if(duration < 30)
{
return;
}
}
if(this->typeOfAccount == 3)
{
//QDate d1 = getDate();
QDate d1 = QDate::currentDate();
QDate d2 = datas::getLastUse();
qDebug()<< d2;
int duration = d2.daysTo(d1);
int md = duration / 90;
//int duration = d2.daysTo(d3);
if(duration >= 90)
{
this->balance = balance * (pow(1.15,md));
}
else if(duration < 90)
{
return;
}
}
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
void account::updateAllUsers()
{
for (int i = 0; i < datas::getUsers().size(); i++)
{
for(int j = 0; j < datas::getUsers()[i].getUsername().size(); j++)
{
if(this->allUsers[j] == datas::getUsers()[i].getUsername())
{
user& tmpuser=datas::getUsers()[i];
for(int u=0;u<tmpuser.getaccounts().size();u++)
{
if(this->num == tmpuser.getaccounts()[u].getNum())
{
account& ref=datas::getAccountRefByUandI(i,u);
ref=*this;
}
}
}
}
}
}
void account::noCard()
{
this->accCard = nullptr;
if(this->typeOfAccount==2)
{
this->updateAllUsers();
}
}
card *account::getCardInfo() const
{
return accCard;
}