1

How can I increment the value of the property TeamSize each time a new Athlete object is added? Because I cannot create a variable to store the value for TeamSize and increment that value each time a new Athlete object so I wonder if there is another way to increment TeamSize.

public class Team
{
    private Athlete[] athletes;

    public int MaxTeamSize
    {
        get;
        set;
    }

    public string TeamName
    {
        get;
        set;
    }

    public static int TeamSize
    {
        get;
        set;
    }

    public Sport TeamSport
    {
        get;
        set;
    }

    public Team(string name, Sport sport, int maxTeamSize)
    {
        TeamName = name;
        TeamSport = sport;
        MaxTeamSize = maxTeamSize;
        athletes = new Athlete[maxTeamSize];
    }

    public Athlete[] AddAthlete(Athlete a)
    {
        int teamSize = this.athletes.Length;

        if (teamSize < MaxTeamSize)
        {
            athletes[teamSize] = a;
            teamSize++;
            TeamSize = teamSize;
        }

        return this.athletes;
    }

}

  • What's wrong with the current approach? – Matt U Sep 19 '21 at 01:47
  • Why wouldn't it be a *method* which returns the collection count? – Ňɏssa Pøngjǣrdenlarp Sep 19 '21 at 01:49
  • You're violating SOLID princicles. `AddAthlete` is for adding, now it also for query the `Athlete[]`. `TeamSize` is depend on how many `Athlete`s are added. But, this property allows public set. – Le Vu Sep 19 '21 at 01:51
  • 2
    The problem with your code is that in your constructor, you initialize the `athletes` field as an array of length `maxTeamSize` so `athletes.Length` is always going to be `maxTeamSize`. `Length` property does not care about the number of non-null objects inside the array. It will always return the initial length. – Pharaz Fadaei Sep 19 '21 at 01:59
  • 1
    Use a List instead of an array to store the teams then just return the count. – Tarik Sep 19 '21 at 02:19

2 Answers2

2

You don't need to increment. Try this

 public int TeamSize
    {
        get { return athletes.Count(i => i!=null);}
      
    }
public int MaxTeamSize
    {
        get {retun athletes.Length;}
       
    }
 public Team(string name, Sport sport, int maxTeamSize)
    {
        TeamName = name;
        TeamSport = sport;
        athletes = new Athlete[maxTeamSize];
    }

public Athlete[] AddAthlete(Athlete a)
    {
       

        if (TeamSize < MaxTeamSize)
        {
            athletes[TeamSize] = a;
         }

        return this.athletes;
    }

You will need to add using System.Linq; if you don't have. But maybe instead of array of atlets you better use list of atlets. It allows dynamically add any quantity of atlets.

Serge
  • 40,935
  • 4
  • 18
  • 45
0

You have set the TeamSize property as static. It is a critical issue. It cause a side-effect for every instance of Team

    public class Team
    {
        private List<Athlete> athletes;

        public Team(string name, Sport sport, int maxTeamSize)
        {
            TeamName = name;
            TeamSport = sport;
            MaxTeamSize = maxTeamSize;
            athletes = new List<Athlete>(MaxTeamSize);
        }

        public int MaxTeamSize
        {
            get;
            private set;
        }

        public string TeamName
        {
            get;
            set;
        }

        public int TeamSize
        {
            get
            {
                return this.athletes.Count;
            }
        }

        public Sport TeamSport
        {
            get;
            private set;
        }

        public IReadOnlyCollection<Athlete> Athletes
        {
            get
            {
                return athletes;
            }
        }


        public void AddAthlete(Athlete a)
        {

            if (athletes.Count < MaxTeamSize)
            {
                athletes.Add(a);
            }
        }
    }
Le Vu
  • 407
  • 3
  • 12