College of Computing and Digital Media: SE 350 - Object-Oriented Software Development
College of Computing and Digital Media: SE 350 - Object-Oriented Software Development
Price Class The Price class will be used to represent the various prices used throughout the application (Order prices, Quote prices, Trade prices, Ticker Prices, etc.). Prices are a fundamental component of a trading system so they will be the first thing we will implement. Price objects, like String objects, should be immutable (unchangeable once created). Methods like add, subtract, and multiply should never modify the state of a Price object, but instead return a new Price object whose state reflects the operation performed. The challenge in creating the Price class is that you are not allowed to use a float or a double to represent the Price objects dollar and cent value. Java float and double types are not recommended for representing price values, since they always carry small rounding differences. Appendix A of this document (Precision Issues Representing Currency Using float or double Types) explains why the use of float and double types is not recommended. Additionally, though using javas BigDecimal class to represent dollar and cent value is a potential technique, this approach comes with its own set of significant disadvantages. For the details of these significant disadvantages, please be sure to read the short article BigDecimal and your Money, at: http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/. The technique we will use in the Price class for representing price (currency) values is described below: Use a java long value to represent the value without a decimal place (you are basically storing price values in cents only). A price of $10.99 for example, should be represented as 1099. A price of $5,280.00 should be represented as 528000. Advantages: Integer (long, short, etc.) values are represented exactly, mathematical operations are easy to handle. Disadvantages: Must convert to decimal currency representation when printing or otherwise displaying these values (however most any technique would have this same disadvantage).
1 of 10
Christopher Hield
Christopher Hield
2 of 10
Christopher Hield
3 of 10
public static Price makeLimitPrice(String value) Creates a (limit) price object representing the value held in the provided String value. public static Price makeLimitPrice(long value) Creates a (limit) price object representing the value held in the provided long value. public static Price makeMarketPrice() Creates a (market) price object.
The String formatted values that must be accepted by the makeLimitPrice(String value) constructor are listed in detail in Appendix B: Required String formats for PriceFactorys String constructor.
PriceFactory and the Flyweight Design Pattern: Note that in a real-time trading environment, many price objects would be created: every order contains a Price object, every quote contains 2 Price objects, other components of this system still to come will also use Price objects. Real-world financial exchanges regularly receive over a billion quotes a day which would require 2 billion Price objects to represent. However, there are not actually 2 billion individual price values in use (i.e., every price between $0.01 and $20,000,000.00 would need to be used in trading if the 2 billion prices were all individual unique values). Only a small fraction of those possible prices are really used the 2 billion price objects would include many duplicate values (many users could enter a quote or order that uses the value $10.00 over the course of a trading day).
Christopher Hield
4 of 10
Christopher Hield
5 of 10
3) Verifying the Functionality of your Mathematical Operations: $10.50 + $1,400.99 = $1,411.49 : PASS $1,400.99 - $1,400.99 = $0.00 : PASS $-51.52 + $0.49 = $-51.03 : PASS $0.49 * 4 = $1.96 : PASS $-0.89 $12.00 = $-12.89 : PASS $12.00 + $90.00 = $102.00 : PASS PASS: Cannot add a LIMIT price to a MARKET Price: (MKT + $10.50) PASS: Cannot subtract a LIMIT price from a MARKET Price: (MKT - $10.50) PASS: Cannot multiply a MARKET price: (MKT * 10) 4) Verifying the Functionality of your Boolean Checks: Value | Negative Check | MKT Check ------------------------------------------$10.50 | PASS | PASS $1,400.99 | PASS | PASS $-51.52 | PASS | PASS $0.49 | PASS | PASS $-0.89 | PASS | PASS $12.00 | PASS | PASS $90.00 | PASS | PASS $14.50 | PASS | PASS MKT | PASS | PASS 5) Verifying the Functionality of your Boolean Comparisons: (Comparison to $14.50) Value | greaterOrEqual | greaterThan | lessOrEqual | lessThan ------------------------------------------------------------------------$10.50 | PASS | PASS | PASS | PASS $1,400.99 | PASS | PASS | PASS | PASS $-51.52 | PASS | PASS | PASS | PASS $0.49 | PASS | PASS | PASS | PASS $-0.89 | PASS | PASS | PASS | PASS $12.00 | PASS | PASS | PASS | PASS $90.00 | PASS | PASS | PASS | PASS $14.50 | PASS | PASS | PASS | PASS MKT | PASS | PASS | PASS | PASS
Christopher Hield
6 of 10
Phases & Schedule The Course Programming Project will be implemented in phases, each with a specific duration and due date as is listed below. Detailed documents on each phase will be provided at the beginning of the phase.
Phase 1 (1 Week) 9/19 9/26: Price & Price Factory Phase 4 (2 Weeks) 10/17 10/31: Product Service Book & Book Side Trade Processor
Phase 2 (1 Week) 9/26 10/3: Tradable & Tradable DTO Order Quote & Quote Side
Phase 3 (2 Weeks) 10/3 10/17: [Midterm 10/10] Current Market Publisher Last Sale Publisher Ticker Publisher Message Publisher Fill Message Cancel Message Market Message User (interface)
Christopher Hield
7 of 10
Now, suppose you wish to compute the total cost of 100 items having a unit cost of $9.48:
float unitCost = 9.48f; float totalCost = unitCost * 100.0f; System.out.println("Total Cost: $" + totalCost);
$947.99994
In this case, the effects of inexactness when using a floating-point data type to represent a monetary value are obvious. This example indicates that 9.48 * 100.0 is not 948.0 as you would expect, but rather 947.99994. If that computed value is then truncated to two decimal places, the result is $947.99, not $948.00. It would cost you a penny if you were preparing a customer invoice every time this happens. The problem only gets worse as the computations become more complex.
Christopher Hield
8 of 10
Christopher Hield
9 of 10
Christopher Hield
10 of 10