private void CalculatorChanged(CalculatorType calculatorType)
        {
            var newCalculator = (ICalculatorViewModel)_containerHelper.Create(calculatorType.TypeName);

            SelectedCalculatorViewModel = newCalculator;
            FlyoutOpen = false;
        }
        static void Main(string[] args)
        {
            var logger      = new Logger();
            var calculators = new Dictionary <CalculatorType, ICalculator>
            {
                { CalculatorType.Number, new NumberCalculator(logger) },
                { CalculatorType.Date, new DateCalculator(logger) }
            };

            PrintWelcomeMessage();

            while (true)
            {
                try
                {
                    CalculatorType calculationMode = AskForCalculationMode();
                    calculators[calculationMode].PerformOneCalculation();
                }
                catch (InvalidOperatorException e)
                {
                    Console.WriteLine("Sorry, there was a problem: " + e.Message);
                    Console.WriteLine();
                }
            }
        }
        public void Calculation_InvalidData_Error(CalculatorType type, decimal p1, decimal p2)
        {
            CalculatorService service = new CalculatorService(new MockCalculatorFactory(), new MockCalculationLogger());
            var result = service.PerformCalculation(type, p1, p2);

            Assert.False(result.Success);
        }
        private void SelectCalculator(CalculatorType calculatorType)
        {
            _driver.FindElementByAccessibilityId("TogglePaneButton").Click();
            _driver.FindElementByAccessibilityId(calculatorType.ToString()).Click();

            //var el = _waiter.Until(ExpectedConditions.ElementExists(By.Name("One")));
            //Assert.IsNotNull(el);
        }
        public void Calculations_ValidData_CorrectValues(CalculatorType type, decimal p1, decimal p2, decimal output)
        {
            CalculatorService service = new CalculatorService(new MockCalculatorFactory(), new MockCalculationLogger());
            var result = service.PerformCalculation(type, p1, p2);

            Assert.True(result.Success);
            Assert.Equal(output, result.Output);
        }
Example #6
0
        public void PowSqureSum_GiveNotImplementedParams_ThrowsNIEx(int a, int b, CalculatorType calculatorType, int expectedRes)
        {
            // Arrange
            _calculatorMock.PowSqureSum(a, b).Returns(Math.Pow(a + b, 2));
            _calculatorMock.Type = calculatorType;

            // Act and Assert
            Assert.Throws <NotImplementedException>(() => _extraCalculator.PowSqureSum(a, b));
        }
Example #7
0
 /// <summary>
 /// Returns the type of calculator requested
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public ICalculator GetCalculator(CalculatorType type)
 {
     ICalculator calculator = null;
     switch (type){
         case CalculatorType.REGULAR:
             calculator = new RegularCalculator();
             break;
     }
     return calculator;
 }
Example #8
0
        public void PowSqureSum_GiveCorrectValues_ReturnsCorrectResult(int a, int b, CalculatorType calculatorType, int expectedRes)
        {
            // Arrange
            _calculatorMock.PowSqureSum(a, b).Returns(Math.Pow(a + b, 2));
            _calculatorMock.Type.Returns(CalculatorType.Decimal); //_calculatorMock.Type = calculatorType;

            // Act
            var res = _extraCalculator.PowSqureSum(a, b);

            // Assert
            Assert.AreEqual(expectedRes, res, 0);
        }
Example #9
0
        public ICalculator SelectCalculator(CalculatorType calculatorType)
        {
            switch (calculatorType)
            {
            case CalculatorType.LoanRepaymentTimeCalculator:
                return(new LoanRepaymentTimeCalculator());

            case CalculatorType.Unknown:
            case CalculatorType.None:
            default:
                return(null);
            }
        }
