Commit 584b13ad authored by karan-saroya's avatar karan-saroya

Completed the basic Algorithm

parent 86cbf2f0
/StockMarket.class /StockMarket.class
/Jtest.class /Jtest.class
/Order.class
/StockMarket$Order.class
...@@ -8,9 +8,7 @@ class Jtest { ...@@ -8,9 +8,7 @@ class Jtest {
@Test @Test
void test() { void test() {
StockMarket obj = new StockMarket();
int res =obj.Add(100, 200);
assertEquals(300, res);
} }
} }
import java.util.*;
import java.io.*;
public class StockMarket public class StockMarket
{ {
public int Add(int a, int b)
ArrayList<Order> pending_list;
public static void main(String[] args) throws Exception
{ {
return a+b; int count=1;
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
StockMarket stock_market = new StockMarket();
stock_market.pending_list = new ArrayList<Order>();
while(true)
{
System.out.println("Press 1 to Add an Order");
System.out.println("Press 2 to View the pending list");
System.out.println("Press 3 to Exit");
String s=buf.readLine();
if(s.equals("1"))
{
boolean is_trade=stock_market.AddOrder(count,buf);
if(is_trade)
System.out.println("Trade Made\n");
else
System.out.println("Added to Pending List\n");
count++;
}
else if(s.equals("2"))
{
stock_market.ViewList();
}
else
{
System.exit(0);
}
}
} }
void ViewList()
public static void main(String[] args)
{ {
System.out.println("Here"); if(pending_list.size() == 0)
{
System.out.println("List is empty\n");
}
else
{
for(int i=0;i<pending_list.size();i++)
{
System.out.println(pending_list.get(i).toString());
System.out.println("");
}
}
} }
boolean AddOrder(int count,BufferedReader buf) throws Exception
{
boolean trade=false;
System.out.println("Enter your Customer ID");
int cid=Integer.parseInt(buf.readLine());
System.out.println("Enter the Stock Name");
String sname=(buf.readLine());
sname=sname.toLowerCase();
System.out.println("Enter your Trade Type(Buy/Sell)");
String type = buf.readLine();
type= type.toLowerCase();
while(!(type.equals("buy") || type.equals("sell")))
{
System.out.println("Please Enter Buy or Sell");
type = buf.readLine();
type= type.toLowerCase();
}
System.out.println("Enter your Price");
int price = Integer.parseInt(buf.readLine());
System.out.println("Enter your Quantity");
int quant = Integer.parseInt(buf.readLine());
Order new_order= new Order(count,cid,sname,type,price,quant);
boolean ans=MatchOrder(new_order);
if(ans == true)
trade=true;
return trade;
}
boolean MatchOrder(Order new_order)
{
boolean did_match=false;
if(new_order.getTradeType().equals("buy"))
{
for(int i=0;i<pending_list.size();i++)
{
Order order = pending_list.get(i);
if(order.getCustID() != new_order.getCustID() && order.getTradeType().equals("sell") && new_order.getStockName().equals(order.getStockName()) &&order.getPrice() <= new_order.getPrice() )
{
if(order.getQuantity() > new_order.getQuantity())
{
order.setQuantity(order.getQuantity()-new_order.getQuantity());
did_match=true;
break;
}
else if(order.getQuantity() == new_order.getQuantity())
{
pending_list.remove(i);
did_match=true;
break;
}
else
{
pending_list.remove(i);
new_order.setQuantity(new_order.getQuantity()-order.getQuantity());
i--;
}
}
}
}
else
{
for(int i=0;i<pending_list.size();i++)
{
Order order = pending_list.get(i);
if(order.getCustID() != new_order.getCustID() && order.getTradeType().equals("buy")&& new_order.getStockName().equals(order.getStockName()) && order.getPrice() >= new_order.getPrice() )
{
if(order.getQuantity() > new_order.getQuantity())
{
order.setQuantity(order.getQuantity()-new_order.getQuantity());
did_match=true;
break;
}
else if(order.getQuantity() == new_order.getQuantity())
{
pending_list.remove(i);
did_match=true;
break;
}
else
{
pending_list.remove(i);
new_order.setQuantity(new_order.getQuantity()-order.getQuantity());
i--;
}
}
}
}
if(did_match == false)
{
pending_list.add(new_order);
}
return did_match;
}
}
class Order
{
int order_ts; // Timestamp
int cust_id; // Customer Id
String stock_name; // Stock ID
String trade_type; // B for Buy, S for Sell
int price;// The price at which the trade should proceed
int quantity; // How many stocks to buy or sell
public Order(int ts,int cid,String sid,String type,int cost,int quant)
{
this.order_ts=ts;
this.cust_id=cid;
this.stock_name=sid;
this.trade_type=type;
this.price=cost;
this.quantity=quant;
}
public int getOrderTS()
{
return this.order_ts;
}
public int getCustID()
{
return this.cust_id;
}
public String getStockName()
{
return this.stock_name;
}
public String getTradeType()
{
return this.trade_type;
}
public int getPrice()
{
return this.price;
}
public int getQuantity()
{
return this.quantity;
}
public void setPrice(int nprice)
{
this.price=nprice;
}
public void setQuantity(int nquantity)
{
this.quantity=nquantity;
}
public String toString()
{
String str="";
str=order_ts+","+cust_id+","+stock_name+","+trade_type+","+price+","+quantity;
return str;
}
} }
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