Doubt in PortfolioManagerFactory.java

// As a first step, Create appropriate instance of StoockQuoteService using

// StockQuoteServiceFactory and then use the same instance of StockQuoteService to create the

// instance of PortfolioManager.

I am unable to understand how to create instance of PortfolioMnager using StockQuoteService.
After reading some posts on forum, I understtod that we need two functions for this

  1. With parameters - provider and restTemplate
  2. With parameters - provider and StockQuoteService.

But unable to proceed after that. Please help !


Please see the above topic. I think your doubt is similar to the above topic. Please let me know if the above topic was helpful or not.

I tried to implement the function as suggested but not sure if I have implemented it in a right way or not. Could you please check my code and point out the errors, if any ?

Do the following steps I think it may resolve your issue.

  1. Create a StockQuoteServiceFactory object, create an instance of StockQuoteServiceFactory.
  1. Then create a StockQuotesService object and use getService method defined in it.
  1. Then create a portfolio manager object and pass StockQuotesService object as a parameter and return portfolio manager object.

In point 3, in which function the StockQuotesService object is supposed to be passed as a parameter?

Hi @Vaishnavi_Singh, You’ll first need to understand the overview of what you are asked to do. In this module, you are tasked with adding multiple stock quote service features. Now it makes sense to create different classes for different services but they all share some methods and attributes i.e they all implement StockQuoteService interface. As in the previous module for PortfolioManagerImpl here also it is advised to use the factory design pattern to create the required StockQuoteService object of a specific service. By implementing all of this you are refactoring the getStockQuotes() logic from the PortfolioManagerImpl class to their respective StockQuoteService classes. Now the ‘PortfolioManager’ class just needs the StockQuoteService object to get the stock data instead of the RestTemplate object. If you make the required changes in PortfolioManagerImpl and PortfolioManagerFactory to accommodate this new feature then you’ll be good to go.

Hey @Vaishnavi_Singh Thanks to @dotslash21 he has given a very good answer still if you have any doubt regarding that please let us know

Thank you @dotslash21 for giving a detailed explanation of what I am supposed to do.

@amanagar, I am still not sure if I am implementing this in a right way or not. Also, when I run ./gradlew test --tests PortfolioManagerFactory it shows - No tests found for given includes: [PortfolioManagerFactory](–tests filter).

Overall, 4 tests are failing
com.crio.warmup.stock.portfolio.PortfolioManagerFactoryTest > getPortfolioManagerWithStockQuoteService() FAILED
java.lang.NullPointerException at PortfolioManagerFactoryTest.java:27

com.crio.warmup.stock.portfolio.PortfolioManagerTest > calculateExtrapolatedAnnualizedReturn() FAILED
java.lang.ClassCastException at PortfolioManagerTest.java:106

com.crio.warmup.stock.quotes.AlphavantageServiceTest > getStockQuoteSingle() FAILED
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException at AlphavantageServiceTest.java:65

com.crio.warmup.stock.quotes.TiingoServiceTest > getStockQuoteSingle() FAILED
com.fasterxml.jackson.databind.exc.InvalidDefinitionException at TiingoServiceTest.java:65

These are some classic mistakes which you have done i am providing you some link just go through them you will find the probable reason

What is the api key in alphavantage ?

@Vaishnavi_Singh Please refer to this TODO


Call alphavantage service to fetch daily adjusted data for last 20 years. Refer to
// documentation here - https://www.alphavantage.co/documentation/


@amanagar
After going through the above links, I figured out the following possible reasons in my code which might be causing failure of the tests -

  1. Invalid Alphavantage URI
  2. JsonIgnoreProperties
  3. Not returning a sorted List sorted ascending based on Candle#getDate
  4. Not Using exactly the same format as RestTemplate#getForObject(URI, String )

I performed the following steps :

  1. Sorted the list in ascending order of Candle#getDate.
  2. Ensured that I am using RestTemplate#getForObject(URI, String )
  3. used the JsonIgnoreProperties(ignoreUnknown = true ) at class level

But the tests are still failing.

I am not sure about my API and PortfolioManagerFactory.java code, that whether these are correct or not. Please help !

If there is an issue with API you can use postman to see if API is working correctly or not.
If your issue is PortfolioManagerFactory.java code, then make sure that PortfolioManagerFactory is using the getService() method of the StockQuoteServiceFactory to know which service to implement and then returning the PortfolioManager object accordingly.

Yes, PortfolioManagerFactory is using the getService() method of the StockQuoteServiceFactory.

As suggested, I created StockQuotesService object and used getservice(). Then used this object to create portfoliomanager object.

For alphavantage API, Status code is 200 OK in postman.

I’m DMing you, we will continue this discussion on slack.

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.