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

Added Matched Order and Junit Test for Process Method in orderProcessing

CLass
parent 4e5b4baf
......@@ -15,6 +15,30 @@ import javax.swing.plaf.synth.SynthSpinnerUI;
*/
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 type;
String product;
......@@ -23,6 +47,7 @@ public class Order {
int qty;
String cust_ID;
public void read() {
Scanner s = new Scanner(System.in);
this.ord_ID = s.nextLine();
......
......@@ -5,39 +5,47 @@ import java.util.ArrayList;
public class OrderProcessing {
private ArrayList<Order> orderQueue = new ArrayList<Order>();
public void Process( Order order) {
public ArrayList<Order> Process( Order order) {
int lenth = orderQueue.size();
ArrayList<Order> matched = new ArrayList<Order>();
if ("Sell".equalsIgnoreCase(order.type)) {
int i = 0;
while (lenth-- > 0) {
Order o = orderQueue.get(i++);
if (("Buy".equalsIgnoreCase(o.type)) && (o.product.equalsIgnoreCase(order.product))
&& (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;
o1.qty = order.qty;
order.qty = 0;
break;
} else {
order.qty -= o.qty;
o.qty = 0;
o1.qty = 0;
continue;
}
}
}
} else {
System.out.println("HERE");
int i = 0;
while (lenth-- > 0) {
Order o = orderQueue.get(i++);
if ("Sell".equalsIgnoreCase(o.type) && (o.product.equalsIgnoreCase(order.product))
&& (o.price <= order.price)) {
Order o1 = new Order(o);
matched.add(o1);
if (o.qty >= order.qty) {
o.qty -= order.qty;
o1.qty = order.qty;
order.qty = 0;
break;
} else {
order.qty -= o.qty;
o.qty = 0;
o1.qty = 0;
continue;
}
}
......@@ -45,22 +53,33 @@ public class OrderProcessing {
}
orderQueue.add(order);
System.out.println(orderQueue);
return matched;
}
public ArrayList<Order> getOrderQueue() {
return orderQueue;
}
public void pending() {
public void pending(ArrayList<Order> matched) {
// TODO Auto-generated method stub
int lenth = orderQueue.size();
System.out.println("Number of orders left : " + lenth);
int i=0;
while(lenth-- > 0)
for(int i=0;i<orderQueue.size();)
{
Order order = orderQueue.get(i);
if(order.qty != 0)
{
System.out.println("Order #"+i++);
order.show();
}
else
{
System.out.println("\nORDER #"+(i+1));
Order order = orderQueue.get(i++);
if(order.qty != 0) order.show();
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 {
"Provide Order Details : \n [orderID : Type : Product : Price : Quantity : CustID]\n");
O1.read(); ///Reading new orders
// 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 : ");
orderProcessing.pending(); //Pending orders.
orderProcessing.pending(matched); //Pending orders.
System.out.println("\nWant to add new order : [y/n] \n ");
@SuppressWarnings("resource")
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