21
Java Java Methods Methods AB AB 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)

Ch09.ppt

  • Upload
    zorro29

  • View
    328

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Ch09.ppt

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)

Page 2: Ch09.ppt

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

Page 3: Ch09.ppt

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)

Page 4: Ch09.ppt

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

Page 5: Ch09.ppt

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)

Page 6: Ch09.ppt

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

Page 7: Ch09.ppt

9-7

SafeTrade Application (cont’d)

Page 8: Ch09.ppt

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

Page 9: Ch09.ppt

9-9

SafeTrade Design

Structural Design

OO Design

Detailed Design

Data structures used

Classes and objects

Fields, constructors, and methods

Page 10: Ch09.ppt

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)

Page 11: Ch09.ppt

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

Page 12: Ch09.ppt

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

Page 13: Ch09.ppt

9-13

SafeTrade OO Design (cont’d)

SafeTrade

«interface» Login

Trader

GUILogin

Exchange

Messenger Server

MsgUser MsgWindow

Java Messenger

SafeTrade

Page 14: Ch09.ppt

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

Page 15: Ch09.ppt

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

Page 16: Ch09.ppt

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

Page 17: Ch09.ppt

9-17

SafeTrade Detailed Design (cont’d)

● TradeOrder objects carry information between traders, Exchange, stock objects

TradeOrder fields determine its accessor and modifier methods

Page 18: Ch09.ppt

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

Page 19: Ch09.ppt

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.

Page 20: Ch09.ppt

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?

Page 21: Ch09.ppt

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?