Exemple #1
0
        /// <summary>
        /// Returns a statistic about families.
        /// </summary>
        /// <param name="families">Families to analyze</param>
        /// <returns>
        /// Returns one statistic entry per family in <paramref name="families"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="families"/> is <c>null</c>.
        /// </exception>
        /// <remarks>
        /// <see cref="FamilySummary.AverageAge"/> is set to 0 if <see cref="IFamily.Persons"/>
        /// in <paramref name="families"/> is empty.
        /// </remarks>
        public static FamilySummary[] GetFamilyStatistic(IReadOnlyCollection <IFamily> families)
        {
            if (families == null)
            {
                throw new ArgumentNullException();
            }
            List <FamilySummary> summary = new List <FamilySummary>();

            foreach (var fam in families)
            {
                //new Family
                FamilySummary placeholder = new FamilySummary();
                placeholder.FamilyID = fam.ID;
                placeholder.NumberOfFamilyMembers = fam.Persons.Count;
                //default age = 0 --> cause of the case if 0 persons are in the family
                placeholder.AverageAge = 0;

                //if there are 0 persons are in the family the average age is 0
                if (fam.Persons.Count > 0)
                {
                    foreach (var person in fam.Persons)
                    {
                        placeholder.AverageAge += person.Age;
                    }
                    placeholder.AverageAge = placeholder.AverageAge / fam.Persons.Count;
                }
                //add the placeholder to the summary
                summary.Add(placeholder);
            }

            return(summary.ToArray());
        }
        /// <summary>
        /// Returns a statistic about families.
        /// </summary>
        /// <param name="families">Families to analyze</param>
        /// <returns>
        /// Returns one statistic entry per family in <paramref name="families"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="families"/> is <c>null</c>.
        /// </exception>
        /// <remarks>
        /// <see cref="FamilySummary.AverageAge"/> is set to 0 if <see cref="IFamily.Persons"/>
        /// in <paramref name="families"/> is empty.
        /// </remarks>
        public static FamilySummary[] GetFamilyStatistic(IReadOnlyCollection <IFamily> families)
        {
            List <FamilySummary> fs = new List <FamilySummary>();

            if (families == null)
            {
                throw new ArgumentNullException();
            }
            for (int i = 0; i < families.Count; i++)
            {
                IFamily family = families.ElementAt(i);
                double  old    = 0.0;
                double  cnt    = 0.0;
                double  temp   = 0.0;
                if (family.Persons.Count > 0)
                {
                    for (int j = 0; j < family.Persons.Count; j++)
                    {
                        IPerson person = family.Persons.ElementAt(j);
                        old += (int)person.Age;
                        cnt++;
                    }
                    temp = old / cnt;
                }
                FamilySummary help = new FamilySummary();
                help.FamilyID = family.ID;
                help.NumberOfFamilyMembers = (int)cnt;
                help.AverageAge            = (decimal)temp;
                fs.Add(help);
            }
            return(fs.ToArray());
        }
Exemple #3
0
        /// <summary>
        /// Returns a statistic about families.
        /// </summary>
        /// <param name="families">Families to analyze</param>
        /// <returns>
        /// Returns one statistic entry per family in <paramref name="families"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="families"/> is <c>null</c>.
        /// </exception>
        /// <remarks>
        /// <see cref="FamilySummary.AverageAge"/> is set to 0 if <see cref="IFamily.Persons"/>
        /// in <paramref name="families"/> is empty.
        /// </remarks>
        public static FamilySummary[] GetFamilyStatistic(IReadOnlyCollection <IFamily> families)
        {
            if (families == null)
            {
                throw new ArgumentNullException();
            }

            List <FamilySummary> summaries = new List <FamilySummary>();

            foreach (var current in families)
            {
                FamilySummary newSummary = new FamilySummary();
                newSummary.FamilyID = current.ID;
                newSummary.NumberOfFamilyMembers = current.Persons.Count;

                Decimal sum = 0;
                foreach (var person in current.Persons)
                {
                    sum = sum + person.Age;
                }
                // The following statement is a problem if number of persons is zero.
                sum = sum / current.Persons.Count;

                newSummary.AverageAge = sum;

                summaries.Add(newSummary);
            }

            return(summaries.ToArray());
        }
        /// <summary>
        /// Returns a statistic about families.
        /// </summary>
        /// <param name="families">Families to analyze</param>
        /// <returns>
        /// Returns one statistic entry per family in <paramref name="families"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="families"/> is <c>null</c>.
        /// </exception>
        /// <remarks>
        /// <see cref="FamilySummary.AverageAge"/> is set to 0 if <see cref="IFamily.Persons"/>
        /// in <paramref name="families"/> is empty.
        /// </remarks>
        public static FamilySummary[] GetFamilyStatistic(IReadOnlyCollection <IFamily> families)
        {
            int     familyId;
            int     numberOfFamilyMembers;
            decimal averageAge;

            int sum;

            if (families == null)
            {
                throw new ArgumentNullException();
            }

            List <FamilySummary> summaries = new List <FamilySummary>();

            foreach (IFamily family in families)
            {
                sum = 0;
                numberOfFamilyMembers = 0;
                averageAge            = 0;

                foreach (IPerson person in family.Persons)
                {
                    sum += (int)person.Age;
                }

                familyId = family.ID;
                numberOfFamilyMembers = family.Persons.Count;
                if (numberOfFamilyMembers != 0)
                {
                    averageAge = (decimal)sum / numberOfFamilyMembers;
                }

                FamilySummary summary = new FamilySummary();
                summary.FamilyID = familyId;
                summary.NumberOfFamilyMembers = numberOfFamilyMembers;
                summary.AverageAge            = averageAge;

                summaries.Add(summary);
            }

            return(summaries.ToArray());
        }
        /// <summary>
        /// Returns a statistic about families.
        /// </summary>
        /// <param name="families">Families to analyze</param>
        /// <returns>
        /// Returns one statistic entry per family in <paramref name="families"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="families"/> is <c>null</c>.
        /// </exception>
        /// <remarks>
        /// <see cref="FamilySummary.AverageAge"/> is set to 0 if <see cref="IFamily.Persons"/>
        /// in <paramref name="families"/> is empty.
        /// </remarks>
        public static FamilySummary[] GetFamilyStatistic(IReadOnlyCollection <IFamily> families)
        {
            if (families != null)
            {
                int             famId;
                int             numofPersons;
                decimal         ages = 0;
                decimal         avgAge;
                FamilySummary[] famSum = new FamilySummary[families.Count];

                if (families.ElementAt(0).Persons.Count == 0)
                {
                    FamilySummary[] empty = new FamilySummary[1];
                    empty[0] = new FamilySummary {
                        FamilyID = 1, NumberOfFamilyMembers = 0, AverageAge = 0
                    };
                    return(empty);
                }

                for (int i = 0; i < families.Count(); i++)
                {
                    ages         = 0;
                    famId        = families.ElementAt(i).ID;
                    numofPersons = families.ElementAt(i).Persons.Count;
                    foreach (var per in families.ElementAt(i).Persons)
                    {
                        ages += per.Age;
                    }
                    avgAge = ages / numofPersons;

                    famSum[i] = new FamilySummary {
                        FamilyID = famId, NumberOfFamilyMembers = numofPersons, AverageAge = avgAge
                    };
                }
                return(famSum);
            }
            else
            {
                throw new ArgumentNullException();
            }
        }