Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CS682-HW1-OrderMatching
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Rohit Prasad
CS682-HW1-OrderMatching
Commits
37592709
You need to sign in or sign up before continuing.
Commit
37592709
authored
Jan 21, 2018
by
Rohit Prasad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add method to find sell orders for a buy order
parent
be0f60d7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
6 deletions
+65
-6
src/OrderMatching.java
src/OrderMatching.java
+65
-6
No files found.
src/OrderMatching.java
View file @
37592709
...
@@ -42,21 +42,21 @@ public class OrderMatching {
...
@@ -42,21 +42,21 @@ public class OrderMatching {
if
(
qty
>=
0
)
{
if
(
qty
>=
0
)
{
// modify sell order quantity to prevent
// modify sell order quantity to prevent
// repeated purchases
// repeated purchases
s
.
setQty
(
s
.
getQty
()
-
b
.
getQty
()
);
s
.
setQty
(
qty
);
matchedOrders
.
add
(
b
);
matchedOrders
.
add
(
b
);
}
else
{
}
else
{
s
.
setQty
(
0
);
// create a temporary BuyOrder with quantity equal to
// create a temporary BuyOrder with quantity equal to
// the remaining stocks in sell order
// the remaining stocks in sell order
BuyOrder
_b
=
new
BuyOrder
(
b
);
BuyOrder
_b
=
new
BuyOrder
(
b
);
_b
.
setQty
(
s
.
getQty
());
_b
.
setQty
(
s
.
getQty
());
// add it to buy orders queue
// add it to buy orders queue
matchedOrders
.
add
(
_b
);
matchedOrders
.
add
(
_b
);
s
.
setQty
(
0
);
// modify buy order's quantity to reflect quantity
// modify buy order's quantity to reflect quantity
// remaining after trade
// remaining after trade
b
.
setQty
(
b
.
getQty
()
-
s
.
getQty
());
b
.
setQty
(
b
.
getQty
()
-
_b
.
getQty
());
}
}
}
}
}
}
...
@@ -74,4 +74,63 @@ public class OrderMatching {
...
@@ -74,4 +74,63 @@ public class OrderMatching {
return
matchedOrders
;
return
matchedOrders
;
}
}
/*
* Finds all sell orders for current buy order
*/
ArrayList
<
SellOrder
>
findSellOrders
(
BuyOrder
b
)
{
ArrayList
<
SellOrder
>
matchedOrders
=
new
ArrayList
<>();
// Iterate over sell orders queue
for
(
SellOrder
s
:
sellOrderQueue
)
{
// if current buy order has already found all sellers,
// exit the loop
if
(
b
.
getQty
()
<=
0
)
break
;
// check if trade is possible
if
(
buySellOrderMatch
(
b
,
s
))
{
int
qty
=
b
.
getQty
()
-
s
.
getQty
();
if
(
qty
>=
0
)
{
// modify buy order quantity to prevent
// repeated purchases
b
.
setQty
(
qty
);
matchedOrders
.
add
(
s
);
}
else
{
// create a temporary SellOrder with quantity equal to
// the remaining stocks in buy order
SellOrder
_s
=
new
SellOrder
(
s
);
_s
.
setQty
(
b
.
getQty
());
// add it to matched orders queue
matchedOrders
.
add
(
_s
);
b
.
setQty
(
0
);
// modify sell order's quantity to reflect quantity
// remaining after trade
s
.
setQty
(
s
.
getQty
()
-
_s
.
getQty
());
}
}
}
// if there is still some stocks to buy
// add it to buy orders queue
if
(
b
.
getQty
()
>
0
)
buyOrderQueue
.
add
(
b
);
// remove all sell orders that were matched from
// sell order queue
for
(
SellOrder
s
:
matchedOrders
)
{
sellOrderQueue
.
remove
(
s
);
}
return
matchedOrders
;
}
List
<
SellOrder
>
printSellOrderQueue
()
{
return
sellOrderQueue
;
}
List
<
BuyOrder
>
printBuyOrderQueue
()
{
return
buyOrderQueue
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment