Created
March 6, 2013 10:19
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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