コード例 #1
0
        public void CalculateAverage(Squad squad)
        {
            if (squad.Players.Count() == 0)
            {
                return;
            }

            if (squad.Players.Count() < 0)
            {
                throw new System.ArgumentException("Number of players in squad is :" + squad.Players.Count());
            }

            int shootingAverage = squad.Players.Sum(p => p.Shooting) / squad.Players.Count();
            int skatingAverage  = squad.Players.Sum(p => p.Skating) / squad.Players.Count();
            int checkingAverage = squad.Players.Sum(p => p.Checking) / squad.Players.Count();

            SquadPlayer player = new SquadPlayer()
            {
                Name     = "Average Skills:",
                Id       = Guid.NewGuid().ToString("N"),
                Skating  = skatingAverage,
                Shooting = shootingAverage,
                Checking = checkingAverage
            };

            squad.Players.Add(player);
        }
コード例 #2
0
        public IEnumerable <SquadPlayer> ProvidePlayers()
        {
            string  filePath = "players.json";
            JObject my_obj   = JsonConvert.DeserializeObject <JObject>(File.ReadAllText(filePath));

            foreach (KeyValuePair <string, JToken> sub_obj in my_obj)
            {
                foreach (JObject player in sub_obj.Value)
                {
                    string id     = (string)player["_id"];
                    string name   = (string)player["firstName"] + " " + (string)player["lastName"];
                    int[]  skills = new int[3]; //shooting, skating, checking
                    int    index  = 0;
                    foreach (JObject skill in player["skills"])
                    {
                        skills[index++] = (int)skill["rating"];
                    }

                    SquadPlayer squadPlayer = new SquadPlayer()
                    {
                        Id = id, Name = name, Shooting = skills[0], Skating = skills[1], Checking = skills[2]
                    };

                    yield return(squadPlayer);
                }
            }
        }