Java - returns a link to a specific place in a linked list with an iterator list

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; } 
+6
source share
3 answers

You have already answered your question in your code!

  if (iter.next().getSymbol().equals(symbol)) { found = iter; 

Cm?

You call iter.next (), which gives you exactly what you are looking for! So, you just need to rework your code in a loop like:

 Investment currentInvestment = iter.next(); if (currentInvestment.get...) { found = currentInvestment; println... return found; } 

And for the future: read the javadoc classes you are dealing with. They usually tell you everything you need to know! No hint. 2: it makes no sense to have println after the return statement.

And I feel inclined to say: some experienced people are looking at your code. This is not buggy; but there are some things that can be improved; for example, using a double value for a currency (which is always a terrible idea); or the fact that your "model" of investment ... comes down to a line denoting its "symbol." This is terribly "low level."

+2
source

Just keep Investment - or in java 8 Optional<Investment>

Instead of a linked list:

 private Map<String, Investment> investmentsBySymbol; public Investment findInvestment(String symbol){ Investment found = investmentsList.get(symbol); return found; } 

BigDecimal is also a better choice than double, because

 new BigDecimal("3.10"); 

has an accuracy of 2, and doubling is always small.

+3
source

Use it like this:

  while (iter.hasNext()) { if ((found = iter.next()).getSymbol().equals(symbol)) { System.out.println("Found the symbol"); return found; } } 
+2
source

All Articles