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

Fully Tested and Commented Code

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