Example #10
0
        public ICalculator Get(CalculatorType calculatorType)
        {
            switch (calculatorType)
            {
            case CalculatorType.Standard: return(new Calculator());

            case CalculatorType.Slow: return(new SlowCalculator());
            }

            string message = String.Format("No implementation matching for type {0}. Please add it", calculatorType);

            throw new ArgumentException(message);
        }
        public ICalculator GetCalculator(CalculatorType calculatorType)
        {
            switch (calculatorType)
            {
            case CalculatorType.CombinedWith:
                return((ICalculator)_serviceProvider.GetService(typeof(Calculators.CombinedWith)));

            case CalculatorType.Either:
                return((ICalculator)_serviceProvider.GetService(typeof(Calculators.Either)));

            default:
                throw new InvalidOperationException($"Unknown calculator type: {calculatorType}");
            }
        }
        public IAverageCalculator CreateCalculator(CalculatorType type)
        {
            //NB: Yes, I have an If statement here, but it is never to be repeated!
            if (type == CalculatorType.Mean)
                return new Mean();

            if (type == CalculatorType.Median)
                return new Median();

            if (type == CalculatorType.Mode)
                return new Mode();

            throw new UnknownCalculatorTypeException(type);
        }
        public ICalculator GetCalculator(CalculatorType calculatorType)
        {
            switch (calculatorType)
            {
            case CalculatorType.CombinedWith:
                return(new Domain.Calculation.Calculators.CombinedWith());

            case CalculatorType.Either:
                return(new Domain.Calculation.Calculators.Either());

            default:
                throw new InvalidOperationException($"Unknown calculator type: {calculatorType}");
            }
        }
        public static IUsageCalculator getCalculator(CalculatorType type, SOMTeamReportFile teamReportFile, Team targetTeam)
        {
            if (type == CalculatorType.SP_BASIC)
            {
                return(new SPOnly_Straight_Calculator(teamReportFile, targetTeam));
            }
            else if (type == CalculatorType.SP_SCHEDULE)
            {
                return(new SPOnly_ScheduleWeighted_Calculator(teamReportFile, targetTeam));
            }
            else if (type == CalculatorType.ALL_PITCHERS_AND_SCHEDULE)
            {
                return(new AllPitchers_ScheduleWeighted_Calculator(teamReportFile, targetTeam));
            }

            throw new Exception("Unimplemented Calculator Type Requested");
        }
Example #15
0
        public static IPenaltyCalculator Generate(CalculatorType type)
        {
            switch (type)
            {
            case CalculatorType.Neighbor:
                return(new NeighborPenaltyCalculator());

            case CalculatorType.NeighborAndMerge:
                return(new NeighborAndMergeCalculator());

            case CalculatorType.NeighborMergeAndCorner:
                return(new NeighborMergeAndCornerCalculator());

            default:
                throw new InvalidOperationException("What Penalty Calculator type?");
            }
        }
        protected void SelectConverterCalculator(CalculatorType calculatorType, string fromUnit, string toUnit, int value)
        {
            SelectCalculator(calculatorType);
            ClearEntryInput();

            _driver.FindElementByAccessibilityId("Units1").Click();
            var wait          = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
            var celsiusButton = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Name(fromUnit)));

            celsiusButton.Click();

            _driver.FindElementByAccessibilityId("Units2").Click();
            var fahrenheitButton = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Name(toUnit)));

            fahrenheitButton.Click();

            PickNumericValue(value.ToString());
        }
Example #17
0
        public static AbstractCalculatorEngine GetCalculator(CalculatorType type)
        {
            switch (type)
            {
            case CalculatorType.SYNC:
                return(new SyncronousDistanceCalculator());

            case CalculatorType.ASYNC:
                return(new AsyncronousDistanceCalculator());

            case CalculatorType.MPI:
                return(new MPIDistanceCalculator());

            case CalculatorType.CUDA:
                return(new MPIDistanceCalculator());
            }

            return(null);
        }
        /// <summary>
        /// Initializes and returns a calculator object
        /// </summary>
        /// <param name="type">Calculator type, sync, parallel, MPI or CUDA computation types</param>
        /// <returns></returns>
        public static IDistanceCalculator GetCalculator(CalculatorType type)
        {
            switch (type)
            {
            case CalculatorType.SYNC:
                return(new SyncDistanceCalculator());

            case CalculatorType.PARALLEL:
                return(new ParallelDistanceCalculator());

            case CalculatorType.MPI:
                return(new MPIDistanceCalculator());

            case CalculatorType.CUDA:
                return(new NvidaCudaDistanceCalculator());
            }

            return(null);
        }
        public IAverageCalculator CreateCalculator(CalculatorType type)
        {
            //NB: Yes, I have an If statement here, but it is never to be repeated!
            if (type == CalculatorType.Mean)
            {
                return(new Mean());
            }

            if (type == CalculatorType.Median)
            {
                return(new Median());
            }

            if (type == CalculatorType.Mode)
            {
                return(new Mode());
            }

            throw new UnknownCalculatorTypeException(type);
        }
