In Java, for this assignment you must extend the functionality of the double auc
ID: 652636 • Letter: I
Question
In Java, for this assignment you must extend the functionality of the double auction system
to allow for order cancellations.Buy and sell order matching should be done using the rules included in the description of the
main project. In addition to buy and sell orders, the input may also contain cancellation
orders. A cancellation order simply specifies the number of an order that must be cancelled
unless it has been matched already.Implementation requirements As for the main project, you may either use the priority queue classes provided by the Java API or implement your own. To receive full credit, your program should require O(logn) time per order when processing a list of n orders.
Program Input
Your program should read from the standard input a list of buy and sell orders in the format
given in the samples below, with one order per line.
Program Output
The program must print to the standard output the number of units matched and the total
clearing price. Use the format given in the examples below, and follow the output with a new
line. Note that after processing the input there can be orders left uncleared.
Sample Input 1
order 1 buy at 183
order 2 buy at 185
order 3 sell at 180
order 4 cancel order 1
order 5 sell at 182
Output for Sample Input 1
units exchanged 1 total clearing price 185
Sample Input 2
order 1 buy at 183
order 2 buy at 185
order 3 sell at 180
order 4 cancel order 2
order 5 sell at 182
Output for Sample Input 2
units exchanged 2 total clearing price 368
Explanation / Answer
import java.util.*;
import java.io.*;
public class HelloWorld
{
public static void main(String args[])
{
ArrayList<String> orders = new ArrayList<String>();
// Create sell and buys array list to get the list of
// buyers and sellers
ArrayList<String> sells = new ArrayList<String>();
ArrayList<String> buys = new ArrayList<String>();
int price = 0;
int exchange = 0;
// since we are using files put the execution code in try..catch
// block
try
{
Scanner input = new Scanner(new File("Orders1.txt"));
// read the lines from the text file
while (input.hasNextLine())
{
orders.add(input.nextLine());
}
System.out.println("Data in the sample Input file is: ");
for (int i = 0; i < orders.size(); i++)
{
System.out.println("" + orders.get(i));
if (orders.get(i).contains("sell"))
{
sells.add(orders.get(i));
}
if (orders.get(i).contains("buy"))
{
buys.add(orders.get(i));
}
}
int buy = 0;
int sell = 0;
for (int i = 0; i < buys.size(); i++)
{
buy = getPrice(buys.get(i));
for (int j = 0; j < sells.size(); j++)
{
sell = getPrice(sells.get(j));
if (buy == sell)
{
price += 2 * buy;
exchange += 2;
}
}
}
System.out.println(" Units of exchanged " + exchange + " total clearing price "
+ price);
} catch (Exception e)
{
e.printStackTrace();
}
}
public static int getPrice(String s)
{
Scanner in = new Scanner(s).useDelimiter("[^0-9]+");
int i = 0;
int j = 0;
while (in.hasNext())
{
i = in.nextInt();
j = in.nextInt();
}
return j;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.