Object-Oriented Programming with Java, part I – Exercise 90

This exercise creates two classes, Team and Players, and then creates teams and adds players to them in ArrayLists.

My Code

Team

import java.util.ArrayList;

public class Team {

    private String name;
    private ArrayList<Player> members = new ArrayList<Player>();
    private int maxSize;

    public Team(String name) {
        this.name = name;
        this.maxSize = 16;

    }

    public String getName() {
        return this.name;
    }

    public void addPlayer(Player name) {
        if (members.size() < maxSize) {
            this.members.add(name);
        }
    }

    public void printPlayers() {
        for (Player member : members) {
            System.out.println(member);
        }
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public int size() {
        return members.size();
    }

    public int goals() {
        int totalGoals = 0;
        for (Player member : members) {
            totalGoals += member.goals();
        }
        return totalGoals;
    }

}

Player

public class Player {
    private String name;
    private int goals;
    
    public Player(String name, int goals){
        this.name = name;
        this.goals = goals;
    }
    
    public Player(String name){
        this.name = name;
        this.goals = 0;
    }
    
    
    public String getName(){
        return this.name;
    }
    
    public int goals(){
        return this.goals;
    }
    
    public String toString(){
        return this.name + " goals, " + this.goals;
    }
    
    
}

Model Code

Team

import java.util.ArrayList;

 

public class Team {

    private String name;

    private ArrayList<Player> players;

    private int maxSize;

 

    public Team(String name) {

        this.name = name;

        this.players = new ArrayList<Player>();

        this.maxSize = 16;

    }

 

    public String getName() {

        return this.name;

    }

 

    public void addPlayer(Player player) {

        if (this.size() >= this.maxSize) {

            return;

        }

 

        this.players.add(player);

    }

 

    public void printPlayers() {

        for (Player player : this.players) {

            System.out.println(player);

        }

    }

 

    public void setMaxSize(int maxSize) {

        this.maxSize = maxSize;

    }

 

    public int size() {

        return this.players.size();

    }

 

    public int goals() {

        int amount = 0;

        for (Player player : this.players) {

            amount += player.goals();

        }

 

        return amount;

    }

}

Player

public class Player {

    private String name;

    private int goals;

 

    public Player(String name) {

        this(name, 0);

    }

 

    public Player(String name, int goals) {

        this.name = name;

        this.goals = goals;

    }

 

    public int goals() {

        return this.goals;

    }

 

    public String getName() {

        return this.name;

    }

 

    @Override

    public String toString() {

        return this.name + ", maaleja " + this.goals;

    }

}

Comments

Well, I have gone through and mostly everything is the same. I have forgotten a few this. statements and my ArrayList initialises in the wrong place (it should initialise in the constructor) but it isn’t all that bad.

Leave a comment