-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommodityMarketSystem.cpp
388 lines (354 loc) · 11.6 KB
/
CommodityMarketSystem.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "Database.h"
#include "CommodityMarketSystem.h"
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
CommodityMarketSystem::CommodityMarketSystem() {
currentOrderNumber = 0;
printer.printWelcomeMessage();
}
CommodityMarketSystem::~CommodityMarketSystem() {
for (map<int, Order*>::iterator i = orders.begin(); i != orders.end(); i++) {
delete i->second;
}
orders.clear();
}
/*
* Process a string input by the user into an instruction, then execute
* instruction using the functions below.
*/
void CommodityMarketSystem::processInput(string command) {
vector<string> commandArray = createCommandArray(command);
if (commandArray.size() < 2) {
printer.printError("INVALID_MESSAGE");
return;
}
string dealer = commandArray[0];
if (isValidDealer(dealer)) {
string instruction = commandArray[1];
vector<string> args(commandArray.begin()+2, commandArray.end());
executeInstruction(dealer, instruction, args);
}
else printer.printError("UNKNOWN_DEALER");
}
/*
* Checks to make sure instruction is valid, then calls the relevant
* command.
*/
void CommodityMarketSystem::executeInstruction(string dealer,
string instruction,
vector<string> args) {
if (instruction.compare("POST") == 0) {
postOrder(dealer, args);
}
else if (instruction.compare("REVOKE") == 0) {
revokeOrder(dealer, args);
}
else if (instruction.compare("CHECK") == 0) {
checkOrder(dealer, args);
}
else if (instruction.compare("LIST") == 0) {
listOrders(dealer, args);
}
else if (instruction.compare("AGGRESS") == 0) {
aggressOrders(dealer, args);
}
else {
printer.printError("INVALID_MESSAGE");
}
}
/****************************** POST ******************************/
/*
* Post an order to the system. Create a new Order object using the given
* arguments, then add the order to the CMS orders map. Increment
* currentOrderNumber.
*/
void CommodityMarketSystem::postOrder(string dealer, vector<string> args) {
if (validatePostArgs(args)) {
string side = args[0];
string commodity = args[1];
int amount = stoi(args[2]);
double price = stod(args[3]);
Order *order = new Order(++currentOrderNumber, dealer, commodity,
side, amount, price);
orders[currentOrderNumber] = order;
printer.printPostConfirmation(*order);
}
}
bool CommodityMarketSystem::validatePostArgs(vector<string> postArgs) {
// check correct number of arguments
if (postArgs.size() != 4) {
printer.printError("INVALID_MESSAGE");
return false;
}
// check string arguments
string side = postArgs[0];
string commodity = postArgs[1];
if (!isValidSide(side)) {
printer.printError("INVALID_MESSAGE");
return false;
}
if (!isValidCommodity(commodity)) {
printer.printError("UNKNOWN_COMMODITY");
return false;
}
// check numeric arguments
string amountString = postArgs[2];
string priceString = postArgs[3];
if (!isPureIntegerString(amountString) || !isPureDoubleString(priceString)) {
printer.printError("INVALID_MESSAGE");
return false;
}
// all arguments valid - return true
return true;
}
/****************************** END POST ******************************/
/****************************** REVOKE ******************************/
/*
* Remove an order from the system. If the orderID is valid and the dealer
* issuing the command is authorized, delete the entry from the orders map.
*/
void CommodityMarketSystem::revokeOrder(string dealer, vector<string> args) {
if (validateRevokeArgs(args)) {
int orderID = stoi(args[0]);
if (!isAuthorized(dealer, orders[orderID])) {
printer.printError("UNAUTHORIZED");
return;
}
delete orders[orderID];
orders.erase(orderID);
printer.printRevokedOrder(orderID);
}
}
bool CommodityMarketSystem::validateRevokeArgs(vector<string> postArgs) {
if (postArgs.size() != 1 || !isPureIntegerString(postArgs[0])) {
printer.printError("INVALID_MESSAGE");
return false;
}
int orderID = stoi(postArgs[0]);
if (!isValidOrder(orderID)) {
printer.printError("UNKNOWN_ORDER");
return false;
}
return true;
}
/****************************** END REVOKE ******************************/
/****************************** CHECK ******************************/
/*
* Check on the status of an order. If the dealer is authorized and the user is
* valid, print either:
* - the order information if the order has not yet been filled
* - a filled message otherwise
*/
void CommodityMarketSystem::checkOrder(string dealer, vector<string> args) {
if (validateCheckArgs(args)) {
int orderID = stoi(args[0]);
if (!isAuthorized(dealer, orders[orderID])) {
printer.printError("UNAUTHORIZED");
return;
}
Order *order = orders[orderID];
if (order->getAmount() == 0) {
printer.printFilledStatus(orderID);
}
else {
printer.printOrderInfo(*order);
}
}
}
bool CommodityMarketSystem::validateCheckArgs(vector<string> postArgs) {
if (postArgs.size() != 1 || !isPureIntegerString(postArgs[0])) {
printer.printError("INVALID_MESSAGE");
return false;
}
int orderID = stoi(postArgs[0]);
if (!isValidOrder(orderID)) {
printer.printError("UNKNOWN_ORDER");
return false;
}
return true;
}
/****************************** END CHECK ******************************/
/****************************** LIST ******************************/
/*
* List all orders that satisfy the given filters. The user may provide filters
* for the dealer and the commodity, and this function prints an order
* information message for every unfilled order that satisfies the given
* constraints.
*/
void CommodityMarketSystem::listOrders(string dealer, vector<string> args) {
if (validateListArgs(args)) {
string commodityFilter = "";
string dealerFilter = "";
if (args.size() >= 1) {
commodityFilter = args[0];
}
if (args.size() == 2) {
dealerFilter = args[1];
}
set<Order*> ordersToPrint = filterOrders(commodityFilter, dealerFilter);
for (Order *order : ordersToPrint) {
if (order->getAmount() != 0) {
printer.printOrderInfo(*order);
}
}
}
}
bool CommodityMarketSystem::validateListArgs(vector<string> postArgs) {
if (postArgs.size() > 2){
printer.printError("INVALID_MESSAGE");
return false;
}
if (postArgs.size() >= 1) {
string commodity = postArgs[0];
if (!isValidCommodity(commodity)) {
printer.printError("UNKNOWN_COMMODITY");
return false;
}
}
if (postArgs.size() == 2) {
string dealer = postArgs[1];
if (!isValidDealer(dealer)) {
printer.printError("UNKNOWN_DEALER");
return false;
}
}
return true;
}
set<Order*> CommodityMarketSystem::filterOrders(string commodityFilter,
string dealerFilter) {
set<Order*> ordersToList;
for (auto &order : orders) {
// no filters set
if (commodityFilter.empty() && dealerFilter.empty()) {
ordersToList.insert(order.second);
}
// commodity filter set
else if (dealerFilter.empty()) {
if (commodityFilter.compare(order.second->getCommodity()) == 0) {
ordersToList.insert(order.second);
}
}
// both filters set
else {
if (commodityFilter.compare(order.second->getCommodity()) == 0 &&
dealerFilter.compare(order.second->getDealer()) == 0) {
ordersToList.insert(order.second);
}
}
}
return ordersToList;
}
/****************************** END LIST ******************************/
/****************************** AGGRESS ******************************/
/*
* Aggress on a given order. If the order number is valid, and the number of
* units supplied is less than or equal to the number of units remaining in
* the order, make the transaction and update the orders map accordingly. Then
* print a success message to the terminal.
*/
void CommodityMarketSystem::aggressOrders(string dealer, vector<string> args) {
if (validateAggressArgs(args)) {
vector<int> ordersToAggress;
vector<int> amountsToAggress;
for (int i = 0; i < args.size(); i++) {
if (i % 2 == 0) {
ordersToAggress.push_back(stoi(args[i]));
}
if (i % 2 == 1) {
amountsToAggress.push_back(stoi(args[i]));
}
}
for (int i = 0; i < ordersToAggress.size(); i++) {
aggressOrder(dealer, ordersToAggress[i], amountsToAggress[i]);
}
}
}
void CommodityMarketSystem::aggressOrder(string dealer, int orderID, int amount) {
Order *order = orders[orderID];
if (order->getAmount() >= amount) {
int newBalance = order->getAmount() - amount;
order->updateAmount(newBalance);
bool aggressorIsSelling = (order->getSide().compare("BUY") == 0);
string boughtOrSold = (aggressorIsSelling ? "SOLD" : "BOUGHT");
printer.printTradeReport(boughtOrSold, amount, *order);
}
else {
printer.printError("INVALID_MESSAGE");
}
}
bool CommodityMarketSystem::validateAggressArgs(vector<string> postArgs) {
if (postArgs.size() == 0 || postArgs.size() % 2 != 0) {
printer.printError("INVALID_MESSAGE");
return false;
}
set<int> usedOrders;
for (int i = 0; i < postArgs.size(); i++) {
// check integer values
if (!isPureIntegerString(postArgs[i])) {
printer.printError("INVALID_MESSAGE");
return false;
}
// check order IDs
if (i % 2 == 0) {
if (!isValidOrder(stoi(postArgs[i]))) {
printer.printError("UNKNOWN_ORDER");
return false;
}
// cannot aggress on the same order twice in one command
if (usedOrders.find(stoi(postArgs[i])) != usedOrders.end()) {
printer.printError("INVALID_MESSAGE");
return false;
}
usedOrders.insert(stoi(postArgs[i]));
}
}
return true;
}
/****************************** END AGGRESS ******************************/
/*
*
*/
vector<string> CommodityMarketSystem::createCommandArray(string command) {
vector<string> commandArray;
stringstream ss(command);
string word;
while (ss >> word) {
commandArray.push_back(word);
}
return commandArray;
}
// Helper: checks if a given commodity is valid
bool CommodityMarketSystem::isValidCommodity(string commodity) {
return !(find(begin(Commodities), end(Commodities), commodity) == end(Commodities));
}
// Helper: checks if a given side is valid
bool CommodityMarketSystem::isValidSide(string side) {
return !(find(begin(Sides), end(Sides), side) == end(Sides));
}
// Helper: checks if a given dealer is valid
bool CommodityMarketSystem::isValidDealer(string dealerID) {
return !(find(begin(DealerIDs), end(DealerIDs), dealerID) == end(DealerIDs));
}
// Helper: checks if a given order ID is valid
bool CommodityMarketSystem::isValidOrder(int orderID) {
return !(orders.find(orderID) == orders.end());
}
// Helper: checks if a given dealer is authorized on a given order
bool CommodityMarketSystem::isAuthorized(string requestingDealer, Order* order) {
return (requestingDealer.compare(order->getDealer()) == 0);
}
// Helper: checks if a string is purely an integer.
// Example: "123" is valid, "123.1", "123a" are not
bool CommodityMarketSystem::isPureIntegerString(string s) {
return !s.empty() && s.find_first_not_of("0123456789") == string::npos;
}
// Helper: checks if a string is purely an integer of double.
// Example "123.1" is valid, "123.1.1", "123.1a" are not
bool CommodityMarketSystem::isPureDoubleString(string s) {
char* endptr = 0;
strtod(s.c_str(), &endptr);
return !(*endptr != '\0' || endptr == s);
}