Commit 4e5b4baf authored by SAURABH GUPTA's avatar SAURABH GUPTA

Adding Processing and Pending Classes and modifying the logic for

efficiency
parent 1895fb94
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>
/**
*
*/
package Order;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.plaf.synth.SynthSpinnerUI;
/**
* @author guptas
*
*/
class Order {
String ord_ID;
String type;
String product;
String pro_ID;
int price;
int qty;
String cust_ID;
void read() {
Scanner s = new Scanner(System.in);
this.ord_ID = s.nextLine();
this.type = s.next();
this.product = s.next();
this.price = s.nextInt();
this.qty = s.nextInt();
this.cust_ID = s.next();
}
void show() {
System.out.println("OrderID :" + this.ord_ID);
System.out.println("Type :" + this.type);
System.out.println("Product :" + this.product);
System.out.println("Price :" + this.price);
System.out.println("Quantity :" + this.qty);
System.out.println("CustomerID :" + this.cust_ID);
}
public void Process(ArrayList<Order> orders) {
int lenth = orders.size();
if("Sell".equalsIgnoreCase(this.type))
{
int i = 0;
while(lenth-- > 0)
{
Order o = orders.get(i++);
if( ("Buy".equalsIgnoreCase(o.type)) && (o.product.equalsIgnoreCase(this.product)) && (o.price >= this.price))
{
if(o.qty > this.qty)
{
o.qty -= this.qty;
// orders.add(i-1, o);
System.out.println("break");
break;
}
else
{
this.qty -= o.qty;
// orders.add(lenth, this);
System.out.println("continue");
continue;
}
}
}
}
else if(this.type == "Buy" || this.type == "buy")
{
int i = 0;
while(lenth-- > 0)
{
Order o = orders.get(i++);
if( (o.type == "Sell" || o.type == "sell") && (o.product.equals(this.product)) && (o.price <= this.price))
{
if(o.qty > this.qty)
{
o.qty -= this.qty;
// orders.add(i-1, o);
System.out.println("break");
break;
}
else
{
this.qty -= o.qty;
// orders.add(lenth, this);
System.out.println("continue");
continue;
}
}
}
}
}
public void pending(ArrayList<Order> orders) {
// TODO Auto-generated method stub
int lenth = orders.size();
System.out.println("Number of orders left : " + lenth);
int i=0;
while(lenth-- > 0)
{
System.out.println("\nORDER #"+(i+1));
Order o = orders.get(i++);
o.show();
}
}
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char choice = 'n';
int i = 0;
ArrayList<Order> orders = new ArrayList<Order>();
do {
Order O1 = 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
O1.Process(orders); // Finding the match
System.out.println("\nPending Orders till now are : ");
O1.pending(orders); //Pending orders.
System.out.println("\nWant to add new order : [y/n] \n ");
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
choice = s.next().charAt(0);
} while (choice == 'y');
}
}
/**
*
*/
package Order;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.plaf.synth.SynthSpinnerUI;
/**
* @author guptas
*
*/
public class Order {
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();
this.type = s.next();
this.product = s.next();
this.price = s.nextInt();
this.qty = s.nextInt();
this.cust_ID = s.next();
}
public void show() {
System.out.println("OrderID :" + this.ord_ID);
System.out.println("Type :" + this.type);
System.out.println("Product :" + this.product);
System.out.println("Price :" + this.price);
System.out.println("Quantity :" + this.qty);
System.out.println("CustomerID :" + this.cust_ID);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return ord_ID+","+type+","+product+","+price+","+qty+","+cust_ID;
}
}
package Order;
import java.util.ArrayList;
public class OrderProcessing {
private ArrayList<Order> orderQueue = new ArrayList<Order>();
public void Process( Order order) {
int lenth = orderQueue.size();
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) {
o.qty -= order.qty;
order.qty = 0;
break;
} else {
order.qty -= o.qty;
o.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)) {
if (o.qty >= order.qty) {
o.qty -= order.qty;
order.qty = 0;
break;
} else {
order.qty -= o.qty;
o.qty = 0;
continue;
}
}
}
}
orderQueue.add(order);
System.out.println(orderQueue);
}
public ArrayList<Order> getOrderQueue() {
return orderQueue;
}
public void pending() {
// TODO Auto-generated method stub
int lenth = 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++);
if(order.qty != 0) order.show();
}
}
}
package main;
import java.util.ArrayList;
import java.util.Scanner;
import Order.Order;
import Order.OrderProcessing;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char choice = 'n';
int i = 0;
OrderProcessing orderProcessing = new OrderProcessing();
do {
Order O1 = 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
orderProcessing.Process(O1); // Finding the match
System.out.println("\nPending Orders till now are : ");
orderProcessing.pending(); //Pending orders.
System.out.println("\nWant to add new order : [y/n] \n ");
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
choice = s.next().charAt(0);
} while (choice == 'y');
}
}
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