Commit bd7df177 authored by Rohit Prasad's avatar Rohit Prasad

Execution code for the program

parent b5779b55
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int time = 0;
OrderMatching orderMatching = new OrderMatching();
do {
System.out.println("Provide input in this format:");
System.out.println("type,from,stock,qnt,price");
String line = br.readLine();
String[] inputs = line.split(",");
Customer c = new Customer(inputs[1]);
Stock stock = new Stock(inputs[2]);
Order order;
if ("sell".compareToIgnoreCase(inputs[0]) == 0) {
order = new SellOrder(time++, c, stock, Integer.parseInt(inputs[3]), Integer.parseInt(inputs[4]));
} else {
order = new BuyOrder(time++, c, stock, Integer.parseInt(inputs[3]), Integer.parseInt(inputs[4]));
}
// check if order is valid
if (!orderMatching.validate(order)) {
System.out.println("Invalid order provided!");
}
ArrayList<Order> matchedOrders = orderMatching.findMatchedOrders(order);
if (!matchedOrders.isEmpty()) {
System.out.print("Following trades happened for " + order.getType() + " order: ");
System.out.println(order);
int count = 1;
for (Order o : matchedOrders) {
System.out.print(count++ + ": ");
System.out.println(o);
}
}
System.out.println();
} while (true);
}
}
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