Skip to content

Commit

Permalink
Permitir "\n" como separador además de las comas
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoguemes committed Feb 6, 2012
1 parent 2fa8ae4 commit 271f571
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions diegoguemes/EneroStringCalculator/StringCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ namespace EneroStringCalculator
{
public class StringCalculator
{
private const string SEPARATOR = ",";
private const string COMMA_SEPARATOR = ",";
private const string NEW_LINE_SEPARATOR = "\n";

public int Add(string numbers)
{
if(string.IsNullOrEmpty(numbers))
return 0;
if(!ContainsSeparator(numbers))
return int.Parse(numbers);
var parsedNumbers = numbers.Split(SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var separators = new string[] { COMMA_SEPARATOR, NEW_LINE_SEPARATOR };
var parsedNumbers = numbers.Split(separators, StringSplitOptions.RemoveEmptyEntries);
return parsedNumbers.Sum(n => int.Parse(n));
}

private bool ContainsSeparator(string numbers)
{
return numbers.Contains(SEPARATOR);
return numbers.Contains(COMMA_SEPARATOR) || numbers.Contains(NEW_LINE_SEPARATOR);
}
}
}
6 changes: 6 additions & 0 deletions diegoguemes/EneroStringCalculator/StringCalculatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@ public void AddWithMultipleNumbers()
{
Assert.AreEqual(50, calculator.Add("2,3,5,10,30"));
}

[Test]
public void AllowAddWithNewLineSeparator()
{
Assert.AreEqual(10, calculator.Add("2\n3,5"));
}
}
}

0 comments on commit 271f571

Please sign in to comment.