Example #1
0
        public bool IsScheduleValid()
        {
            ErrorMessages = new List <string>();
            var gameMessages     = new List <string>();
            var dayMessages      = new List <string>();
            var scheduleMessages = new List <string>();
            var dayValidators    = new List <ScheduleDayValidator>();

            TeamGamesPlayed     = new Dictionary <string, int>();
            TeamDaysPlayed      = new Dictionary <string, int>();
            TeamHomeGamesPlayed = new Dictionary <string, int>();
            TeamAwayGamesPlayed = new Dictionary <string, int>();

            Schedule.Days.Keys.ToList().ForEach(dayNumber =>
            {
                var day       = Schedule.Days[dayNumber];
                var validator = new ScheduleDayValidator(Schedule.Days[dayNumber]);

                day.Games.ForEach(game =>
                {
                    var gameValidator = new ScheduleGameValidator(game);
                    if (!gameValidator.IsValid)
                    {
                        gameMessages.AddRange(gameValidator.Messages);
                    }
                });

                if (!validator.IsValid)
                {
                    dayMessages.AddRange(validator.Messages);
                }

                foreach (KeyValuePair <string, int> pair in validator.HomeTeamCounts)
                {
                    if (!TeamHomeGamesPlayed.ContainsKey(pair.Key))
                    {
                        AddStringKeyToDictionary(pair.Key, TeamHomeGamesPlayed, TeamAwayGamesPlayed, TeamDaysPlayed, TeamGamesPlayed);
                    }

                    TeamHomeGamesPlayed[pair.Key] += pair.Value;
                    TeamGamesPlayed[pair.Key]     += pair.Value;
                    TeamDaysPlayed[pair.Key]      += pair.Value;
                }

                foreach (KeyValuePair <string, int> pair in validator.AwayTeamCounts)
                {
                    if (!TeamAwayGamesPlayed.ContainsKey(pair.Key))
                    {
                        AddStringKeyToDictionary(pair.Key, TeamHomeGamesPlayed, TeamAwayGamesPlayed, TeamDaysPlayed, TeamGamesPlayed);
                    }

                    TeamAwayGamesPlayed[pair.Key] += pair.Value;
                    TeamGamesPlayed[pair.Key]     += pair.Value;
                    TeamDaysPlayed[pair.Key]      += pair.Value;
                }
            });

            if (!IsUnevenScheduleOkay)
            {
                int games = -1;

                foreach (KeyValuePair <string, int> data in TeamGamesPlayed)
                {
                    if (games == -1)
                    {
                        games = data.Value;
                    }

                    if (data.Value != games)
                    {
                        gameMessages.Add("Not all teams play the same number of games");
                    }
                }
            }

            if (!IsUnevenHomeAwayOkay)
            {
                int homeGames = -1;
                int awayGames = -1;

                foreach (KeyValuePair <string, int> data in TeamGamesPlayed)
                {
                    if (homeGames == -1)
                    {
                        homeGames = TeamHomeGamesPlayed[data.Key];
                    }
                    if (awayGames == -1)
                    {
                        awayGames = TeamAwayGamesPlayed[data.Key];
                    }

                    if (homeGames != awayGames)
                    {
                        gameMessages.Add("Team : " + data.Key + " has " + homeGames + " home games and " + awayGames + " away games");
                    }
                }
            }

            ErrorMessages.AddRange(gameMessages);
            ErrorMessages.AddRange(scheduleMessages);
            ErrorMessages.AddRange(dayMessages);

            return(ErrorMessages.Count == 0);
        }
Example #2
0
        //validates if there are any duplicate teams
        public bool IsDayValid(ScheduleDay day)
        {
            var validator = new ScheduleDayValidator(day);

            return(validator.IsValid);
        }