You need to sign in or sign up before continuing.
Commit bd2fed22 authored by Rohit Prasad's avatar Rohit Prasad

Modify findBuyOrders() to take care of the case when buy order has more stock than the sell order

parent 7e5fb883
...@@ -19,10 +19,12 @@ public class OrderMatching { ...@@ -19,10 +19,12 @@ public class OrderMatching {
boolean buySellOrderMatch(BuyOrder b, SellOrder s) { boolean buySellOrderMatch(BuyOrder b, SellOrder s) {
return !b.getCustomer().equals(s.getCustomer()) && return !b.getCustomer().equals(s.getCustomer()) &&
b.getStock().equals(s.getStock()) && b.getStock().equals(s.getStock()) &&
b.getPrice() >= s.getPrice() && b.getPrice() >= s.getPrice();
b.getQty() <= s.getQty();
} }
/*
* Finds all buy orders for current sell order
*/
ArrayList<BuyOrder> findBuyOrders(SellOrder s) { ArrayList<BuyOrder> findBuyOrders(SellOrder s) {
ArrayList<BuyOrder> matchedOrders = new ArrayList<>(); ArrayList<BuyOrder> matchedOrders = new ArrayList<>();
...@@ -34,11 +36,28 @@ public class OrderMatching { ...@@ -34,11 +36,28 @@ public class OrderMatching {
// check if trade is possible // check if trade is possible
if (buySellOrderMatch(b, s)) { if (buySellOrderMatch(b, s)) {
matchedOrders.add(b);
int qty = s.getQty() - b.getQty();
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(s.getQty() - b.getQty());
matchedOrders.add(b);
} else {
s.setQty(0);
// create a temporary BuyOrder with quantity equal to
// the remaining stocks in sell order
BuyOrder _b = new BuyOrder(b);
_b.setQty(s.getQty());
// add it to buy orders queue
matchedOrders.add(_b);
// modify buy order's quantity to reflect quantity
// remaining after trade
b.setQty(b.getQty() - s.getQty());
}
} }
} }
......
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