Skip to content

Instantly share code, notes, and snippets.

Created March 6, 2013 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5098336 to your computer and use it in GitHub Desktop.
Save anonymous/5098336 to your computer and use it in GitHub Desktop.
Given this simple implementation of string calculator, mid way, how would you refactor this, based on SOLID principals? and how far would you go?
using NUnit.Framework;
namespace StringCalculatorTests
{
[TestFixture]
public class StringCalculatorTests
{
[TestCase("",0)]
[TestCase("1",1)]
[TestCase("1,2",3)]
public void Add_Empty_DefaultResults(string input,int expected)
{
StringCalculator sc = new StringCalculator();
int result = sc.Add(input);
Assert.AreEqual(result, expected);
}
}
public class StringCalculator
{
public int DEFAULT_RESULT = 0;
public int Add(string input)
{
if (input==string.Empty)
{
return DEFAULT_RESULT;
}
if (input.Contains(","))
{
return handleMultiple(input);
}
return ParseSingle(input);
}
private static int ParseSingle(string input)
{
return int.Parse(input);
}
private int handleMultiple(string input)
{
string[] numbers = input.Split(',');
return Add(numbers[0]) + Add(numbers[1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment