3

Using a for each loop. how can I count the number of goals each player has and return that in the method goals() which is in the Team class? I know my current return statement is wrong I was unsure what to put there:

import java.util.ArrayList;

public class Team {

    private String teamName;
    private ArrayList<Player> list;
    private int maxSize = 16;

    public Team(String teamName) {
        this.teamName = teamName;
        this.list = new ArrayList<Player>();
    }

    public String getName() {

        return this.teamName;
    }

    public void addPlayer(Player player) {

        if (list.size() < 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;
    }
}

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;
    }
}

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());
    }
}

3 Answers 3

3

I think you're looking for something like

public int goals(){
    int total = 0;
    for(Player p : list){ // for each Player p in list         
       total += p.goals();
    }       
    return total;
}

Add the number of each Player's goals to the total and then return the total.

Sign up to request clarification or add additional context in comments.

2 Comments

Great! That works. Would it have been easier to create the ArrayList in the main initially do you think?
@Noah Kettler, you can! In Java 8 and later, you can use aggregate function by calling your list.stream().mapToInt(Player::goals).count(). See the Stream interface introduced in Java 8. :-)
3
return list.stream().mapToInt(Player::goals).sum();

1 Comment

Note that this will only work in Java 8 and later versions.
2

Just accumulate them in a temporary variable:

public int goals() {
    int goals = 0;
    for(Player p : list){
        goals += p.goals();
    }       
    return goals;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.