Example #20
0
        public int CalculateAverage_TheGoodWay(int[] listOfValues, CalculatorType type)
        {
//          Responsibility of deciding which implementation to use is encapsulated within the Factory class
            var creatorOfAverageCalulators = new AverageCalculatorFactory();

            var averageCalculator = creatorOfAverageCalulators.CreateCalculator(type);

            //responsibility for performing the calculation is pushed down to where it belongs; with each of the concrete implementations of IAverageCalculator

            return(averageCalculator.Average(listOfValues));

            /*
             * Here we have separated Responsibilities of deciding which implementation to use, and the actual implementations of those calculations.
             * Now, if we want to change how we calculate Mode (the implementation is a bit poor and could use a refactor!):
             * - we have no fear of breaking the other calculations
             * - we have no fear of breaking the logic that determines which algorithm to use
             * - Each implementation is independently testable, as is the factory.
             * Also
             * - Its easy to add or remove implementations without the breaking the others
             */
        }
Example #21
0
        public ICalculationResult PerformCalculation(CalculatorType type, decimal probability1, decimal probability2)
        {
            IInputData         data = new InputData(probability1, probability2);
            ICalculationResult calculationResult;

            var validationResult = data.Validate();

            if (validationResult.Success == false)
            {
                CalculationResult concreteResult = new CalculationResult(data);
                concreteResult.AppendResult(validationResult);

                calculationResult = concreteResult;
            }
            else
            {
                var calculator = _calculatorFactory.GetCalculator(type);
                calculationResult = calculator.Calculate(data);
            }

            _logger.LogCalculation(calculationResult);

            return(calculationResult);
        }
 public UnknownCalculatorTypeException(CalculatorType type)
 {
     //bla bla bla
 }
Example #23
0
 public IncomeTaxCalculator(decimal taxPercentage)
 {
     this.taxPercentage  = taxPercentage;
     this.calculatorType = CalculatorType.Income;
 }
 public CalculatorTest6(CalculatorType calculatorType)
 {
     _calculatorType    = calculatorType;
     _calculatorFactory = new CalculatorFactory();
 }
Example #25
0
        public int CalculateAverage_TheGoodWay(int[] listOfValues, CalculatorType type)
        {
            //          Responsibility of deciding which implementation to use is encapsulated within the Factory class
            var creatorOfAverageCalulators = new AverageCalculatorFactory();

            var averageCalculator = creatorOfAverageCalulators.CreateCalculator(type);

            //responsibility for performing the calculation is pushed down to where it belongs; with each of the concrete implementations of IAverageCalculator

            return averageCalculator.Average(listOfValues);

            /*
             Here we have separated Responsibilities of deciding which implementation to use, and the actual implementations of those calculations.
             Now, if we want to change how we calculate Mode (the implementation is a bit poor and could use a refactor!):
             - we have no fear of breaking the other calculations
             - we have no fear of breaking the logic that determines which algorithm to use
             - Each implementation is independently testable, as is the factory.
             Also
             - Its easy to add or remove implementations without the breaking the others
             */
        }
Example #26
0
        public int CalculateAverage_TheBadWay(int[] listOfValues, CalculatorType type)
        {
            var average = 0;

            if (type == CalculatorType.Mean)
            {
                #region Implementation of Mean
                var total = 0;

                foreach (var item in listOfValues)
                {
                    total = total + item;
                }

                average = total / listOfValues.Count();
                #endregion
            }

            if (type == CalculatorType.Median)
            {
                #region Implemenation for Median
                int? greatestFrequency = null;
                var median = 0;

                foreach (var distinctItem in listOfValues.Distinct())
                {
                    var countOfItem = listOfValues.Count(x => x == distinctItem);

                    if (!greatestFrequency.HasValue)
                    {
                        greatestFrequency = countOfItem;
                        median = distinctItem;
                    }
                    else if (countOfItem > greatestFrequency.Value)
                    {
                        greatestFrequency = countOfItem;
                        median = distinctItem;
                    }
                }

                average = median;
                #endregion
            }

            if (type == CalculatorType.Mode)
            {
                #region implementation of mode
                int? greatestFrequency = null;
                var median = 0;

                foreach (var distinctItem in listOfValues.Distinct())
                {
                    var countOfItem = listOfValues.Count(x => x == distinctItem);

                    if (!greatestFrequency.HasValue)
                    {
                        greatestFrequency = countOfItem;
                        median = distinctItem;
                    }
                    else if (countOfItem > greatestFrequency.Value)
                    {
                        greatestFrequency = countOfItem;
                        median = distinctItem;
                    }
                }

                average = median;
                #endregion
            }

            /*
             The code above does not do one thing, it actually has 4 responsibilities:
            - calculates the average for mean, mode & median (that's 3 responsibilities!)
            - and determines which calculation to use

             This means that the decision of which calculation to use and the calculation themselves are tightly coupled:
             I risk breaking all 4 responsibilities whenever I change any code in this method!!!

            Imagine how many unit tests I will need to write for this one method in order to achieve 100% coverage....

             Unfortunately this is a common occurrence & core services is littered with similar examples of

             * If (ProductCode == "PC") //do x
             * If (ProductCode == "LC") //do Y
             * If (ProductCode == "BI") //do Z

             The problem gets worse when you start to add more and more variations:

             * If (ProductCode == "PC") //do x
             * If (ProductCode == "LC") //do Y
             * If (ProductCode == "BI") //do Z
             * If (ProductCode == "HO") //do A
             * If (ProductCode == "LI") //do B
             * If (ProductCode == "PT") //do C

             But it grows exponentially when the number of places in the code that need to make the decision increases:
              for example the way you
             * (1) Save
             * (2) Load
             * (3) Edit
             * (4) Delete

               a risk, depending on whether it is a Car, Van, Bike, etc

             If Statements and Switch Statements are evil - try to avoid them :)
             */

            return average;
        }
 protected void SelectCalculator(CalculatorType calculatorType)
 {
     _driver.FindElementByAccessibilityId("TogglePaneButton").Click();
     _driver.FindElementByAccessibilityId(calculatorType.ToString()).Click();
 }
