A command-line stock exchange simulation built for Object-Oriented Software Development. It models orders, quotes, product books, users, and live “current market” updates.
- Encapsulation: Domain objects keep state private; updates go through validated methods. Shared read-only views use DTOs to avoid leaking internals.
- Immutability:
Priceis an immutable value type (stored as centsintto avoid floating-point drift) and implementsComparable. - Interfaces & Polymorphism:
Tradableabstracts both orders and quote sides, letting the book match them uniformly.
- Factory:
PriceFactorynormalizes creation (parsing"$1,234.56"→ cents) and centralizes validation. - Flyweight:
PriceFactoryreturns one shared instance per price value, reducing memory churn across the book. - Singleton:
UserManagerandProductManagerexpose one source of truth for users and product books. - Façade: Those managers provide a simple API while hiding coordination logic (routing tradables to the right book, user updates, etc.).
- Observer (Publish/Subscribe):
CurrentMarketPublishernotifies subscribed users when top-of-book changes;UserimplementsCurrentMarketObserver.
-
Order book:
TreeMap<Price, List<Tradable>>for each side- BUY: descending by price; SELL: ascending.
- Efficient top-of-book lookups and price-level aggregation.
-
Lookups:
HashMapfor users, products, and user-specific quote/order tracking. -
Matching logic:
ProductBook.tryTrade()computes tradable volume across sides.ProductBookSide.tradeOut(price, vol)proportionally fills across price-level entries, updates filled/remaining/cancelled, and emits fills.
- Purpose-built exceptions (e.g., InvalidPriceException, data-validation errors).
- Strict input rules for user IDs, symbols, volumes, and price formats.
- A provided
Mainscript exercises: adding orders/quotes, cancelling, trading, and current-market publications. Output is deterministic for quick verification.
Prereqs
- JDK 17+ installed (
java -versionto confirm)
Clone
git clone <your-repo-url>
cd <repository-folder-name>Compile
(Adjust the main class path if your Main has a package.)
javac -d out --source-path src src/Main.javaRun
java -cp out MainYou’ll see console output showing adds/cancels, fills, and current-market snapshots (e.g., WMT $70.70x35 - $70.75x40 [$0.05]).