Commit 1deea996 authored by SAURABH GUPTA's avatar SAURABH GUPTA

Fully Tested and Commented Code

parent 5e3b5e17
...@@ -15,10 +15,22 @@ import javax.swing.plaf.synth.SynthSpinnerUI; ...@@ -15,10 +15,22 @@ import javax.swing.plaf.synth.SynthSpinnerUI;
*/ */
public class Order { public class Order {
String ord_ID;
String type;
String product;
String pro_ID;
int price;
int qty;
String cust_ID;
//Empty Constructor
public Order() public Order()
{ {
} }
//Copy Constructor
public Order(Order O) public Order(Order O)
{ {
super(); super();
...@@ -29,6 +41,8 @@ public class Order { ...@@ -29,6 +41,8 @@ public class Order {
this.qty = O.qty; this.qty = O.qty;
this.cust_ID = O.cust_ID; this.cust_ID = O.cust_ID;
} }
//Constructor for Initialization
public Order(String ord_ID, String type, String product, int price, int qty, String cust_ID) { public Order(String ord_ID, String type, String product, int price, int qty, String cust_ID) {
super(); super();
this.ord_ID = ord_ID; this.ord_ID = ord_ID;
...@@ -39,15 +53,6 @@ public class Order { ...@@ -39,15 +53,6 @@ public class Order {
this.cust_ID = cust_ID; this.cust_ID = cust_ID;
} }
String ord_ID;
String type;
String product;
String pro_ID;
int price;
int qty;
String cust_ID;
public void read() { public void read() {
Scanner s = new Scanner(System.in); Scanner s = new Scanner(System.in);
this.ord_ID = s.nextLine(); this.ord_ID = s.nextLine();
......
...@@ -2,12 +2,20 @@ package Order; ...@@ -2,12 +2,20 @@ package Order;
import java.util.ArrayList; import java.util.ArrayList;
//Class for Processing the Orders
public class OrderProcessing { public class OrderProcessing {
//OrderQueue in which all Orders are added and removed if they got a match.
private ArrayList<Order> orderQueue = new ArrayList<Order>(); private ArrayList<Order> orderQueue = new ArrayList<Order>();
//Processing Logic
public ArrayList<Order> Process( Order order) { public ArrayList<Order> Process( Order order) {
int lenth = orderQueue.size(); int lenth = orderQueue.size();
//Matched Queue for storing the matched orders at that time only i.e. why i have declared locally so that it will persist for more than one orders.
ArrayList<Order> matched = new ArrayList<Order>(); ArrayList<Order> matched = new ArrayList<Order>();
//if current order is sell type order
if ("Sell".equalsIgnoreCase(order.type)) { if ("Sell".equalsIgnoreCase(order.type)) {
int i = 0; int i = 0;
while (lenth-- > 0) { while (lenth-- > 0) {
...@@ -29,7 +37,9 @@ public class OrderProcessing { ...@@ -29,7 +37,9 @@ public class OrderProcessing {
} }
} }
} }
} else { }
//if current order is buy type order
else {
int i = 0; int i = 0;
while (lenth-- > 0) { while (lenth-- > 0) {
Order o = orderQueue.get(i++); Order o = orderQueue.get(i++);
...@@ -56,27 +66,33 @@ public class OrderProcessing { ...@@ -56,27 +66,33 @@ public class OrderProcessing {
return matched; return matched;
} }
//getting the Current OrderQueue
public ArrayList<Order> getOrderQueue() { public ArrayList<Order> getOrderQueue() {
return orderQueue; return orderQueue;
} }
public void pending(ArrayList<Order> matched) {
//Function for pending orders
public void pending() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
for(int i=0;i<orderQueue.size();) for(int i=0;i<orderQueue.size();)
{ {
Order order = orderQueue.get(i); Order order = orderQueue.get(i);
if(order.qty != 0) if(order.qty != 0)
{ {
//prints only non-zero quantity orders
System.out.println("Order #"+i++); System.out.println("Order #"+i++);
order.show(); order.show();
} }
else else
{ {
//remove the zero quantity orders
orderQueue.remove(order); orderQueue.remove(order);
} }
} }
} }
//Function for returning matched orders
public void matched(ArrayList<Order> matched) { public void matched(ArrayList<Order> matched) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
for(int i=0;i<matched.size();i++) for(int i=0;i<matched.size();i++)
......
...@@ -10,7 +10,7 @@ public class Main { ...@@ -10,7 +10,7 @@ public class Main {
/** /**
* @param args * @param args
*/ *///Main Function
public static void main(String[] args) { public static void main(String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
char choice = 'n'; char choice = 'n';
...@@ -18,17 +18,25 @@ public class Main { ...@@ -18,17 +18,25 @@ public class Main {
OrderProcessing orderProcessing = new OrderProcessing(); OrderProcessing orderProcessing = new OrderProcessing();
do { do {
Order O1 = new Order(); Order O1 = new Order();
//Reading new order
System.out.println( System.out.println(
"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();
// orders.add(O1); //Added in the list
ArrayList<Order> matched = orderProcessing.Process(O1); // Finding the match
// Finding the match
ArrayList<Order> matched = orderProcessing.Process(O1);
// Producing the Matched Orders
System.out.println("Matched Orders are : "); System.out.println("Matched Orders are : ");
orderProcessing.matched(matched); orderProcessing.matched(matched);
//Producing the Pending Orders
System.out.println("\nPending Orders till now are : "); System.out.println("\nPending Orders till now are : ");
orderProcessing.pending();
orderProcessing.pending(matched); //Pending orders. //Asking for New 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