Example #28
0
 public SocialContributionsCalculator(decimal taxPercentage)
 {
     this.taxPercentage  = taxPercentage;
     this.calculatorType = CalculatorType.SocialContributions;
 }
 public UnknownCalculatorTypeException(CalculatorType type)
 {
     //bla bla bla
 }
Example #30
0
        public int CalculateAverage_TheBadWay(int[] listOfValues, CalculatorType type)
        {
            var average = 0;

            if (type == CalculatorType.Mean)
            {
                #region Implementation of Mean
                var total = 0;

                foreach (var item in listOfValues)
                {
                    total = total + item;
                }

                average = total / listOfValues.Count();
                #endregion
            }

            if (type == CalculatorType.Median)
            {
                #region Implemenation for Median
                int?greatestFrequency = null;
                var median            = 0;

                foreach (var distinctItem in listOfValues.Distinct())
                {
                    var countOfItem = listOfValues.Count(x => x == distinctItem);

                    if (!greatestFrequency.HasValue)
                    {
                        greatestFrequency = countOfItem;
                        median            = distinctItem;
                    }
                    else if (countOfItem > greatestFrequency.Value)
                    {
                        greatestFrequency = countOfItem;
                        median            = distinctItem;
                    }
                }

                average = median;
                #endregion
            }

            if (type == CalculatorType.Mode)
            {
                #region implementation of mode
                int?greatestFrequency = null;
                var median            = 0;

                foreach (var distinctItem in listOfValues.Distinct())
                {
                    var countOfItem = listOfValues.Count(x => x == distinctItem);

                    if (!greatestFrequency.HasValue)
                    {
                        greatestFrequency = countOfItem;
                        median            = distinctItem;
                    }
                    else if (countOfItem > greatestFrequency.Value)
                    {
                        greatestFrequency = countOfItem;
                        median            = distinctItem;
                    }
                }

                average = median;
                #endregion
            }

            /*
             * The code above does not do one thing, it actually has 4 responsibilities:
             * - calculates the average for mean, mode & median (that's 3 responsibilities!)
             * - and determines which calculation to use
             *
             * This means that the decision of which calculation to use and the calculation themselves are tightly coupled:
             * I risk breaking all 4 responsibilities whenever I change any code in this method!!!
             *
             * Imagine how many unit tests I will need to write for this one method in order to achieve 100% coverage....
             *
             * Unfortunately this is a common occurrence & core services is littered with similar examples of
             *
             * If (ProductCode == "PC") //do x
             * If (ProductCode == "LC") //do Y
             * If (ProductCode == "BI") //do Z
             *
             * The problem gets worse when you start to add more and more variations:
             *
             * If (ProductCode == "PC") //do x
             * If (ProductCode == "LC") //do Y
             * If (ProductCode == "BI") //do Z
             * If (ProductCode == "HO") //do A
             * If (ProductCode == "LI") //do B
             * If (ProductCode == "PT") //do C
             *
             * But it grows exponentially when the number of places in the code that need to make the decision increases:
             * for example the way you
             * (1) Save
             * (2) Load
             * (3) Edit
             * (4) Delete
             *
             * a risk, depending on whether it is a Car, Van, Bike, etc
             *
             *
             * If Statements and Switch Statements are evil - try to avoid them :)
             */


            return(average);
        }