Due: Wednesday, March 19, 2008, 11:59pm
Authors: Adnan Aziz, Gayatri Ramachandran
Keep an eye on the changelog.
In this lab you will build a class that implements a stock quote server.
The server gets two types of requests: updates and queries.
These requests are in the form of text strings, and the response is in the form of text strings.
An update is an offer to sell or buy a certain quantity of stock at a certain price, or a change in the quantity of stock for an existing offer. The update format is timestamp id type symbol quantity unit-price
| Field | Meaning | Example |
|---|---|---|
| timestamp | number of milliseconds since the start of the day's trading | 12340100 |
| id | unique string associated with the offer | 246 |
| type | buy, sell, change, withdraw, or halt | B |
| symbol | a 1-4 character string specifying the company being traded | ATT |
| quantity | positive integer denoting how many shares are being offered | 900 |
| unit-price | positive real number, with two digits after the decimal place | 270.12 |
1232111 131 B GOOG 1000 270.12 is an offer to buy 1000 shares of Google (GOOG) at $270.12 a share.2212313 246 S IBM 100 111.01 is an offer to sell 100 shares of IBM (IBM) at $111.01 a share.1411001 131 U GOOG 900 270.01 is an update to the order with id 131 indicating that there are now only 900 shares on offer at a price of $270.01 1507981 131 W says that the order with id 131 has been withdrawn89123123 812312 H tells the server to halt operation and recover all allocated memory
IMPORTANT The server is not matching buyers and sellers. In particular, if 1232111 131 S GOOG 1000 270.12 is followed by 1232112 132 B GOOG 1000 270.12, the two do not cancel out - both are active. Think of the code you are writing as targeting a trader who wants to observe the state of the market. (If the two orders are matched by the market, then your server will get two withdraw updates, one for each offer.)
A query is specified as a string. The format is timestamp id type symbol quantity unit-price
The timestamp, id, symbol, quantity, and price fields are as before.
| Type | Definition |
|---|---|
| QB | best price to purchase given quantity of symbol (price is a don't care) |
| QBM | is it possible to purchase given quantity of symbol, without paying more per unit than price? |
| QBT | track the availability of specified quantity of symbol at or better than designated price |
| QBTO | turn off tracking of query with given id |
1232131 34 QB GOOG 100 0 queries the best price to buy 100 shares of Google 1402122 777 QBM GOOG 100 271.11 queries the best price to buy 100 shares of Google, without paying more than $271.11 a share1412313 1231 QBT GOOG 100 271.11 track the best price to buy 100 shares of Google, without paying more than $271.11 a share2231112 1231 QBTO stop tracking query with id 1231For each buy query, there is an analogous sell query.
While tracking a query, you should not output a response after each update. You need to output the tracking result only after something has happened related to the stock you are tracking. For instance, if we are tracking a sell for GOOG, then the result for this will be output only after there is an addition, update or withdraw related to a buy request for GOOG.
You will need to handle two kinds of errors for this lab:
Error: Duplicate id, and discard the request.Error: Invalid id, and discard the message. This error occurs under the following conditions:
Assume that the server is reading queries and updates one per line from stdin and writing its output to stdout The format of the output is given below. Lines starting with —- are the lines on standard output, and the text from the # symbol to the end of the line is a comment that will not be present in the actual running.
1 1 QB GOOG 100 0 -- NA # indicates that 100 shares are not available 21 2 S GOOG 1000 271.01 37 3 S GOOG 100 270.00 44 5 QB GOOG 200 0 -- 54101.00 # 100 shares at 270.00 and 100 shares at 271.01 55 3 W 79 12 QB GOOG 200 0 -- 54202.00 # 200 shares at 271.01 121 15 S GOOG 1000 270.50 180 19 QBM GOOG 500 270.80 -- Yes # can fulfill such an order 201 27 QBT GOOG 1000 270.51 -- 27 Yes # can be fulfilled 270 31 B GOOG 1000 269.97 336 15 U GOOG 500 270.50 # only 500 shares are being offered now -- 27 No # cannot be fulfilled now 412 37 S IBM 700 170.02 512 41 S GOOG 700 269.11 -- 27 Yes # from the orders with id 41 and 15 661 27 QBTO 1243 1231 H # halt operations
1 1 B ATT 100 123.21 45 2 S ATT 100 123.21 50 3 QB ATT 200 0 -- NA #only 100 shares are available 234 4 QB ATT 50 0 -- 6160.50 #50 shares at 123.21 300 5 QB ATT 100 0 -- 12321.00 #100 shares at 123.21 334 6 QB ATT 100 0 #to show that queries do not update the server -- 12321.00 434 7 QB GOOG 50 0 -- NA 1000 8 B ATT 100 123.21 2431 9 B ATT 50 123.21 #to show that buyers and sellers aren’t matched up 1243 2 H #halt operations. id is a don't care
Note: (by Adnan) - the withdraws for id 1 at time 800 onwards should also be flagging an error
100 1 S IBM 500 543.21 110 1 B GOOG 45 676.97 -- Error: Duplicate id 200 1 S GOOG 50 680.86 -- Error: Duplicate id 242 1 QB IBM 500 0 -- Error: Duplicate id 246 1 QBM IBM 100 543.22 -- Error: Duplicate id 300 1 QBT IBM 150 543.20 -- Error: Duplicate id 310 1 W 330 1 B IBM 500 543.21 379 1 S IBM 500 543.21 -- Error: Duplicate id 400 1 W 489 1 S IBM 500 543.21 578 1 W 780 1 QB IBM 500 0 -- NA 800 1 W -- Error: Invalid id 810 1 QB IBM 500 32.53 #price is a don’t care -- NA 850 1 W -- Error: Invalid id 880 1 QBM IBM 500 544.00 -- No 900 1 W -- Error: Invalid id 923 1 QBT IBM 500 543.21 -- 1 No 940 2 S IBM 500 543.20 -- 1 Yes 945 2 W -- 1 No 940 2 S IBM 500 543.20 -- 1 Yes 950 3 QBM IBM 500 544.00 -- Yes 960 4 QB IBM 500 543.00 #price is a don’t care -- 271600.00 #500 shares at 543.20 970 5 QB GOOG 500 0 -- NA 987 123 H #id is a don’t care
154 1 B YHOO 1000 122.12 176 2 S GOOG 500 653.23 190 3 QB IBM 2000 234.53 -- NA 213 4 QBM YHOO 500 123.00 -- No 234 5 QBT GOOG 500 653.25 -- 5 Yes 256 1 U GOOG 500 652.50 320 2 U GOOG 100 650.50 -- 5 No 336 2 U GOOG 500 653.23 -- 5 Yes 367 3 U IBM 200 250.50 -- Error: Invalid id 390 4 U IBM 100 243.00 -- Error: Invalid id 425 5 U YHOO 300 132.50 -- Error: Invalid id 456 1 QBTO -- Error: Invalid id 466 2 QBTO -- Error: Invalid id 576 3 QBTO -- Error: Invalid id 588 4 QBTO -- Error: Invalid id 599 1 W 611 2 W -- 5 No 624 5 QBTO 643 3 W -- Error: Invalid id 657 4 W -- Error: Invalid id 667 5 W -- Error: Invalid id 682 1 U GOOG 500 650.50 -- Error: Invalid id 698 2 U GOOG 500 650.50 -- Error: Invalid id 762 5 QBTO -- Error: Invalid id 764 1 W -- Error: Invalid id 790 2 W -- Error: Invalid id 853 2 S GOOG 500 653.23 890 1 H
100 1 B ATT 100 123.70 107 2 S ATT 100 120.70 202 3 S ATT 100 124.70 222 4 QS ATT 50 123.21 #price is a don’t care -- 6185.00 #50 shares at 123.70 257 5 B ATT 100 122.70 298 6 QS ATT 150 0 -- 18505.00 #100 shares at 123.70 and 50 shares at 122.70 456 7 QSM ATT 150 123.50 -- No 486 8 QSM ATT 50 123.50 -- Yes 568 9 QSM GOOG 50 121.23 -- No 708 11 QSM ATT 150 123.35 -- Yes #100 shares at 123.70 and 50 shares at 122.70 764 12 QS IBM 150 0 -- NA 769 13 QST ATT 200 123.10 -- 13 Yes 864 1 W -- 13 No 890 1 B ATT 100 123.70 -- 13 Yes 894 13 QSTO 904 1 W 978 56 H
100 1 QB ATT 100 0 -- NA 101 1 QBM ATT 100 120.00 -- No 164 1 QS GOOG 100 0 -- NA 207 1 QSM ATT 100 120.00 -- No 242 1 QBT ATT 100 121.00 -- 1 No 276 1 QBTO 342 1 QST ATT 100 121.00 -- 1 No 376 1 QSTO 399 1 S ATT 100 118.00 415 2 B ATT 100 120.00 424 3 QBT ATT 100 119.00 -- 3 Yes 456 4 QBT ATT 100 117.00 -- 4 No 466 8 QST GOOG 100 120.00 -- 8 No 476 5 QST ATT 100 119.00 -- 5 Yes 487 6 QST ATT 100 121.00 -- 6 No 490 7 S ATT 100 117.00 -- 3 Yes -- 4 Yes 499 2 U GOOG 100 120.00 -- 5 No -- 6 No -- 8 Yes 500 2 U GOOG 100 119.00 -- 8 No 512 9 B GOOG 100 120.00 -- 8 Yes 523 10 QSM GOOG 150 119.70 -- No 524 10 QSM GOOG 150 119.60 -- Yes 525 10 QBM ATT 150 117.30 -- No 529 10 QBM ATT 150 117.40 -- Yes 567 125 H
399 1 S ATT 200 118.00 486 2 S ATT 100 118.00 543 3 B ATT 100 118.00 568 4 B ATT 50 118.00 586 5 QBM ATT 300 118.00 -- Yes 597 5 QSM ATT 150 118.00 -- Yes 654 5 QBT ATT 300 118.00 -- 5 Yes 676 6 QBT ATT 400 118.00 -- 6 No 712 7 QBT ATT 200 118.00 -- 7 Yes 743 2 W -- 5 No -- 6 No -- 7 Yes 754 8 QST ATT 150 118.00 -- 8 Yes 764 9 QST ATT 100 118.00 -- 9 Yes 875 10 QST ATT 200 118.00 -- 10 No 878 4 W -- 8 No -- 9 Yes -- 10 No 888 21 H
You are to code the class Qs implementing the server. It should have the following methods:
Start() - begin in interactive mode, i.e., read queries and updates from stdin, write results to stdoutStop() - stop processing, call the class destructors to recover memory (this method should be called when the update is of type halt H)NonInteractive() - this tells the server not to take inputs from stdin and write to stdout (by default, the server should be in interactive mode)Request(String aRequest) - this tells the server to process the request in aRequest (this method will be called only when the server is in non-interactive mode). The method should return the string that would ordinarily be printed to stdout in interactive mode (this string may span multiple lines, in which case your result should have '\n's in the approriate places)The inputs and outputs of the examples are given below. You could copy them into your directory and use redirection instead of typing in the inputs each time.
If you are working on a Linux machine, you could do the following to read in all the inputs from Input1 and print the output into my_output1:
java Qs < Input1 > my_output1
After this is done, you could type the command
diff my_output1 Output1
to check if you get the expected output.
| Input | Output |
|---|---|
| Input1 | Output1 |
| Input2 | Output2 |
| Input3 | Output3 |
| Input4 | Output4 |
| Input5 | Output5 |
| Input6 | Output6 |
| Input7 | Output7 |
You should base your implementation on BSTs - you can use any standard implementation (e.g., STL or the Java Collections API). You will likely need to augment the data structure to achieve reasonable performance.
/******************************************************************************** @file <filename> <Description> @author Fullname1(ECE login1) @date *******************************************************************************/
Example:
/******************************************************************************** @file Qs.java Implementing Simple Hash Map @author Adnan Aziz(adnan) @date 03/10/2008 *******************************************************************************/
along with your reason. You should also submit a 2-3 page writeup on the design of your algorithm.| Mar 20, 11:59pm | 100% of your score |
| Mar 21, 11:59pm | 95% of your score |
| Mar 22, 11:59pm | 90% of your score |
| After Mar 22, 11:59pm | No submissions accepted! |
halt on a newline to be more like queries and updates.