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
671505e4
You need to sign in or sign up before continuing.
Commit
671505e4
authored
Jan 20, 2018
by
Rohit Prasad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement method to find all buy orders for a sell order
parent
0154d87a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
2 deletions
+45
-2
src/OrderMatching.java
src/OrderMatching.java
+45
-2
No files found.
src/OrderMatching.java
View file @
671505e4
...
@@ -2,8 +2,8 @@ import java.util.*;
...
@@ -2,8 +2,8 @@ import java.util.*;
public
class
OrderMatching
{
public
class
OrderMatching
{
List
<
SellOrder
>
sellOrderQueue
=
new
LinkedList
<>();
private
List
<
SellOrder
>
sellOrderQueue
=
new
LinkedList
<>();
List
<
BuyOrder
>
buyOrderQueue
=
new
LinkedList
<>();
private
List
<
BuyOrder
>
buyOrderQueue
=
new
LinkedList
<>();
/*
/*
* Checks if the order is valid
* Checks if the order is valid
...
@@ -12,4 +12,47 @@ public class OrderMatching {
...
@@ -12,4 +12,47 @@ public class OrderMatching {
return
order
.
getQty
()
>
0
&&
order
.
getPrice
()
>
0.0
;
return
order
.
getQty
()
>
0
&&
order
.
getPrice
()
>
0.0
;
}
}
/*
* Checks if trade can happen between buy order and
* sell order
*/
boolean
buySellOrderMatch
(
BuyOrder
b
,
SellOrder
s
)
{
return
!
b
.
getCustomer
().
equals
(
s
.
getCustomer
())
&&
b
.
getStock
().
equals
(
s
.
getStock
())
&&
b
.
getPrice
()
>=
s
.
getPrice
()
&&
b
.
getQty
()
<=
s
.
getQty
();
}
ArrayList
<
BuyOrder
>
findBuyOrders
(
SellOrder
s
)
{
ArrayList
<
BuyOrder
>
matchedOrders
=
new
ArrayList
<>();
// Iterate over buy orders queue
for
(
BuyOrder
b
:
buyOrderQueue
)
{
// if current sell orders have already found all buyers,
// exit the loop
if
(
s
.
getQty
()
<=
0
)
break
;
// check if trade is possible
if
(
buySellOrderMatch
(
b
,
s
))
{
matchedOrders
.
add
(
b
);
// modify sell order quantity to prevent
// repeated purchases
s
.
setQty
(
s
.
getQty
()
-
b
.
getQty
());
}
}
// if there is still some stocks to sell
// add it to sell orders queue
if
(
s
.
getQty
()
>
0
)
sellOrderQueue
.
add
(
s
);
// remove all buy orders that were matched from
// buy order queue
for
(
BuyOrder
b
:
matchedOrders
)
{
buyOrderQueue
.
remove
(
b
);
}
return
matchedOrders
;
}
}
}
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