JavaJavaMethods Methods ABAB
Data Structures
Maria Litvin
Gary Litvin
Copyright © 2003-2004 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.
TM
Data Structures in Action: A Case Study
CHPT 9.00 (+1.00)
9-2
Objectives:
● Get experience with a larger and more realistic software project, which models a toy stock exchange
● Review the data structures studied in this course
● Discuss issues involved in object-oriented design
● Practice working on a software project as a team
9-3
SafeTrade Application
● Implements a toy Stock Exchange
● Runs on a single computer
● Can be structured as a team development project
● Uses TreeSet, TreeMap, HashMap, ListQueue, and HeapPriorityQueue classes
● Reuses some of the code from the Java Messenger project (Section 5.8)
9-4
Stock Market Basics
● Stocks are listed on a stock exchange, such as NYSE (New York Stock Exchange)
● A particular stock is identified by its trading symbol (e.g., SUNW for Sun Microsystems or MSFT for Microsoft)
● Stocks are usually traded in multiples of 100
● Stock prices are in dollars and cents (can include fractions of cents in the real world)
● Online brokerage firms allow customers to trade stocks online from their computers
9-5
Stock Market Basics (cont’d)
● Buy order — an order to buy shares of stock
● Sell order — an order to sell shares of stock
● Limit order — specifies the maximum price for a buy or a minimum price for a sell
● Market buy order — an order to buy shares at the current market price (the lowest “ask” price)
● Market sell order — an order to sell shares at the current market price (the highest bid price)
9-6
SafeTrade Application (cont’d)
● Allows registered “users” to “trade” shares of stocks
● Users must login first; the program can register a new user
● Users place “buy” and “sell” orders and can get price quotes for stocks
● SafeTrade runs on a single computer; each active user opens a separate trading window on the screen
● SafeTrade does not keep track of cash or of the number of shares available on each account
9-7
SafeTrade Application (cont’d)
9-8
SafeTrade Application (cont’d)
● Stock prices are in dollars and cents
● The “exchange” keeps track of all buy and sell orders for each stock
● The “exchange” has a method to “list” (i.e., register) a new stock
● An order is executed when the highest bid is not less than the lowest “ask” (i.e., the asking price)
● In SafeTrade, all orders are partial orders: if only a portion of the order can be executed, then the largest possible number of shares is traded
9-9
SafeTrade Design
Structural Design
OO Design
Detailed Design
Data structures used
Classes and objects
Fields, constructors, and methods
9-10
SafeTrade Structural Design
Data Structure => interface => class
Registered traders BST => Map => TreeMap
Logged-in traders BST => Set => TreeSet
Mailbox for each trader Queue => Queue => ListQueue
Listed stocks Hash table => Map => HashMap
Sell orders for each stock Priority queue => PriorityQueue => HeapPriorityQueue (with ascending price comparator)
Buy orders for each stock Priority queue => PriorityQueue => HeapPriorityQueue (with descending price comparator)
9-11
Structural Design — Tradeoffs
Registered traders:
• large number (100,000s)• access time is not critical
BST (TreeMap)
Listed stocks:
• relatively small number• fast access time is critical
Hash table (HashMap)
OK to sacrifice some wasted space for better performance
9-12
SafeTrade OO Design
● Part 1: Trader registration and login• SafeTrade — small main class• GUILogin — reusable GUI for registration and login• Exchange — keeps track of the registered and
logged-in traders
• Trader — represents a trader
● The code for these classes is adapted from Java Messenger
9-13
SafeTrade OO Design (cont’d)
SafeTrade
«interface» Login
Trader
GUILogin
Exchange
Messenger Server
MsgUser MsgWindow
Java Messenger
SafeTrade
9-14
SafeTrade OO Design (cont’d)
● Part 2: Stocks and orders• Stock — represents a stock• TradeOrder — represents an order to buy or sell
stock• PriceComparator — implements a comparator for
orders, based on the bid or ask price
9-15
SafeTrade OO Design (cont’d)
TradeOrder
PriceComparator
TraderWindow
SafeTrade
«interface» Login
Trader
GUILogin
Exchange, Stock, and the other classes below them all connect to TradeOrder
Exchange Stock
9-16
SafeTrade Detailed Design
● Constructors, methods, fields for each class
● User interface: GUI, printouts, help, etc.
● GUI may be a good starting point
A trader’s window
getQuote method
placeOrder method
receiveMessage method
Quit method
9-17
SafeTrade Detailed Design (cont’d)
● TradeOrder objects carry information between traders, Exchange, stock objects
TradeOrder fields determine its accessor and modifier methods
9-18
SafeTrade Detailed Design (cont’d)
● A Stock object represents information about one stock
● Provides quotes for the stock
● Holds all pending orders for the stock
● Executes as many orders as possible when a new order arrives
9-19
SafeTrade Detailed Design (cont’d)public class Stock
Constructor:
public Stock(String symbol, String name, double price)
Initializes the stock’s symbol and company name.
Sets low, high and last sale prices to price. Sets
volume to 0. Creates empty priority queues for buy
and sell orders (see also PriceComparator).
Methods:
public String getQuote()
Returns a quote for this stock, including the company
name, symbol, last sale price, day’s low, high, and
volume, and, if available, highest bid and lowest ask
price and size.
public void placeOrder (TraderOrder order)
Sends an acknowledgement to the originating trader
listing the details of the order. Adds order to the
appropriate queue. Executes as many orders as
possible.
9-20
Review:
● Name a few data structures used in SafeTrade.
● What is a limit order and a market order?
● Why is HashMap a good choice to represent listed stocks?
● Name a few SafeTrade classes.
● If you had to split the SafeTrade development team into two groups, which classes would you assign to each group?
9-21
Review (cont’d):
● What Java Messenger class is converted into the Exchange class?
● Explain the role of the Login interface in the SafeTrade project.
● What data structures are used to hold buy and sell orders for a given stock? Why?
● What is the role of the PriceComparator class?
● What data structure would be appropriate for keeping track of stock holdings for each trader?