About the Application Project - Hyland RPA - Foundation 23.2 - Foundation 23.2 - Ready - Hyland RPA - external

Hyland RPA

Platform
Hyland RPA
Product
Hyland RPA
Release
Foundation 23.2
License

The application project contains the business logic, related data structures, interfaces to other services or databases.

Because we created already a functioning unit test, we can start with the implementation of the business logic.

Since this tutorial focuses on the development of activities, we will not go into to much detail of the CalculatorApplication implementation. See below for a complete implementation of CalculatorApplication.

public class CalculatorApplication : ICalculatorApplication
{
private const string ArithmeticSignPattern = @"[\+\-\*\/]";

public int CalculateFromString(string calculationString)
{
string arithmeticSignString = this.GetElementaryArithmeticString(calculationString);
int firstNumber =
this.GetFirstNumber(calculationString, arithmeticSignString);

int secondNumber =
this.GetSecondNumber(calculationString, arithmeticSignString);

var arithmeticSign = this.GetElementaryArithmetic(arithmeticSignString);

return this.Calculate(firstNumber, secondNumber, arithmeticSign);
}
public int Calculate(int firstNumber, int secondNumber, ElementaryArithmetic elementaryArithmetic)
{
switch (elementaryArithmetic)
{
case ElementaryArithmetic.Addition:
return firstNumber + secondNumber;

case ElementaryArithmetic.Subtraction:
return firstNumber - secondNumber;

case ElementaryArithmetic.Division:
try
{
return firstNumber / secondNumber;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}

case ElementaryArithmetic.Multiplication:
return firstNumber * secondNumber;
}
return 0;
}

private string GetElementaryArithmeticString(string calculationString)
{
Regex regex = new Regex(ArithmeticSignPattern);

var matches = regex.Matches(calculationString);

if (matches.Count != 1)
{
Console.WriteLine("More than one calculation sign detected.");
}

return matches.Cast<Match>().FirstOrDefault()?.Value;
}

private ElementaryArithmetic GetElementaryArithmetic(string arithmeticSign)
{
switch (arithmeticSign)
{
case "+":
return ElementaryArithmetic.Addition;

case "-":
return ElementaryArithmetic.Subtraction;

case "*":
return ElementaryArithmetic.Multiplication;

case "/":
return ElementaryArithmetic.Division;

default:
Console.WriteLine("Elementary Arithmetic sign not found");

return ElementaryArithmetic.Addition;
}
}

private int GetFirstNumber(string calculationString, string arithmeticSign)
{
string firstNumberString = calculationString.Substring(0, calculationString.IndexOf(arithmeticSign, StringComparison.Ordinal));
if (!int.TryParse(firstNumberString.Trim(), out int firstNumber))
{
Console.WriteLine("First number not found");
}
return firstNumber;
}

private int GetSecondNumber(string calculationString, string arithmeticSign)
{
int startIndex = calculationString.IndexOf(arithmeticSign, StringComparison.Ordinal) + 1;
string secondNumberString = calculationString.Substring(startIndex, calculationString.Length - startIndex);

if (!int.TryParse(secondNumberString.Trim(), out int secondNumber))
{
Console.WriteLine("Second number not found");
}
return secondNumber;
}
}

Now that CalculatorApplication is implemented correctly the unit test will now pass successfully.