I have a Portfolio class that also has a linked list of investment class (example - Google is an example of investment), each investment has a trading history (another linked list) with data for each transaction.
When a user wants to make a deal (buy google shares for 5K), I need to find if the investment (in google) already exists in the InvestmentList. If this is not the case, add new investments (and add trading for your trading history), if so, just add another link to the google tradeHistory link list.
Problem - I need to find the Investmentment method to return the google link (investment instance) from the InvestmentList so that I can update my trading history. The method returns a listIterator, not a location reference in an InvestmentList (there must be an investment class). How to fix findInvestment? (found = iter is wrong)
public class Portfolio { private LinkedList<Investment> investmentsList; public Portfolio() { investmentsList = new LinkedList<Investment>(); } public void addInvestment(String symbol, double money){ Investment invest = findInvestment(symbol); if (invest == null) { System.out.println("symbol does not exist"); getInvestmentsList().add(new Investment(symbol,money)); System.out.println("New invetment has been added to your portfolio - " +symbol); } else { invest.addTrade(symbol,money); System.out.println("A new trade has been added to the current investment - " + symbol); } } public Investment findInvestment(String symbol){ Investment found = null; ListIterator<Investment> iter = investmentsList.listIterator(); while (iter.hasNext()) { if (iter.next().getSymbol().equals(symbol)) { found = iter; return found; System.out.println("Found the symbol"); } } return found; }
source share