Reference no: EM131843273
Assignment 1: Stock market in Java
Description
A stock market is a platform that has stocks listed on it and investing accounts can use the platform to trade stocks. The current value of a stock on the market is actually the price of the last trade of that stock that occurred on the market. In order to trade stocks, investors (represented here by investing accounts) need to signal their intention to buy (make a bid) or to sell (asking for a certain price) to the stock market. Bids and asks are usually referred to as "orders".
Sending a "bid" to the stock market means that you are sending to the market information about how much of a particular stock you want to buy and what price you are willing to pay per stock. Sending an "ask" to the stock market means that you are sending to the market information about how much of a particular stock you want to sell and what price you are asking for.
The stock market, as a platform/system, has the responsibility to monitor all the bids and asks that it receives, and make transactions happen. Transactions are made if and only if, for a particular stock, there is a bid (order to buy) with a price equal or superior to an asking price (order to sell). For example, let's assume that we have a stock named COMP, and this is the list of bids and asks for this stock:
Bids:
- Account #111111: 100 of COMP at $10.50
- Account #111112: 100 of COMP at $10.20
- Account #111113: 100 of COMP at $10.10
Asks:
- Account #111114: 100 of COMP at $10.75
- Account #111115: 100 of COMP at $11.00
- Account #111116: 100 of COMP at $12.50
At this point in time, no transaction can be made, because the bids are too low (or if you look at it the other way around, the asks are too high).
Let's assume now that a new ask is sent to the system: "Account #111117: 100 of COMP at $10.40". Now the system can make a transaction, because there is a bid with a price ($10.50) >= to the price of an ask ($10.40). The price of the transaction is always going to be set to the best possible price for the last order (bid or ask) that comes in and triggers the transaction. In our example, the last offer to come in is an ask at $10.40, so the price of that transaction will be $10.50, because there was already someone in the system who was offering to buy at $10.50.
When multiple orders are received by the system at the same price, priority will always be given to the order that arrived first.
You can assume the following conditions:
1. Investing accounts can only send an ask or a bid for one particular stock once: duplicate entries are ignored.
2. Investing accounts can only send asks (orders to sell) for a particular stock if they own the stock: if the investing account does not hold the stock, the order is invalid.
3. Investing accounts can only send bids (orders to buy) for a particular stock if they can afford it: if the balance is not high enough to make the purchase (including the transaction fee), the order is invalid.
4. To make things simpler, all the orders to buy and to sell will be of the same number of stocks (100 in the above example).
5. Bids and asks stay on the system until a transaction is made, or a request to remove them is made. This means that if a particular investing account has multiple bids for different stocks on the system, and they all end up being processed, it is possible that this account will get a negative balance.
6. Each time a transaction is completed, there is a fee of $9.99 (for both accounts involved).
Actions
There are seven actions and one comment that the system should recognize. For each action, an example is given as well as a list of outcomes (what can happen as a result of the command). Your program must be able to read and process all the actions (operations) provided in a text file given as a command-line argument.
STOCK [STOCK_SYMBOL]
- This action adds a new stock to the system. It sets the initial price to 0. The bids and asks for this stocks are initially empty.
- Example: STOCK COMP
- Outcomes: CONFIRMED, DUPLICATE
- A stock is a duplicate if there is already a stock with this symbol in the system.
- The symbol is usually composed of three of four capital letters (no whitespaces).
NEW [ACCOUNT#] [BALANCE] [OWNED_STOCKS]
- This action adds a new investing account to the system.
- [ACCOUNT#] is going to be a valid int.
- [BALANCE] is going to be a valid double. It corresponds to the starting balance in the account.
- [OWNED_STOCKS] is going to be a list of triples representing all the stocks that are owned in the account. The three elements in the triple will be: (1) the stock symbol, (2) the number of stocks and (3) the price for each stock, separated by dashes (-). A whitespace separates each triple.
For example, [OWNED_STOCKS] could be: COMP-100-10.55 MATH-100-5.55
[OWNED_STOCKS] can also be empty, if the account does not hold any stock at the moment of creation.
- Example: NEW 111111 10000.10 COMP-100-10.55 MATH-100-5.55
- Outcomes: CONFIRMED, DUPLICATE, STOCK NOT FOUND
- If the investing account already exists (there is already an account with the same account number), the DUPLICATE error should be reported.
- All stocks in [OWNED_STOCKS] must already be in the system. If not, the STOCK NOT FOUND error should be reported.
ADD [ASK_OR_BID] [ACCOUNT#] [SYMBOL] [NB_STOCKS] [PRICE]
- This action adds an order (order to sell (ask) or to buy (bid)) to the system, from a specific account number (int), for a specific stock symbol (string), for a certain number of stocks (int) and for a certain price (double).
- [ASK_OR_BID] is a string and can either be "ASK" or "BID".
- Example: ADD ASK 111111 COMP 100 10.50
- Outcomes: ACCOUNT NOT FOUND, STOCK NOT FOUND, INVALID ORDER, DUPLICATE, CONFIRMED, TRANSACTION COMPLETED
- ACCOUNT NOT FOUND is used when the account number doesn't exist.
- STOCK NOT FOUND is used when the stock doesn't exist.
- INVALID ORDER is used for two different situations, depending on whether the order is an ask or a bid:
- if the order is an ask: INVALID ORDER is used when the investing account does not currently hold the stock (cannot offer to sell what you don't own).
- if the order is a bid: INVALID ORDER is used when the balance of the investing account isn't high enough to buy the stocks and cover the transaction fees.
- DUPLICATE is used when the account already has an ask or a bid order for this stock (no duplicates allowed, i.e. an investing account cannot have more than 1 ask or 1 bid for the same stock).
- CONFIRMED is used when the order is successfully added to the system.
- TRANSACTION COMPLETED is used if, after the order has been confirmed, it triggers a transaction. The system should obviously deal with the transaction at this time, which means: (1) setting the price of the transaction (following the rules described earlier), (2) adding the stocks to list of stocks owned by the buyer, (3) removing the stocks from the list of stocks owned by the previous owner, (4) updating the balances of both investing accounts, including the transaction fees and (5) removing both the bid and ask orders that triggered the transaction from the system.
REMOVE [ASK_OR_BID] [ACCOUNT#] [SYMBOL]
- This action removes an ask order from the system, for a specific account number (int) and for a specific stock symbol (string). Price and number of stocks is not necessary here, since only one ask can be in the system for each account.
- [ASK_OR_BID] is a string and can either be "ASK" or "BID".
- Example: REMOVE BID 111111 COMP
- Outcomes: ACCOUNT NOT FOUND, STOCK NOT FOUND, ORDER NOT FOUND, CONFIRMED
- ACCOUNT NOT FOUND is used when the account number doesn't exist.
- STOCK NOT FOUND is used when the stock doesn't exist.
- ORDER NOT FOUND is used when there is no order of this type (type refers to [ASK_OR_BID]) from that account for that stock.
- CONFIRMED is used when the order was successfully removed from the system.
BALANCE [ACCOUNT#]
- This action prints the status of an investing account: what is the current balance? Which stocks are owned and at what price?
- Example BALANCE 111111
- Outcomes: NOT FOUND (if the account number doesn't exist), or an output is produced.
- if an account is in the system, here is an example of an output: Account #111111 has a balance of $1500.01 and owns these stocks:
-Account #111111: 100 of COMP at $10.50.
-Account #111111: 100 of MATH at $5.55.
-Account #111111: 100 of BIO at $6.68.
STATUS [STOCK_SYMBOL]
- This action prints the status of a stock: what is the current price of the stock (based on the last trade)? What are the bids and asks for this stock?
- Example STATUS COMP
- Outcomes: NOT FOUND (if the stock doesn't exist), or an output is produced.
- If the stock is valid, here is an example of an output: Stock: COMP has a current price of $10.50.
Here is the list of bids:
-Account #111111: 100 of COMP at $10.50.
-Account #111112: 100 of COMP at $10.20.
-Account #111113: 100 of COMP at $10.10. Here is the list of asks:
-Account #111114: 100 of COMP at $10.75
-Account #111115: 100 of COMP at $11.00
-Account #111116: 100 of COMP at $12.50
QUIT
- This ends the program.
- Outcome: the program ends. The message DONE should be printed.
#
- any line that starts with # is a comment.
- example #this is a comment
- if a line is a comment, it does not affect the system in any way.
- all comments should be echoed to output.
Additional notes on the operations:
- One account can have (not more than) one bid and (not more than) one ask order for the same stock at a time. The system does not need to check before making a transaction if the buyer is the same account as the seller (so it's possible for an account to sell to itself).
- An account can hold multiple times the same stock (e.g. 100 of COMP at 9.95 and 100 of COMP at 10.55). If it is the case, you should not combine them together (e.g. no need to combine in order to get 200 of COMP at 10.25).
Input Error Checking
When the format is specified for a command, you can assume all the stated properties hold (i.e., stock symbols will all have the proper format, integers will be valid, doubles will be valid, etc.). Your code does not need to do error checking for these properties.
Data Structures
There are some restrictions on data structures you must use for the assignment. Do not use ANY built-in Java collection classes (e.g. ArrayLists, Hashes, ...) or Java Generics in this assignment: any linked structures must be your own, built from scratch. You should not use arrays other than for temporary operations (e.g., split() for strings).