-
Notifications
You must be signed in to change notification settings - Fork 2
/
Order.cpp
143 lines (124 loc) · 4.4 KB
/
Order.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
#include<iostream>
#include<string>
#include "Order.h"
using namespace std;
// Status Initialization the different types of Orders.
const char Order::orderAddition = 'A';
const char Order::orderModify = 'M';
const char Order::orderRemove = 'X';
// Default constructor implementation.
Order::Order(const int& seqId, const char actionType, const long& orderId, const char orderSide, const long& orderQty,const double& orderPrice) :
m_seqId(seqId) , m_actionType(actionType), m_orderId(orderId), m_orderSide(orderSide), m_orderQty(orderQty), m_orderPrice(orderPrice)
{
}
// Copy constructor implementation.
Order::Order(const Order& copyOrder)
{
m_seqId = copyOrder.m_seqId;
m_actionType = copyOrder.m_actionType;
m_orderId = copyOrder.m_orderId;
m_orderSide = copyOrder.m_orderSide;
m_orderQty = copyOrder.m_orderQty;
m_orderPrice = copyOrder.m_orderPrice;
}
// Assignment operator implementation.
Order& Order::operator=(const Order& assignOrder)
{
m_seqId = assignOrder.m_seqId;
m_actionType = assignOrder.m_actionType;
m_orderId = assignOrder.m_orderId;
m_orderSide = assignOrder.m_orderSide;
m_orderQty = assignOrder.m_orderQty;
m_orderPrice = assignOrder.m_orderPrice;
return(*this);
}
// Display Order properties for debugging purpose.
void Order::show() const
{
std::cout << "Order seqId = " << m_seqId << std::endl;
std::cout << "Order ActionType = " << m_actionType << std::endl;
std::cout << "OrderId = " << m_orderId << std::endl;
std::cout << "OrderSide = " << m_orderSide << std::endl;
std::cout << "OrderQty = " << m_orderQty << std::endl;
std::cout << "OrderPrice = " << m_orderPrice << std::endl;
}
// Find out the order details.
std::string Order::getOrderDetails() const
{
std::string returnMessage = "Order seqId = " + std::to_string(m_seqId) + '\n';
std::string str(1, m_actionType);
returnMessage += "Order ActionType = " + str + '\n';
returnMessage += "OrderId = " + std::to_string(m_orderId) + '\n';
returnMessage += "OrderSide = " + std::to_string(m_orderSide) + '\n';
returnMessage += "OrderQty = " + std::to_string(m_orderQty)+ '\n';
returnMessage += "OrderPrice = " + std::to_string(m_orderPrice) + '\n';
return(returnMessage);
}
// Some very important getters and setters required for the Order.
// Getters are below.
int Order::getSeqId() const { return(m_seqId); }
const char Order::getActionType() const { return(m_actionType); }
long Order::getOrderId() const { return(m_orderId); }
const char Order::getOrderside() const { return(m_orderSide); }
long Order::getOrderQty() const { return(m_orderQty); }
double Order::getOrderPrice() const { return(m_orderPrice); }
// Setters are below.
void Order::setOrderQty(const int& newQty) { m_orderQty = newQty; }
// The comparison operators for the Order structure sorting are implemented below.
bool operator < (const Order& lhs, const Order& rhs)
{
// This is a 2 criteria sorter. First by price. if prices are same then by seqId. The lesser the seqId the earlier the order had come hence it is given priority in the queue.
if(lhs.getOrderPrice() < rhs.getOrderPrice())
{
return(true);
}
if(lhs.getOrderPrice() > rhs.getOrderPrice())
{
return(false);
}
if(lhs.getOrderPrice() == rhs.getOrderPrice())
{
return(lhs.getSeqId() < rhs.getSeqId());
}
return(false);
}
bool operator > (const Order& lhs, const Order& rhs)
{
// This is a 2 criteria sorter. First by price. if prices are same then by seqId. The lesser the seqId the earlier the order had come hence it is given priority in the queue.
if(lhs.getOrderPrice() > rhs.getOrderPrice())
{
return true;
}
if(rhs.getOrderPrice() < lhs.getOrderPrice())
{
return false;
}
if(lhs.getOrderPrice() == rhs.getOrderPrice())
{
return(lhs.getSeqId() < rhs.getSeqId());
}
return(false);
}
bool operator == (const Order& lhs, const Order& rhs)
{
return(lhs.getOrderId() == rhs.getOrderId());
}
// Construct the order message from the properties.
std::string Order::getOrderMessage() const
{
// A,100000,B,2,94
std::string str(1, m_actionType);
std::string returnMessage = str;returnMessage += ",";
returnMessage += std::to_string(m_orderId); returnMessage += ",";
returnMessage += m_orderSide; returnMessage += ",";
returnMessage += std::to_string(m_orderQty); returnMessage += ",";
returnMessage += std::to_string(m_orderPrice);
return(returnMessage);
}
orderType Order::getOrderType()
{
if(m_orderSide == 'B')
return(orderType::buyOrder);
else
return(orderType::sellOrder);
}