-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank.cpp
107 lines (97 loc) · 3.24 KB
/
bank.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
#include "bank.h"
Bank::Bank(QString dbPath) : _db(DBController(dbPath)) {}
BankResponse<Account> Bank::getAccount(QString id)
{
try {
Account acc = _db.getAccount(id);
return BankResponse<Account>(acc);
} catch (DBController::NotFoundException &e) {
return BankResponse<Account>(NOT_FOUND);
}
}
BankResponse<ResponseStatus> Bank::createAccount(const Account &acc)
{
return _db.addAccount(acc) ? BankResponse<ResponseStatus>(ResponseStatus::OK) : BankResponse<ResponseStatus>(ResponseStatus::ERROR);
}
BankResponse<double> Bank::withdraw(QString id, const double amount)
{
if (amount <= 0)
return BankResponse<double>(ERROR);
try {
Account acc = _db.getAccount(id);
double bal = acc.balance();
if (QDateTime::currentDateTime().daysTo(acc.expirationDate()) <= 0)
return BankResponse<double>(EXPIRED);
if (acc.blocked())
return BankResponse<double>(BLOCKED);
if (bal < amount)
return BankResponse<double>(NOT_ENOUGH);
acc.setBalance(bal - amount);
_db.updateAccount(acc);
return BankResponse<double>(acc.balance());
} catch (DBController::NotFoundException &e) {
return BankResponse<double>(NOT_FOUND);
}
}
BankResponse<double> Bank::top_up(QString id, const double amount)
{
if (amount <= 0)
return BankResponse<double>(ERROR);
try {
Account acc = _db.getAccount(id);
if (QDateTime::currentDateTime().daysTo(acc.expirationDate()) <= 0)
return BankResponse<double>(EXPIRED);
if (acc.blocked())
return BankResponse<double>(BLOCKED);
double bal = acc.balance();
acc.setBalance(bal + amount);
_db.updateAccount(acc);
return BankResponse<double>(acc.balance());
} catch (DBController::NotFoundException &e) {
return BankResponse<double>(NOT_FOUND);
}
}
BankResponse<ResponseStatus> Bank::block(QString id)
{
try {
Account acc = _db.getAccount(id);
acc.setBlocked(true);
_db.updateAccount(acc);
return BankResponse<ResponseStatus>(OK);
} catch (DBController::NotFoundException &e) {
return BankResponse<ResponseStatus>(NOT_FOUND);
}
}
BankResponse<ResponseStatus> Bank::unblock(QString id)
{
try {
Account acc = _db.getAccount(id);
acc.setBlocked(false);
_db.updateAccount(acc);
return BankResponse<ResponseStatus>(OK);
} catch (DBController::NotFoundException &e) {
return BankResponse<ResponseStatus>(NOT_FOUND);
}
}
BankResponse<bool> Bank::checkPin(QString id, QString pin)
{
try {
Account acc = _db.getAccount(id);
return BankResponse<bool>(acc.pin() == pin);
} catch (DBController::NotFoundException &e) {
return BankResponse<bool>(NOT_FOUND);
}
}
BankResponse<ResponseStatus> Bank::setPin(QString id, QString newPin)
{
try {
Account acc = _db.getAccount(id);
if (acc.blocked())
return BankResponse<ResponseStatus>(BLOCKED);
acc.setPin(newPin);
_db.updateAccount(acc);
return BankResponse<ResponseStatus>(OK);
} catch (DBController::NotFoundException &e) {
return BankResponse<ResponseStatus>(NOT_FOUND);
}
}