title: How to return the sum in an object ArrayList
body: <p>Using a for each loop. how can I count the number of goals each player has and return that in the method <code>goals()</code> which is in the <code>Team</code> class?  I know my current return statement is wrong I was unsure what to put there:</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class Team {
  
      private String teamName;
      private ArrayList&lt;Player&gt; list;
      private int maxSize = 16;
  
      public Team(String teamName) {
          this.teamName = teamName;
          this.list = new ArrayList&lt;Player&gt;();
      }
  
      public String getName() {
  
          return this.teamName;
      }
  
      public void addPlayer(Player player) {
  
          if (list.size() &lt; this.maxSize) {
              this.list.add(player);
          }
  
      }
  
      public void printPlayers() {
          System.out.println(list);
      }
  
      public void setMaxSize(int maxSize) {
  
          this.maxSize = maxSize;
      }
  
      public int size() {
  
          return list.size();
      }
  
      public int goals(){
  
          for(Player goals : list){
  
          }
          return list;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class Player {
  
      private String playerName;
      private int goals;
  
      public Player(String playerName) {
  
          this.playerName = playerName;
      }
  
      public Player(String playerName, int goals) {
  
          this.playerName = playerName;
          this.goals = goals;
      }
  
      public String getName() {
  
          return this.playerName;
      }
  
      public int goals() {
  
          return this.goals;
      }
  
      public String toString() {
  
          return "Player: " + this.playerName + "," + goals;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class Main {
      public static void main(String[] args) {
          // test your code here
          Team barcelona = new Team("FC Barcelona");
  
          Player brian = new Player("Brian");
          Player pekka = new Player("Pekka", 39);
          barcelona.addPlayer(brian);
          barcelona.addPlayer(pekka);
          barcelona.addPlayer(new Player("Mikael", 1)); // works similarly as the above
  
          System.out.println("Total goals: " + barcelona.goals());
      }
  }
  </code></pre>