Ejemplo n.º 1
0
        public double CalculateAverage(List <double> values, AveragingMethod averagingMethod)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            switch (averagingMethod)
            {
            case AveragingMethod.Mean:
                return(values.Sum() / values.Count);

            case AveragingMethod.Median:
                var sortedValues = values.OrderBy(x => x).ToList();

                int n = sortedValues.Count;

                if (n % 2 == 1)
                {
                    return(sortedValues[(n - 1) / 2]);
                }

                return((sortedValues[sortedValues.Count / 2 - 1] + sortedValues[n / 2]) / 2);

            default:
                throw new ArgumentException("Invalid averagingMethod value");
            }
        }
Ejemplo n.º 2
0
        public double CalculateAverageWithEnum(List <double> values, No3.AveragingMethod method)
        {
            CalculatorCreator calculatorCreator = new CalculatorCreator();

            if (values == null)
            {
                throw new ArgumentNullException($"{nameof(values)} can't be null");
            }
            ICalculator calculator = calculatorCreator.CalculatorCreate(method);

            return(calculator.Calculate(values));
        }