public int RunOperation(OperationType opType) { string operationStr = ""; int result = 0; string delimiterScrubbedOpParams = ProcessDelimiters(InputParameters.OperationValues, InputParameters.CustomDelimiter); List <int> opValues = new List <int>(); switch (opType) { case OperationType.Addition: operationStr = "+"; opValues = ProcessOperationParameters(operationStr, delimiterScrubbedOpParams, InputParameters); result = Add(opValues); break; case OperationType.Subtraction: operationStr = "-"; opValues = ProcessOperationParameters(operationStr, delimiterScrubbedOpParams, InputParameters); result = Subtract(opValues); break; case OperationType.Multiplication: operationStr = "*"; opValues = ProcessOperationParameters(operationStr, delimiterScrubbedOpParams, InputParameters); result = Multiplication(opValues); break; case OperationType.Division: operationStr = "/"; opValues = ProcessOperationParameters(operationStr, delimiterScrubbedOpParams, InputParameters); result = Division(opValues); break; } CalcLog.Append($" = {result}"); Console.WriteLine(GetLog()); ClearLog(); return(result); }
private List <int> ProcessOperationParameters(string logOpStr, string scrubbedOpParams, InputParameters inputParams) { List <int> opValueList = new List <int>(); string[] rawValues = scrubbedOpParams.Split(','); int upperBound = inputParams.UpperBound.GetValueOrDefault(0); if (rawValues == null || rawValues.Length < 2) { CalcLog.Append($"Invalid parameter for operation. Parameter: {scrubbedOpParams}"); return(null); } int cnt = 0; bool foundNegNum = false; StringBuilder sbNegNum = new StringBuilder(); foreach (string val in rawValues) { if (cnt > 0) { CalcLog.Append(logOpStr); } if (Int32.TryParse(val, out int adInt)) { // check for negative number if (adInt < 0) { if (inputParams.AllowNegativeNumbers) { opValueList.Add(adInt); CalcLog.Append($"{adInt}"); } else { // save the negative number for printing in the exception if (sbNegNum.Length > 0) { sbNegNum.Append(","); } sbNegNum.Append(adInt); foundNegNum = true; } } // set upper bound if one was chosen else if (upperBound > 1) { if (adInt < upperBound) { opValueList.Add(adInt); CalcLog.Append($"{adInt}"); } else { CalcLog.Append("0"); } } else { opValueList.Add(adInt); CalcLog.Append($"{adInt}"); } } else { opValueList.Add(0); CalcLog.Append("0"); } cnt++; } if (foundNegNum) { CalcLog.Append("Negative values found: " + sbNegNum); throw new ArgumentOutOfRangeException("Negative values are not allowed! Set the proper parameter to allow negatives values."); } return(opValueList); }