-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.cpp
More file actions
344 lines (319 loc) · 12.3 KB
/
Bank.cpp
File metadata and controls
344 lines (319 loc) · 12.3 KB
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
//
// Created by Luca Teodorescu on 20.03.2026.
//
#include "Bank.h"
#include "Account.h"
#include "ExchangeBuffer.h"
#include <iostream>
#include <set>
void Bank::BackgroundFetcher() {
vector<pair<string, string>> currencies = {
{"EUR", "RON"}, {"USD", "RON"}, {"GBP", "RON"}, {"CHF", "RON"}
};
while (running.load(memory_order_acquire)) {
for (auto currency : currencies) {
ExchangeBuffer::RateUpdate update;
update.from = currency.first;
update.to = currency.second;
double random_number = double(rand()) / double(RAND_MAX);
update.rate = random_number;
if (!exchangeBuffer.InsertProducer(update)) {
break;
}
}
this_thread::sleep_for(chrono::seconds(5));
}
}
void Bank::RefreshRates() {
while (exchangeBuffer.PopConsumer(this)) {
}
}
void Bank::BackgroundAuditConsumer() {
while (running.load(std::memory_order_acquire)) {
if (!auditBuffer.PopConsumer(this)) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
}
Bank::Bank() : exchangeBuffer(this, 1024), auditBuffer(this, 1024) {
running.store(true, std::memory_order_release);
rateUpdateThread = thread(&Bank::BackgroundFetcher, this);
auditThread = std::thread(&Bank::BackgroundAuditConsumer, this);
}
Bank::~Bank() {
running.store(false, std::memory_order_release);
if (rateUpdateThread.joinable()) {
rateUpdateThread.join();
}
if (auditThread.joinable()) {
auditThread.join();
}
for (auto it = AllUsers.begin(); it != AllUsers.end(); it++) {
delete it->second;
}
AllUsers.clear();
GraphUsers.clear();
}
void Bank::CreateUser(const string &FirstName, const string &LastName, const string &CNP, const string &BirthDate) {
if (AllUsers.find(CNP) != AllUsers.end()) {
cout << "Eroare: Utilizatorul cu CNP-ul " << CNP << " exista deja!" << '\n';
return;
}
User* new_client = new User(FirstName, LastName, CNP, BirthDate);
AllUsers[CNP] = new_client;
}
void Bank::CreateUser(const string &FirstName, const string &LastName, const string &CNP, const string &BirthDate, const string ¤cy) {
if (AllUsers.find(CNP) != AllUsers.end()) {
cout << "Eroare: Utilizatorul cu CNP-ul " << CNP << " exista deja!" << '\n';
return;
}
User* new_client = new User(FirstName, LastName, CNP, BirthDate, currency);
AllUsers[CNP] = new_client;
}
void Bank::DeleteUser(const string &FirstName, const string &LastName, const string &CNP) {
auto it = AllUsers.find(CNP);
if (it != AllUsers.end()) {
User *client = it->second;
if (GraphUsers.contains(CNP)) {
for (User* prieten : GraphUsers[CNP]) {
string prieten_cnp = prieten->GetCNP();
GraphUsers[prieten_cnp].erase(client);
prieten->EraseFriend(CNP);
}
GraphUsers.erase(CNP);
}
delete client;
AllUsers.erase(it);
cout << "Utilizatorul cu CNP-ul " << CNP << " a fost sters cu succes." << '\n';
}
else {
cout << "Invalid CNP" << '\n';
}
}
void Bank::CreateAccount(const string &CNP, const string ¤cy) {
if (AllUsers.find(CNP) != AllUsers.end()) {
User* client = AllUsers[CNP];
client->AddAccount(currency);
}
else {
cout << "Invalid CNP" << '\n';
}
}
void Bank::DeleteAccount(const string &CNP, const string &IBAN) {
auto it = AllUsers.find(CNP);
if (it != AllUsers.end()) {
User *client = it->second;
client->DeleteAccount(IBAN);
cout << "Solicitarea de stergere pentru contul " << IBAN << " a fost trimisa catre client." << '\n';
}
else {
cout << "Eroare: Invalid CNP. Utilizatorul nu a fost gasit!" << '\n';
}
}
void Bank::CreateWallet(const string &FirstName, const string &LastName, const string &CNP, const string &BirthDate, const string &IBAN) {
if (AllUsers.find(CNP) != AllUsers.end()) {
User* client = AllUsers[CNP];
client->AddWallet(IBAN);
cout << "Portofelul a fost adaugat cu succes in contul " << IBAN << '\n';
}
else {
User* client = new User(FirstName, LastName, CNP, BirthDate);
AllUsers[CNP] = client;
cout << "Clientul nu exista. I s-a creat un cont" << '\n';
}
}
void Bank::DeleteWallet(const string &CNP, const string &IBAN, const string &WalletID) {
if (AllUsers.find(CNP) != AllUsers.end()) {
User *client = AllUsers[CNP];
client->DeleteWallet(IBAN, WalletID);
}
else {
cout << "Invalid CNP" << '\n';
}
}
bool Bank::Deposit(const string &CNP, const string &IBAN, const string &WalletID, const double &amount, const string ¤cy) {
RefreshRates();
if (AllUsers.find(CNP) != AllUsers.end()) {
User *client = AllUsers[CNP];
Account *account = client->GetAccount(IBAN);
if (account == nullptr) {
cout << "IBAN invalid" << '\n';
return false;
}
Wallet *wallet = account->GetWallet(WalletID);
if (wallet == nullptr) {
cout << "WalletID invalid" << '\n';
return false;
}
string wallet_currency = wallet->GetCurrency();
double ExchangeRate = this->GetExchangeRates(currency, wallet_currency);
wallet->Deposit(amount * ExchangeRate);
cout << "Depunere reusita! S-au adaugat " << amount * ExchangeRate << " " << wallet_currency << " in cont." << '\n';
return true;
}
else {
cout << "CNP invalid" << '\n';
return false;
}
}
bool Bank::Withdraw(const string &CNP, const string &IBAN, const string &WalletID, const double &amount, const string ¤cy) {
RefreshRates();
if (AllUsers.find(CNP) != AllUsers.end()) {
User *client = AllUsers[CNP];
Account *account = client->GetAccount(IBAN);
if (account == nullptr) {
cout << "IBAN invalid" << '\n';
return false;
}
Wallet *wallet = account->GetWallet(WalletID);
if (wallet == nullptr) {
cout << "WalletID invalid" << '\n';
return false;
}
string wallet_currency = wallet->GetCurrency();
double ExchangeRate = this->GetExchangeRates(currency, wallet_currency);
if (wallet->GetBalance() < ExchangeRate * amount) {
cout << "Fonduri insuficiente" << '\n';
return false;
}
wallet->Withdraw(amount * ExchangeRate);
cout << "Retragere reusita! S-au extras " << amount * ExchangeRate << " " << wallet_currency << " in cont." << '\n';
return true;
}
else {
cout << "CNP invalid" << '\n';
return false;
}
}
void Bank::ChangeExchangeRates(const string &first_currency, const string &second_currency, const double &amount) {
ExchangeRates[{first_currency, second_currency}] = amount;
ExchangeRates[{second_currency, first_currency}] = 1.0 / amount;
}
double Bank::GetExchangeRates(const string &first_currency, const string &second_currency) {
if (first_currency == second_currency) {
return 1.0;
}
auto it = ExchangeRates.find({first_currency, second_currency});
if (it != ExchangeRates.end()) {
return it->second;
}
else {
cout << "Eroare. Nu exista curs valutar intre " << first_currency << "si " << second_currency << "." << '\n';
return 0.0;
}
}
void Bank::Transfer(const string &Send_CNP, const string &Receive_CNP, const string &Send_IBAN, const string &Receive_IBAN, const string &Send_WalletID, const string &Receive_WalletID, const double &amount, const string ¤cy) {
RefreshRates();
bool withdraw = this->Withdraw(Send_CNP, Send_IBAN, Send_WalletID, amount, currency);
if (withdraw) {
bool deposit = this->Deposit(Receive_CNP, Receive_IBAN, Receive_WalletID, amount, currency);
if (deposit) {
cout << "Tranzactia a fost acceptata" << '\n';
AuditBuffer::USER tx;
tx.SenderCNP = Send_CNP;
tx.SenderIBAN = Send_IBAN;
tx.SenderWalletID = Send_WalletID;
tx.ReceiverCNP = Receive_CNP;
tx.ReceiverIBAN = Receive_IBAN;
tx.ReceiverWalletID = Receive_WalletID;
tx.amount = amount;
if (!auditBuffer.InsertProducer(tx)) {
cout << "[Audit System] Buffer plin, se auto-redimensioneaza in fundal..." << '\n';
}
}
else {
cout << "Eroare la depunere" << '\n';
this->Deposit(Send_CNP, Send_IBAN, Send_WalletID, amount, currency);
}
}
else {
cout << "Transfer anulat: Tranzactia de retragere a esuat" << '\n';
}
}
void Bank::SendFriendInvitation(const string &Send_CNP, const string &Receive_CNP) {
if (Send_CNP != Receive_CNP) {
if (AllUsers.find(Send_CNP) != AllUsers.end() and AllUsers.find(Receive_CNP) != AllUsers.end()) {
User *client = AllUsers[Receive_CNP];
client->SendFriendInvitation(Send_CNP);
}
}
else {
cout << "Invalid Send Invitation" << '\n';
}
}
void Bank::AcceptFriendInvitation(const string &Send_CNP, const string &Receive_CNP) {
if (Send_CNP != Receive_CNP) {
if (AllUsers.find(Send_CNP) != AllUsers.end() and AllUsers.find(Receive_CNP) != AllUsers.end()) {
User *first_client = AllUsers[Receive_CNP];
if (first_client->AcceptFriendInvitation(Send_CNP)) {
User *second_client = AllUsers[Receive_CNP];
first_client->AddFriend(Receive_CNP);
second_client->AddFriend(Receive_CNP);
GraphUsers[Send_CNP].insert(first_client);
User *SendClient = AllUsers[Send_CNP];
GraphUsers[Receive_CNP].insert(SendClient);
}
}
}
else {
cout << "Invalid Accept Invitation" << '\n';
}
}
void Bank::DeleteFriend(const string &Send_CNP, const string &Receive_CNP) {
if (Send_CNP != Receive_CNP) {
if (AllUsers.find(Send_CNP) != AllUsers.end() and AllUsers.find(Receive_CNP) != AllUsers.end()) {
User *first_client = AllUsers[Send_CNP];
if (first_client->CheckFriend(Receive_CNP)) {
User *second_client = AllUsers[Receive_CNP];
first_client->EraseFriend(Receive_CNP);
second_client->EraseFriend(Send_CNP);
GraphUsers[Send_CNP].erase(second_client);
GraphUsers[Receive_CNP].erase(first_client);
}
}
}
else {
cout << "Invalid User" << '\n';
}
}
bool Bank::DFSCycleCheck(const string ¤t, const string &target, double amount, set<string> &visited) {
if (current == target) {
return true;
}
visited.insert(current);
for (const auto &edge : this->TransactionGraph[current]) {
if (edge.amount == amount) {
if (visited.find(edge.receiverCNP) == visited.end()) {
if (DFSCycleCheck(edge.receiverCNP, target, amount, visited)) {
return true;
}
}
}
}
return false;
}
void Bank::AddTransferVerification(const AuditBuffer::USER &userAudit, const bool &transfer) {
if (transfer) {
std::lock_guard<std::mutex> lock(this->graphMutex);
AuditEdge edge;
edge.receiverCNP = userAudit.ReceiverCNP;
edge.amount = userAudit.amount;
this->TransactionGraph[userAudit.SenderCNP].push_back(edge);
std::cout << "S-a adaugat muchia: "
<< userAudit.SenderCNP << " -> " << userAudit.ReceiverCNP
<< " cu suma: " << userAudit.amount << '\n';
}
}
bool Bank::VerifyTransaction(const AuditBuffer::USER &userAudit) {
std::lock_guard<std::mutex> lock(this->graphMutex);
std::cout << "Se analizeaza potentialul fraudulos pentru: "
<< userAudit.SenderCNP << " -> " << userAudit.ReceiverCNP << " (" << userAudit.amount << ")" << '\n';
std::set<std::string> visited;
if (DFSCycleCheck(userAudit.ReceiverCNP, userAudit.SenderCNP, userAudit.amount, visited)) {
std::cout << "S-a detectat un pattern circular de tranzactionare! "
<< "Suma de " << userAudit.amount << " RON se invarte in cerc." << '\n';
return false;
}
std::cout << "Nu s-a detectat niciun pattern circular pentru aceasta suma." << '\n';
return true;
}