Q. Given the following code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class TradeMatcher { private final Scanner in; private final BufferedWriter out; public TradeMatcher(Scanner in, BufferedWriter out) { this.in = in; this.out = out; } /* * store state */ //********** TO BE COMPLETED ************* /** * Matching Trade * @param trade * @return */ public List<Long> match(Trade trade) { //********** TO BE COMPLETED ************* return new ArrayList<Long>(); } private void processTrades() throws IOException { while (in.hasNext()) { Trade order = readTrade(); List<Long> matchingIds = match(order); outputMatches(matchingIds); } out.close(); } private Trade readTrade() { final long tradeId = in.nextLong(); final String securitySymbol = in.next(); final Side customerSide = Side.valueOf(in.next()); final long size = in.nextLong(); return new Trade(tradeId, securitySymbol, customerSide, size); } private void outputMatches(List<Long> matchingIds) throws IOException { int length = matchingIds.size(); out.write(matchingIds.size() + " matches found. Matching ids are: ["); for (int i = 0; i < length; i++) { out.write(String.valueOf(matchingIds.get(i))); if (i < length - 1) { out.write(','); } } out.write("]"); out.newLine(); } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); TradeMatcher m = new TradeMatcher(in, out); m.processTrades(); } //inner class private static class Trade { final long tradeId; // Unique id final String securitySymbol; //if symbol matches & side differs then trade matches final Side side; //BUY or SELL long size; //trade QTY public Trade(long orderId, String securitySymbol, Side customerSide, long size) { this.tradeId = orderId; this.securitySymbol = securitySymbol; this.side = customerSide; this.size = size; } } private enum Side { BUY, SELL } } |
Fill in required code in the “//********** TO BE COMPLETED *************” section within the “match(Trade trade)” method and in the variables declaration section at the top. Assume that this object is only accessed by a single thread. … Read more ›...