-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWallet.cpp
More file actions
85 lines (79 loc) · 1.72 KB
/
Copy pathWallet.cpp
File metadata and controls
85 lines (79 loc) · 1.72 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
#include "Wallet.h"
#include "CSVReader.h"
#include <string>
#include <iostream>
Wallet::Wallet() {}
void Wallet::insertCurrency(string type, double amount) {
double balance;
if (amount < 0)
{
throw exception{};
}
if (currencies.count(type) == 0)
{
balance = 0;
}
else {
balance = currencies[type];
}
balance += amount;
currencies[type] = balance;
}
bool Wallet::removeCurrency(string type, double amount) {
if (amount < 0)
{
return false;
}
if (currencies.count(type) == 0)
{
return false;
}
else {
if (containsCurrency(type, amount))
{
currencies[type] -= amount;
return true;
}
else
return false;
}
}
bool Wallet::containsCurrency(string type, double amount)
{
if (currencies.count(type) == 0)
return false;
else
return currencies[type] >= amount;
}
string Wallet::toString()
{
string s;
for (pair<string,double> pair : currencies)
{
string currency = pair.first;
double amount = pair.second;
s += currency + " : " + to_string(amount) + "\n";
}
return s;
}
bool Wallet::canFulfillOrder(OrderBookEntry order)
{
vector<string> currs = CSVReader::tokenize(order.product, '/');
// ask
if (order.orderType == OrderBookType::ask)
{
double amount = order.amount;
string currency = currs[0];
cout << "Wallet::canFulfillOrder " << currency << " : " << amount << endl;
return containsCurrency(currency, amount);
}
// bid
if (order.orderType == OrderBookType::bid)
{
double amount = order.amount * order.price;
string currency = currs[1];
cout << "Wallet::canFulfillOrder " << currency << " : " << amount << endl;
return containsCurrency(currency, amount);
}
return false;
}