-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet.cpp
More file actions
97 lines (83 loc) · 2.8 KB
/
Copy pathwallet.cpp
File metadata and controls
97 lines (83 loc) · 2.8 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
#include "wallet.h"
#include <stdexcept>
#include "csv_reader.h"
wallet::wallet() {}
void wallet::insert_currency(std::string type, double amount) {
double balance;
if (amount < 0) {
throw std::invalid_argument("Amount cannot be negative");
}
if (!this->currencies.contains(type)) {
balance = 0;
} else {
balance = this->currencies[type];
}
balance += amount;
this->currencies[type] = balance;
}
bool wallet::remove_currency(std::string type, double amount) {
double balance;
if (amount < 0) {
throw std::invalid_argument("Amount cannot be negative");
return false;
}
if (currencies.count(type) == 0) {
return false;
}
if (this->contains_currency(type, amount)) {
this->currencies[type] -= amount;
return true;
}
return false;
}
bool wallet::contains_currency(std::string type, double amount) {
if (!this->currencies.contains(type)) {
return false;
} else {
return this->currencies[type] >= amount;
}
}
std::string wallet::to_string() {
std::string s;
for (std::pair<std::string, double> pair: currencies) {
std::string currency = pair.first;
double amount = pair.second;
s += currency + " : " + std::to_string(amount) + "\n";
}
return s;
}
bool wallet::can_fulfill_order(const order_book_entry &order) {
std::vector<std::string> currs = csv_reader::tokenise(order.product, '/');
// ask
if (order.order_type == order_book_type::ask) {
const double amount = order.amount;
const std::string currency = currs[0];
return this->contains_currency(currency, amount);
}
// bid
if (order.order_type == order_book_type::bid) {
const double amount = order.amount * order.price;
const std::string currency = currs[1];
return this->contains_currency(currency, amount);
}
return false;
}
void wallet::process_sale(const order_book_entry &sale) {
std::vector<std::string> currs = csv_reader::tokenise(sale.product, '/');
if (sale.order_type == order_book_type::asksale) {
double outgoing_amount = sale.amount;
std::string outgoing_currency = currs[0];
double incoming_amount = sale.amount * sale.price;
std::string incoming_currency = currs[1];
currencies[incoming_currency] += incoming_amount;
currencies[outgoing_currency] -= outgoing_amount;
}
if (sale.order_type == order_book_type::bidsale) {
double outgoing_amount = sale.amount * sale.price;
double incoming_amount = sale.amount;
std::string outgoing_currency = currs[1];
std::string incoming_currency = currs[0];
currencies[incoming_currency] += incoming_amount;
currencies[outgoing_currency] -= outgoing_amount;
}
}