public void GetValueByRatesTest() { //Arrange double value = 4; double leftRate = 1.2; double rightRate = 0.8; double expected = 2.66666666667; //Act var actual = CalculationStation.GetValueByRates(value, leftRate, rightRate); //Assert //Close enough Assert.IsTrue(Math.Abs(expected - actual) < 0.01); }
//Calculation public void MakeCalculation(bool isFirst) { //exit if already calculating or if data isn't loaded if (isCalculating || !IsDataLoaded) { return; } try { isCalculating = true; //Default to 1 double firstValue = FirstAmount != 0 ? FirstAmount : 1; double secondValue = SecondAmount != 0 ? SecondAmount : 1; int leftIndex = FirstCurrency; int rightIndex = SecondCurrency; //If they have the same currency, flip the last pair of currencies around if (leftIndex == rightIndex) { leftIndex = CurrentState.SecondIndex; rightIndex = CurrentState.FirstIndex; FirstCurrency = leftIndex; SecondCurrency = rightIndex; } //Order of calculations based on which block of elements was updated if (isFirst) { SecondAmount = Math.Round( CalculationStation.GetValueByRates( firstValue, _currencies[leftIndex].Rate, _currencies[rightIndex].Rate ), 2); } else { //Don't calculate tax FirstAmount = Math.Round( CalculationStation.GetValueByRates( secondValue, _currencies[rightIndex].Rate, _currencies[leftIndex].Rate ), 2); } CurrentState.FirstIndex = leftIndex; CurrentState.SecondIndex = rightIndex; double taxValue = 0; //Show or hide Tax if (_taxInput > 0) { taxValue = Math.Round(SecondAmount * (_taxInput * 0.01), 2); TaxOutput = $"+{taxValue}"; TaxVisible = true; } else { _taxInput = 0; TaxVisible = false; } //Final display FirstOutput = $"{_firstAmount}"; SecondOutput = $"{_secondAmount + taxValue}"; FirstName = $"{_currencies[CurrentState.FirstIndex].Name}"; SecondName = $"{_currencies[CurrentState.SecondIndex].Name}"; } catch (Exception ex) { Debug.WriteLine("\tView Model Calculation Failed: {0}", ex.Message); } finally { CurrentState = CurrentState; isCalculating = false; } }