public void CanGetCalculators() { var calcFlatRate = _calculatorFactory.GetCalculator(TaxCalculationType.FlatRate); Assert.IsInstanceOf <FlatRateCalculator>(calcFlatRate); var calcProgressive = _calculatorFactory.GetCalculator(TaxCalculationType.Progressive); Assert.IsInstanceOf <ProgressiveCalculator>(calcProgressive); var calcFlatValue = _calculatorFactory.GetCalculator(TaxCalculationType.FlatValue); Assert.IsInstanceOf <FlatValueCalculator>(calcFlatValue); }
public async Task <IActionResult> CalcMortgageAsync([FromBody] MortgageCalculationRequest request) { try { ValidateRequest(request); var calc = _calcFactory.GetCalculator(request); var result = await calc.CalculateAsync(request); return(Ok(result)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public string DoMath(string className, decimal a, decimal b) { var calculator = factory.GetCalculator(className); string results = calculator != null?calculator.Calculate(a, b).ToString() : "Invalid Results"; return(results); }
public void DoMath() { Console.WriteLine("Input number 1"); var num1 = int.Parse(Console.ReadLine()); Console.WriteLine("Input number 2"); var num2 = int.Parse(Console.ReadLine()); Console.WriteLine("Input the operator */+-"); var operand = Console.ReadLine(); try { ICalculator calculator = _calculatorFactory.GetCalculator(operand); try { Console.WriteLine("The answer is " + calculator.Calculate(num1, num2)); } catch (ArithmeticException) { Console.WriteLine("You cannot divide by zero"); } } catch (ArgumentException) { Console.WriteLine("You gave " + operand + " which is an invalid operand"); } }
public decimal CalculateTax(decimal salary, string postalCode) { var taxCalculationType = _taxCalculatorRepository.GetTaxCalculationTypeByPostalCode(postalCode); var taxAmount = _calculatorFactory.GetCalculator(taxCalculationType).DoCalculation(salary); _taxCalculatorRepository.SaveTaxResult(taxCalculationType, salary, postalCode, taxAmount); return(taxAmount); }
public decimal CalculateProbability(CalculateProbabilityRequest request) { ValidateRequest(request); var input = CalculateProbabilityInput.FromCalculateProbabilityRequest(request); var calculatedProbability = calculatorFactory.GetCalculator(request.CalculationType).Calculate(input); var executedCalculation = CreateExecutedCalculation(input, request.CalculationType, calculatedProbability); calculationsStorer.StoreCalculation(executedCalculation); return(calculatedProbability); }
public async Task <ActionResult <ApiResponse> > Create(TaxCalculationInput taxInput) { try { var result = _factory.GetCalculator(taxInput.PostalCode); var tax = result.calculator.Calculate(taxInput.Income, result.settings); var addResult = await _context.TaxValues.AddAsync(new TaxValue { Name = taxInput.Name, PostalCode = taxInput.PostalCode.ToUpper(), Income = taxInput.Income, Tax = tax }); var commitResult = await _context.SaveChangesAsync(); return(new ActionResult <ApiResponse>(new ApiResponse { Success = true, Result = addResult.Entity })); } catch (Exception ex) { return(new ActionResult <ApiResponse>(new ApiResponse { Success = false, Message = ex.Message, Details = ex.Source, StackTrace = ex.StackTrace })); } }
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); }