$30
Q1: You have to use the Observer Design Pattern to build a stock trading platform where each user can subscribe to the different kinds of stocks in the market.
Consider an input file containing the stock names followed by their counts and price (Table 1).
P1 3 40.00
P2 4 30.00
P3 5 80.00
P4 6 25.00
P5 7 15.00
P6 9 50.00
Table 1: Contents of the input file
After a user logs into the system, he will be shown the number of stocks and their respective prices. A user can subscribe to a particular stock by using the S command (e.g., S P3). Analogously, he can also unsubscribe to a subscribed stock using the U command.
Use the following trigger commands (fed into the system) to change the state of the stocks:
1. I: Increase a stock price. Example: I P1 10.00
2. D: Decrease a stock price. Example: D P4 5.00
3. C: Change in stock count (always positive). Example: C P2 2
Subscribers will be notified if the state of their subscribed stocks has changed.
Q2: The city of Johannesburg has the following four major public service organizations.
• Johannesburg Water Supply Authority (JWSA) provides WATER related services.
• Johannesburg Power Development Board (JPDC) provides ENERGY related services.
• Johannesburg Road and Transport Authority (JRTA) provides TRANSPORT services.
• Johannesburg Telecommunication Regulatory Commission (JTRC) provides TELECOM services.
1
The organizations depend on each other for cross services (e.g. JRTA depends on JPDC for ENERGY related services). Due to the recent allegations of poor service and lack of cooperation among the organizations, Johannesburg City Corporation (JCC) has decided to act as the mediator among these four entities. Any cross service will have to be performed through JCC.
Each organization can either provide service or request for service that others provide. The requests for services are sent to JCC who puts the requests on the specific organization’s service queue. The organizations have no idea whom they are serving. Implement the above scenario using Mediator design pattern.
Sample Input
Expected Outcome
Init
All four services are initiated through mediator
JWSA POWER
JWSA requests for POWER service
JRTA POWER
JRTA requests for POWER service
JPDC TELECOM
JPDC requests for TELECOM service
JPDC SERVE
JPDC serves the request of JWSA
JPDC SERVE
JPDC serves the request of JRTA
Table 2: Sample Input and Expected Outcome
You can use built-in Queue data structure of Java for this problem.
2