Starting from:

$20

CS1331 Homework 14 - Stock Market Solved


Problem Background
Many times when programming, you will come across situations where you’re trying to deal with data that doesn’t come right away. Tasks like accessing a webpage over the internet or reading through a file system need some processing time in the background before they can be done. For tasks like these, we sometimes employ event-driven programming in order to make our programs fast and reactive when the data comes back.

In this homework, you will be creating an application that keeps track of changes in a stock market database and reports information out when relevant. In our sample stock market, data might be coming in and changing in real time. When values of stocks change, we want to build a system that can reevaluate the new values of these stocks and print out updated analysis.

Solution Description
For this assignment, create the following file: StockTracker.java.

You have also been provided with Stock.java, Market.java, and Tester.java. We have a few analysis questions that you must respond to as a comment!

Provided classes/interfaces

Stock.java:

An enum that holds instances of the Stocks that we might track. You should look at the JavaDoc comments for getCompanyName and getInitialValue inside this file.

Market.java:

An interface that specifies a contract for a stock markets with stocks that change values. You should look at the JavaDocs for registerHandler inside this file. You will NOT be implementing Market yourself but you will be using instances of classes that implement Market. These instances will be created for you and passed into the methods that you create inside of StockTracker.java.

Tester.java:

This class is provided for you so that you can test your code. It contains a main method which creates an instance of Market and calls the methods inside of StockTracker. It is setup to simulate asynchronously accessing information from the actual stock market. It may be useful to read through the main method but don’t worry about the implementation details.

StockTracker Class:

Instance Variables:

•      List<Stock trackedStocks - A List of the stocks that this tracker is tracking

•      int cutoff - The threshold for buying or selling a stock

•      Market market - The Market that this StockTracker is tracking

All fields should be private and non-static.

Constructor:

•      StockTracker(Market market, Stock[] stocks, int cutoff) - Takes in a an array of Stocks and adds them to trackedStocks. Afterwards, assign the instance variables market and cutoff to their respective fields. When this is done, register a buy, hold, and sell tracker on every stock passed in using the private helper methods in the class.

Constructor should be public.

Methods:

•      getter for trackedStocks. Returns a deep copy of trackedStocks

•      getters for market and cutoff

•      private void registerHoldTracker(Stock stock) - Should make sure that a message is printed any time a change in the stock’s value causes it to be valued both = the stock’s initalValue and <= the stock’s initialValue + cutoff. When this happens print out "{Company Name} is valued at ${value}. Hold on to what you have.".

–    You should achieve this by registering an event handler on the market for the given stock.

–    This method must use only an instance of a named inner class when creating the handler

•      private void registerBuyTracker(Stock stock) - Should make sure that a message is printed any time a change in the stock’s value causes it to be valued < the stock’s initialValue - cutoff. When this happens print out "{Company Name} just dropped to ${value}. Buy now!".

–    You should achieve this by registering an event handler on the market for the given stock.

–    This method must use only an anonymous inner class when creating the handler

•      private void registerSellTracker(Stock stock) - Should make sure that a message is printed any time a change in the stock’s value causes it to be valued the stock’s initialValue + cutoff.

When this happens print out "{Company Name} just rose to ${value}. Sell now!".

–    You should achieve this by registering an event handler on the market for the given stock.

–    This method must use only a lambda expression when creating the handler

Getters should follow best practices taught in class

Analysis Questions
Answer these at the top of StockTracker.java in a comment. Put your collab statement first and then a multiline comment with your answers.

1.    What are the pros and cons of using named inner classes, anonymous inner classes, and lambda expressions? (4-7 bullet points. < 200 words.)

2.    Why syntactically can we use a lambda expression when we are registering an event handler for the market? (1-3 sentences. < 150 words.)

3.    Why would we decide to use event driven programming to access the stock market? (2-5 sentences. < 220 words)

Testing your code:

If you want to test out your own code before submitting we recommend creating a separate java file and implementing a main method there.

We have also provided sample tests in Tester.java These tests are not comprehensive!

This should be the output of Tester.java:

Apple is valued at $402. Hold on to what you have.

Pfizer is valued at $609. Hold on to what you have.

Cisco is valued at $411. Hold on to what you have.

Apple is valued at $403. Hold on to what you have.

AT&T just dropped to $117. Buy now!

Pfizer just rose to $614. Sell now!

Facebook is valued at $709. Hold on to what you have.

Intel is valued at $258. Hold on to what you have.

Pfizer just rose to $622. Sell now!

Facebook just rose to $715. Sell now!

Apple is valued at $407. Hold on to what you have. General Electric just dropped to $488. Buy now!

Cisco is valued at $418. Hold on to what you have.

AT&T just dropped to $121. Buy now!

Facebook just rose to $719. Sell now!

Apple just rose to $412. Sell now!

Microsoft is valued at $354. Hold on to what you have.

Intel is valued at $254. Hold on to what you have.

Cisco is valued at $420. Hold on to what you have.

Facebook just rose to $720. Sell now!

Apple just rose to $411. Sell now!

General Electric just dropped to $482. Buy now!

Intel is valued at $258. Hold on to what you have.

Intel is valued at $254. Hold on to what you have.

Ford is valued at $303. Hold on to what you have.

Apple just rose to $420. Sell now!

Cisco is valued at $411. Hold on to what you have.

AT&T just dropped to $114. Buy now!

Ford is valued at $305. Hold on to what you have.

Facebook just rose to $728. Sell now!

Bank of America is valued at $300. Hold on to what you have.

General Electric just dropped to $477. Buy now!

Intel is valued at $255. Hold on to what you have.

Pfizer just rose to $630. Sell now!

Facebook just rose to $724. Sell now!

Note that Tester will not finishing running immediately because it is (fake) accessing data from the Stock Market.

Feel free to modify or create your own tests in the main method!

Allowed Imports
• You are allowed to import the following classes:

–    java.util.Arrays

–    java.util.ArrayList

–    java.util.List

–    java.util.function.Consumer

More products