-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitcoinExchange.cpp
More file actions
248 lines (227 loc) · 6.15 KB
/
Copy pathBitcoinExchange.cpp
File metadata and controls
248 lines (227 loc) · 6.15 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
#include "BitcoinExchange.hpp"
#include "Defines.hpp"
#include <bits/stdc++.h>
BitcoinExchange::BitcoinExchange (int argc, char **argv)
{
if (argc != 2)
throw std::runtime_error ("invalid number of arguments");
std::ifstream file (argv[1]);
if (file.is_open () == false)
throw std::runtime_error ("unable to open input file");
loadDatabase ("data.csv");
printData (file);
}
void
BitcoinExchange::loadDatabase (std::string filename)
{
std::ifstream file (filename.c_str ());
std::string line;
if (file.is_open ())
{
size_t i, lineNumber = 0;
while (std::getline (file, line))
{
lineNumber++;
trim (line);
if (line == "date,exchange_rate")
continue;
validadeDatabaseLine (line, lineNumber);
i = line.find (",");
if (i != std::string::npos)
{
std::string key = line.substr (0, i);
trim (key);
double value = std::atof (line.substr (i + 1).c_str ());
_Database[key] = value;
}
}
file.close ();
}
else
{
std::string error
= RED "Error:" RESET " unable to open '" + filename + "' file";
throw std::runtime_error (error.c_str ());
}
}
void
BitcoinExchange::validadeDatabaseLine (std::string line, size_t lineNumber)
{
size_t i = 0, commaCount = 0;
std::string error = RED "Database error: " RESET "line: ";
std::stringstream lineNumberStr;
lineNumberStr << lineNumber;
error += lineNumberStr.str () + ": ";
if (line.empty ())
{
error += "empty line";
throw std::runtime_error (error.c_str ());
}
i = line.find (",");
if (i == std::string::npos)
{
error += "expected ','";
throw std::runtime_error (error.c_str ());
}
if (haveAplha (line.substr (0, i)) && lineNumber != 1)
{
error += "expected only numbers";
throw std::runtime_error (error.c_str ());
}
if (isDateValid (line.substr (0, i)) == false && lineNumber != 1)
{
error += "invalid date";
throw std::runtime_error (error.c_str ());
}
for (size_t j = 0; j < line.size (); j++)
{
if (line[j] == ',')
commaCount++;
}
if (commaCount != 1)
{
error += "expected only 1 comma";
throw std::runtime_error (error.c_str ());
}
if (isValueValid (line.substr (i + 1)) == false)
{
error += "invalid value";
throw std::runtime_error (error.c_str ());
}
}
void
BitcoinExchange::printData (std::ifstream &file)
{
std::map<std::string, double>::iterator dataBaseElement;
std::string line;
while (std::getline (file, line))
{
try
{
trim (line);
if (line == "date | value")
continue;
validadeInputLine (line);
std::string date, valueStr;
double value;
size_t i = line.find ("|");
date = line.substr (0, i);
trim (date);
valueStr = line.substr (i + 1);
trim (valueStr);
value = std::atof (valueStr.c_str ());
dataBaseElement = getNearestDate (date);
if (dataBaseElement == _Database.end ())
throw std::runtime_error ("date not found in database");
print (date << " => " << valueStr << " = " << std::fixed
<< std::setprecision (2)
<< dataBaseElement->second * value);
}
catch (std::exception &e)
{
std::cout << "Error: " << e.what () << ": '" << line << "'"
<< std::endl;
}
}
}
void
BitcoinExchange::validadeInputLine (std::string line)
{
size_t i;
if (line.empty ())
throw std::runtime_error ("empty line");
if (haveAplha (line))
throw std::runtime_error ("expected only numbers");
i = line.find ("|");
if (i == std::string::npos)
throw std::runtime_error ("expected '|'");
std::string date = line.substr (0, i);
trim (date);
if (date.empty () || isDateValid (date) == false)
throw std::runtime_error ("invalid date");
std::string value = line.substr (i + 1);
trim (value);
if (value.empty ())
throw std::runtime_error ("empty value");
if (std::atof (value.c_str ()) < 0 || std::atof (value.c_str ()) > 1000)
throw std::runtime_error ("invalid value");
}
std::map<std::string, double>::iterator
BitcoinExchange::getNearestDate (std::string inputDate)
{
std::map<std::string, double>::iterator it;
it = _Database.find (inputDate);
if (it != _Database.end ())
return it;
it = _Database.lower_bound (inputDate);
it--;
return it;
}
bool
BitcoinExchange::haveAplha (std::string str)
{
for (size_t i = 0; i < str.size (); i++)
{
if (std::isalpha (str[i]))
return true;
}
return false;
}
bool
BitcoinExchange::isValueValid (std::string value)
{
if (value.empty ())
return false;
if (std::atof (value.c_str ()) < 0)
return false;
return true;
}
bool
BitcoinExchange::isDateValid (std::string date)
{
const char *delimiter = "-";
int tokenCount = 0, hifenCount = 0;
char *token;
for (size_t i = 0; i < date.size (); i++)
{
if (date[i] == '-')
hifenCount++;
}
if (hifenCount != 2)
return false;
token = std::strtok (const_cast<char *> (date.c_str ()), delimiter);
while (token != 0)
{
if (tokenCount == 0
&& (std::atoi (token) < 0 || std::atoi (token) > 2024))
return false;
if (tokenCount == 1 && (std::atoi (token) < 1 || std::atoi (token) > 12))
return false;
if (tokenCount == 2 && (std::atoi (token) < 1 || std::atoi (token) > 31))
return false;
tokenCount++;
token = std::strtok (0, delimiter);
}
if (tokenCount != 3)
return false;
return true;
}
void
BitcoinExchange::trim (std::string &str)
{
const char *whiteSpace = " \t\n\r\f\v";
str.erase (str.find_last_not_of (whiteSpace) + 1);
str.erase (0, str.find_first_not_of (whiteSpace));
}
BitcoinExchange::BitcoinExchange () {}
BitcoinExchange::BitcoinExchange (const BitcoinExchange &other)
{
(void)other;
}
BitcoinExchange &
BitcoinExchange::operator= (const BitcoinExchange &other)
{
(void)other;
return *this;
}
BitcoinExchange::~BitcoinExchange () {}