Commit b5779b55 authored by Rohit Prasad's avatar Rohit Prasad

Implement findMatchedOrders which will handle buy and sell orders

parent 409aaad9
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* for both SellTrade and BuyTrade classes that will inherit * for both SellTrade and BuyTrade classes that will inherit
* for it. * for it.
*/ */
public abstract class Order { public class Order {
private int qty; private int qty;
private float price; private float price;
private int time; private int time;
......
...@@ -12,6 +12,14 @@ public class OrderMatching { ...@@ -12,6 +12,14 @@ public class OrderMatching {
return order.getQty() > 0 && order.getPrice() > 0.0; return order.getQty() > 0 && order.getPrice() > 0.0;
} }
/*
* General method to handle both BuyOrder and SellOrder
*/
ArrayList<Order> findMatchedOrders(Order o) {
return (o.getType() == "SELL")
? findBuyOrders((SellOrder) o)
: findSellOrders((BuyOrder) o);
}
/* /*
* Checks if trade can happen between buy order and * Checks if trade can happen between buy order and
* sell order * sell order
...@@ -25,8 +33,8 @@ public class OrderMatching { ...@@ -25,8 +33,8 @@ public class OrderMatching {
/* /*
* Finds all buy orders for current sell order * Finds all buy orders for current sell order
*/ */
ArrayList<BuyOrder> findBuyOrders(SellOrder s) { ArrayList<Order> findBuyOrders(SellOrder s) {
ArrayList<BuyOrder> matchedOrders = new ArrayList<>(); ArrayList<Order> matchedOrders = new ArrayList<>();
// Iterate over buy orders queue // Iterate over buy orders queue
for (BuyOrder b : buyOrderQueue) { for (BuyOrder b : buyOrderQueue) {
...@@ -67,7 +75,7 @@ public class OrderMatching { ...@@ -67,7 +75,7 @@ public class OrderMatching {
// remove all buy orders that were matched from // remove all buy orders that were matched from
// buy order queue // buy order queue
for (BuyOrder b : matchedOrders) { for (Order b : matchedOrders) {
buyOrderQueue.remove(b); buyOrderQueue.remove(b);
} }
...@@ -77,8 +85,8 @@ public class OrderMatching { ...@@ -77,8 +85,8 @@ public class OrderMatching {
/* /*
* Finds all sell orders for current buy order * Finds all sell orders for current buy order
*/ */
ArrayList<SellOrder> findSellOrders(BuyOrder b) { ArrayList<Order> findSellOrders(BuyOrder b) {
ArrayList<SellOrder> matchedOrders = new ArrayList<>(); ArrayList<Order> matchedOrders = new ArrayList<>();
// Iterate over sell orders queue // Iterate over sell orders queue
for (SellOrder s : sellOrderQueue) { for (SellOrder s : sellOrderQueue) {
...@@ -119,7 +127,7 @@ public class OrderMatching { ...@@ -119,7 +127,7 @@ public class OrderMatching {
// remove all sell orders that were matched from // remove all sell orders that were matched from
// sell order queue // sell order queue
for (SellOrder s : matchedOrders) { for (Order s : matchedOrders) {
sellOrderQueue.remove(s); sellOrderQueue.remove(s);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment