Task Description
In this assignment you will develop a Stock Exchange application in the Java programming language. Use the
examples below and the Javadoc found under resources on EdStem to write the program to specification.
You are encouraged to ask questions on Ed using the assignments category. As with any assignment, make
sure that your work is your own, and you do not share your code or solutions with other students.
You can work on this assignment on your own computer or the lab machines. It is important that you
continually back up your assignment files onto your own machine, external drives, and in the cloud.
You are encouraged to submit your assignment on Ed while you are in the process of completing it. By
submitting you will obtain some feedback of your progress on the sample test cases provided.
Write a program in Java that implements the Stack Exchange application as documented in the Javadoc on
EdStem. You can assume that our test cases will contain only valid input commands and not cause any integer
overflows. Commands are case insensitive.
The Exchange stores details on all the registered traders, as well as a reference to the matching engine
(market) used to process orders and trades. Traders are identified by their ID, and store both their balance
(in dollars) as well as their inventory, which tracks the products and quantities that the trader possesses.
The Market stores collections of orders (within sell and buy books, for sell and buy orders respectively) and
trades. An order can be made by a trader to buy or sell an amount of a product for a given price per unit.
Each order has a unique ID hexadecimal String of 4 characters. Order IDs begin at 0000 and increment up
(such that the second order will have ID 0001). Note that as the IDs use hexadecimal values, after 0009 is
000A, and after 000F is 0010.
A trade is for a single product for a set amount at a given price per unit and occurs between a sell order and
a buy order. Each trade represents a completed transaction in which funds and product have been
transferred between traders.
The Market utilises the Price-Time Priority Algorithm (explained below) when a new order is made to the
engine. When an order is created, it is initially open. The Price-Time Priority Algorithm then determines the
best order from the corresponding book (sell book for a new buy order, buy book for a new sell order), if any,
with which a trade can be made. If a trade can be made, the price from the existing order is used, and the
minimum amount of the two orders is transferred. After the money and product is transferred, if one or both of the orders no
longer require any more amount, the order(s) is closed and is removed from the order book. If an order
cannot be matched, or remains open after going through one or more trades, it is added to the market’s
order book. Examples of this process are provided below.
Your program should only be comprised of Exchange.java, Market.java, Trader.java, Trade.java and
Order.java. Do not modify any of the existing method signatures found in the documentation. You are
encouraged to write your own additional methods to improve style and reduce repetitive code. Your program
must produce no errors when built and run. Your program will read from standard input and write to standard
output.
Your program output must match the exact output format shown in the examples below. You are encouraged
to submit your assignment while you are working on it, so you can obtain some feedback.
In order to obtain full marks, your program will be checked against automatic test cases, manually inspected
by your tutors and you must submit a set of test cases that ensure you have implemented functionality
correctly.
The Price-Time Priority Algorithm first prioritises by price, and then uses time to differentiate between orders
at the same price. If a sell order is made, the algorithm will find a corresponding buy order for the same
product with the highest price greater or equal to the price specified by the sell order. If there are multiple
such buy orders, the earliest one is used.
If a buy order is made, the algorithm will find a corresponding sell order for the same price with the lowest
price less than or equal to the price specified by the buy order. If there are multiple such sell orders, the
earliest one is used.
Initially both the buy book and the sell book are empty. Let there be 5 traders in the market, with ID’s trader1,
trader2, trader3, trader4 and trader5, each with $100 dollars balance, and assume that trader1 has imported
100 units of product ABC.
trader2 wants to buy ABC and decides to make a buy order for it. They want to purchase 10 units, and the
most they are willing to pay per unit is $7.50. They hence make a buy order for 10 units of ABC at $7.50 per
unit. As the sell book is currently empty, no trades can be made, so the order is added unchanged to the buy
book.
trader3 also wishes to buy some ABC and hence also makes a buy order for it. They want to purchase 5 units,
and the most they are willing to pay per unit is $10.00. They hence make a buy order for 5 units of ABC at
$10.00 per unit. As the sell book is still empty, no trades can be made, so the order is added unchanged to
the buy book, which now has trader2’s order (with ID 0000) and trader3’s order (with ID 0001).
trader4 realises that ABC must be a popular product and will probably rise in value soon, so hence decides to
make a buy order for it. They want to purchase 15 units, but for a maximum price per unit of $5.00. They
hence make a buy order for 15 units of ABC at $5.00 per unit.
trader5 comes to the same conclusion as trader4 and makes a buy order for 20 units of ABC with a maximum
price per unit of $7.50.
trader1 notices the interest in the market for ABC and decides to sell all of their stock. They decide to make
a sell order for 100 units of ABC, with the lowest price they will accept be $6.50. The buy book is currently
not empty, so the Price-Time Priority Algorithm gets to work to determine whether any trades can be made.
Currently the buy book is as follows:
Order ID Product Trader Amount Price per unit $
0000 ABC trader2 10 7.50
0001 ABC trader3 5 10.00
0002 ABC trader4 15 5.00
0003 ABC trader5 20 7.50
As the algorithm is attempting to match with a buy order, it will first find the order(s) with the highest price
per unit (note that if it were trying to match with a sell order, it would find the orders with the lowest price).
Order 0001 has the highest price per unit at $10.00. As this price is above the minimum price specified by the
sell order, a trade is possible. 0001 only wants to purchase 5 units, hence trader1 transfers 5 units of ABC to
trader3, and trader3 transfers 5x$10 back to trader1, completing the trade. trader1 now has a balance of
$150, and trader3 a balance of $50.
As there are still 95 units of ABC yet to be sold, the sell order is not closed. Instead, the algorithm attempts
to make another trade. The buy book is now as follows:
Order ID Product Trader Amount Price per unit $
0000 ABC trader2 10 7.50
0002 ABC trader4 15 5.00
0003 ABC trader5 20 7.50
There are now two orders with the highest price, 0000 and 0003 with a price per unit of $7.50. To
differentiate between the two, the algorithm prioritises by time and chooses the earliest made order, which
is 0000 from trader2. As the price is above the minimum price specified by the sell order, a trade can be
made. trader1 transfers 10 units of ABC to trader2, and trader2 transfers 10x$7.50 back to trader1.
There are still 85 units of ABC to be sold, so the algorithm continues to try making more trades. The buy book
is now as follows:
Order ID Product Trader Amount Price per unit $
0002 ABC trader4 15 5.00
0003 ABC trader5 20 7.50
The highest priced order is 0003 at $7.50 per unit, which is above the minimum price for the sell. A trade is
hence made for 20 units of ABC at $7.50 per unit, with 20 units transferred to trader5 and 20x$7.50
transferred to trader1.
The sell order remains open as there are still 65 units to be sold. The algorithm continues, this time with the
buy book as follows:
Order ID Product Trader Amount Price per unit $
0002 ABC trader4 15 5.00
The only order in the book has a price per unit of $5.00, which is below the minimum price specified by the
sell order. A trade hence cannot be made.
As the algorithm is unable to identify an order to make a trade with, trader1’s sell order cannot be closed at
the current stage and instead is added to the sell book. The remaining 65 units of ABC in the order are
transferred to the market. Note that this is not the same for when a buy order goes unmatched (such as
with trader2’s buy order from earlier), in which case the money is transferred at the time of sale.