Class Stock


  • public class Stock
    extends Object
    Represents a stock in the SafeTrade project
    • Field Detail

      • stockSymbol

        private String stockSymbol
      • companyName

        private String companyName
      • loPrice

        private double loPrice
      • hiPrice

        private double hiPrice
      • lastPrice

        private double lastPrice
      • volume

        private int volume
    • Constructor Detail

      • Stock

        public Stock​(String symbol,
                     String name,
                     double price)
        Constructs a new stock with a given symbol, company name, and starting price. Sets low price, high price, and last price to the same opening price. Sets "day" volume to zero. Initializes a priority qieue for sell orders to an empty PriorityQueue with a PriceComparator configured for comparing orders in ascending order; initializes a priority qieue for buy orders to an empty PriorityQueue with a PriceComparator configured for comparing orders in descending order.
        Parameters:
        symbol - the stock symbol.
        name - full company name.
        price - opening price for this stock.
    • Method Detail

      • getQuote

        public String getQuote()
        Returns a quote string for this stock. The quote includes: the company name for this stock; the stock symbol; last sale price; the lowest and highest day prices; the lowest price in a sell order (or "market") and the number of shares in it (or "none" if there are no sell orders); the highest price in a buy order (or "market") and the number of shares in it (or "none" if there are no buy orders). For example:
         Giggle.com (GGGL)
         Price: 10.00  hi: 10.00  lo: 10.00  vol: 0
         Ask: 12.75 size: 300  Bid: 12.00 size: 500
        Or: Giggle.com (GGGL) Price: 12.00 hi: 14.50 lo: 9.00 vol: 500 Ask: none Bid: 12.50 size: 200
        Returns:
        the quote for this stock.
      • placeOrder

        public void placeOrder​(TradeOrder order)
        Places a trading order for this stock. Adds the order to the appropriate priority queue depending on whether this is a buy or sell order. Notifies the trader who placed the order that the order has been placed, by sending a message to that trader. For example:
         New order:  Buy GGGL (Giggle.com)
         200 shares at $38.00
        Or:
         New order:  Sell GGGL (Giggle.com)
         150 shares at market
        Executes pending orders by calling executeOrders.
        Parameters:
        order - a trading order to be placed.
      • executeOrders

        protected void executeOrders()
        Executes as many pending orders as possible.
        1. Examines the top sell order and the top buy order in the respective priority queues.
        i) If both are limit orders and the asking price is less than or equal to the selling price, executes the order (or a part of it) at the sell order price.
        ii) If one order is limit and the other is market, executes the order (or a part of it) at the limit order price
        iii) If both orders are market, executes the order (or a part of it) at the last sale price.
        2. Figures out how many shares can be traded, which is the smallest of the numbers of shares in the two orders.
        3. Subtracts the traded number of shares from each order; Removes each of the orders with 0 remaining shares from the respective queue.
        4. Updates the day's low price, high price, and volume.
        5. Sends a message to each of the two traders involved in the transaction. For example:
            You bought: 150 GGGL at 38.00 amt 5700.00
        6. Repeats steps 1-5 for as long as possible, that is as long as there is any movement in the buy / sell order queues. (The process gets stuck when the top buy order and sell order are both limit orders and the ask price is higher than the bid price.)