Commit 5e3b5e17 authored by SAURABH GUPTA's avatar SAURABH GUPTA

Added Matched Order and Junit Test for Process Method in orderProcessing

CLass
parent 4e5b4baf
...@@ -14,6 +14,30 @@ import javax.swing.plaf.synth.SynthSpinnerUI; ...@@ -14,6 +14,30 @@ import javax.swing.plaf.synth.SynthSpinnerUI;
* *
*/ */
public class Order { public class Order {
public Order()
{
}
public Order(Order O)
{
super();
this.ord_ID = O.ord_ID;
this.type = O.type;
this.product = O.product;
this.price = O.price;
this.qty = O.qty;
this.cust_ID = O.cust_ID;
}
public Order(String ord_ID, String type, String product, int price, int qty, String cust_ID) {
super();
this.ord_ID = ord_ID;
this.type = type;
this.product = product;
this.price = price;
this.qty = qty;
this.cust_ID = cust_ID;
}
String ord_ID; String ord_ID;
String type; String type;
...@@ -22,6 +46,7 @@ public class Order { ...@@ -22,6 +46,7 @@ public class Order {
int price; int price;
int qty; int qty;
String cust_ID; String cust_ID;
public void read() { public void read() {
Scanner s = new Scanner(System.in); Scanner s = new Scanner(System.in);
......
...@@ -5,39 +5,47 @@ import java.util.ArrayList; ...@@ -5,39 +5,47 @@ import java.util.ArrayList;
public class OrderProcessing { public class OrderProcessing {
private ArrayList<Order> orderQueue = new ArrayList<Order>(); private ArrayList<Order> orderQueue = new ArrayList<Order>();
public void Process( Order order) { public ArrayList<Order> Process( Order order) {
int lenth = orderQueue.size(); int lenth = orderQueue.size();
ArrayList<Order> matched = new ArrayList<Order>();
if ("Sell".equalsIgnoreCase(order.type)) { if ("Sell".equalsIgnoreCase(order.type)) {
int i = 0; int i = 0;
while (lenth-- > 0) { while (lenth-- > 0) {
Order o = orderQueue.get(i++); Order o = orderQueue.get(i++);
if (("Buy".equalsIgnoreCase(o.type)) && (o.product.equalsIgnoreCase(order.product)) if (("Buy".equalsIgnoreCase(o.type)) && (o.product.equalsIgnoreCase(order.product))
&& (o.price >= order.price)) { && (o.price >= order.price)) {
if (o.qty > order.qty) { Order o1 = new Order(o);
matched.add(o1);
if (o.qty >= order.qty) {
o.qty -= order.qty; o.qty -= order.qty;
o1.qty = order.qty;
order.qty = 0; order.qty = 0;
break; break;
} else { } else {
order.qty -= o.qty; order.qty -= o.qty;
o.qty = 0; o.qty = 0;
o1.qty = 0;
continue; continue;
} }
} }
} }
} else { } else {
System.out.println("HERE");
int i = 0; int i = 0;
while (lenth-- > 0) { while (lenth-- > 0) {
Order o = orderQueue.get(i++); Order o = orderQueue.get(i++);
if ("Sell".equalsIgnoreCase(o.type) && (o.product.equalsIgnoreCase(order.product)) if ("Sell".equalsIgnoreCase(o.type) && (o.product.equalsIgnoreCase(order.product))
&& (o.price <= order.price)) { && (o.price <= order.price)) {
Order o1 = new Order(o);
matched.add(o1);
if (o.qty >= order.qty) { if (o.qty >= order.qty) {
o.qty -= order.qty; o.qty -= order.qty;
o1.qty = order.qty;
order.qty = 0; order.qty = 0;
break; break;
} else { } else {
order.qty -= o.qty; order.qty -= o.qty;
o.qty = 0; o.qty = 0;
o1.qty = 0;
continue; continue;
} }
} }
...@@ -45,22 +53,33 @@ public class OrderProcessing { ...@@ -45,22 +53,33 @@ public class OrderProcessing {
} }
orderQueue.add(order); orderQueue.add(order);
System.out.println(orderQueue); System.out.println(orderQueue);
return matched;
} }
public ArrayList<Order> getOrderQueue() { public ArrayList<Order> getOrderQueue() {
return orderQueue; return orderQueue;
} }
public void pending() { public void pending(ArrayList<Order> matched) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
int lenth = orderQueue.size(); for(int i=0;i<orderQueue.size();)
System.out.println("Number of orders left : " + lenth);
int i=0;
while(lenth-- > 0)
{ {
System.out.println("\nORDER #"+(i+1)); Order order = orderQueue.get(i);
Order order = orderQueue.get(i++); if(order.qty != 0)
if(order.qty != 0) order.show(); {
System.out.println("Order #"+i++);
order.show();
}
else
{
orderQueue.remove(order);
}
} }
} }
public void matched(ArrayList<Order> matched) {
// TODO Auto-generated method stub
for(int i=0;i<matched.size();i++)
matched.get(i).show();
}
} }
package Order;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
class ProcessTest {
@Test
void testProcess() {
OrderProcessing junit = new OrderProcessing();
assertEquals((junit.Process(new Order("t1","sell","apple",325,25,"c01"))).size(),0);
assertEquals((junit.Process(new Order("t2","sell","apple",320,30,"c02"))).size(),0);
assertEquals((junit.Process(new Order("t3","buy","apple",315,20,"c03"))).size(),0);
assertEquals((junit.Process(new Order("t4","buy","apple",330,35,"c04"))).size(),2);
}
}
...@@ -22,10 +22,13 @@ public class Main { ...@@ -22,10 +22,13 @@ public class Main {
"Provide Order Details : \n [orderID : Type : Product : Price : Quantity : CustID]\n"); "Provide Order Details : \n [orderID : Type : Product : Price : Quantity : CustID]\n");
O1.read(); ///Reading new orders O1.read(); ///Reading new orders
// orders.add(O1); //Added in the list // orders.add(O1); //Added in the list
orderProcessing.Process(O1); // Finding the match ArrayList<Order> matched = orderProcessing.Process(O1); // Finding the match
System.out.println("Matched Orders are : ");
orderProcessing.matched(matched);
System.out.println("\nPending Orders till now are : "); System.out.println("\nPending Orders till now are : ");
orderProcessing.pending(); //Pending orders. orderProcessing.pending(matched); //Pending orders.
System.out.println("\nWant to add new order : [y/n] \n "); System.out.println("\nWant to add new order : [y/n] \n ");
@SuppressWarnings("resource") @SuppressWarnings("resource")
Scanner s = new Scanner(System.in); Scanner s = new Scanner(System.in);
......
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