Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions jomarmen/StringCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;

namespace KataStringCalculator
{
public class StringCalculator
{
private const String SEPARADOR_DEFECTO = ",";
private const String SEPARADOR_DEFECTO_2 = "\n";
private const int VALOR_DEFECTO = 0;
private const string SEPARADOR_INICIAL = "//[";
private const string SEPARADOR_FINAL = "]";
private const int NUMERO_MAXIMO = 1000;

public int Add(string numbers)
{
List<String> separadores = RecuperaSeparadores(numbers);
numbers = LimpeaDefinicionSeparadorOpcional(numbers, separadores);

return Suma(numbers, separadores);
}

private int Suma(string cadena, List<String> separadores)
{
int resultado = VALOR_DEFECTO;
foreach (string number in cadena.Split(separadores.ToArray(), StringSplitOptions.RemoveEmptyEntries))
{
int numero = Convert.ToInt32(number);
if (ValidarNumeros(Convert.ToInt32(numero)))
{
resultado += numero;
}
}
return resultado;
}

private bool ValidarNumeros(int numero)
{
bool resultado = true;
if (numero < 0)
{
throw new ArgumentException("Negatives not allowed");
}
if (numero > NUMERO_MAXIMO)
{
resultado = false;
}
return resultado;
}
private List<String> RecuperaSeparadores(string cadena)
{
List<String> separadores = new List<String>();
separadores.Add(SEPARADOR_DEFECTO);
separadores.Add(SEPARADOR_DEFECTO_2);

string[] separadorOpcional = RecuperaDelimitadorOpcional(cadena);
separadores.AddRange(separadorOpcional);

return separadores;
}

private string LimpeaDefinicionSeparadorOpcional(string cadena, List<String> separadores)
{

foreach(string separador in separadores)
{
cadena = cadena.Replace(SEPARADOR_INICIAL + separador, "").Replace(SEPARADOR_FINAL, "").Replace("["+separador,"");
}
return cadena;
}

public string[] RecuperaDelimitadorOpcional(string cadena)
{
if (cadena.StartsWith(SEPARADOR_INICIAL))
{
string[] parametros = cadena.Split('\n');
string parteConDelimitador = parametros[0];
parteConDelimitador = parteConDelimitador.Replace(SEPARADOR_INICIAL, "");
parteConDelimitador = parteConDelimitador.Replace(SEPARADOR_FINAL, "");

string[] delimitadores = parteConDelimitador.Split('[');

return delimitadores;

}
return null;
}

}
}
114 changes: 114 additions & 0 deletions jomarmen/StringCalculatorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;


namespace KataStringCalculator
{
[TestFixture]
public class StringCalcultorTests
{

[Test]
public void TestEmpty()
{
StringCalculator sc = new StringCalculator();
Assert.AreEqual(0, sc.Add(""));

}

[Test]
public void Test1N()
{
StringCalculator sc = new StringCalculator();
Assert.AreEqual(1, sc.Add("1"));

}

[Test]
public void Test2N()
{
StringCalculator sc = new StringCalculator();
Assert.AreEqual(3, sc.Add("1,2"));
}

[Test]
public void TestNParametros()
{
StringCalculator sc = new StringCalculator();
Assert.AreEqual(10,sc.Add("1,2,3,4"));
}

[Test]
public void TestMultipleSeparador()
{
StringCalculator sc = new StringCalculator();
Assert.AreEqual(15, sc.Add("1\n2,3\n4,5"));
}

[Test]
public void TestSuperMultipleSeparador()
{
StringCalculator sc = new StringCalculator();
string parametro = "";
int resultado = 0;
for (int i = 0; i < 100; i++)
{
string separador = ",";
if ((i % 2) == 2)
{
separador = "\n";
}
if (i == 0)
{
parametro += i;
}
else
{
parametro += separador + i;
}

resultado += i;

Assert.AreEqual(resultado, sc.Add(parametro));
}
}

[Test]
public void TestConDelimitadorOpcional()
{
StringCalculator sc = new StringCalculator();
Assert.AreEqual(10, sc.Add("//[$]\n1,2\n3$4"));
}

[Test, ExpectedException(typeof(ArgumentException), ExpectedMessage = "Negatives not allowed")]
public void TestExceptionNumerosNegativos()
{
StringCalculator sc = new StringCalculator();
sc.Add("//[$]\n1,2\n-3$4");
}

[Test]
public void TestIgnorarNumerosSuperioresAMil()
{
StringCalculator sc = new StringCalculator();
Assert.AreEqual(7, sc.Add("//[$]\n1,2\n1001$4"));
}
[Test]
public void TestDelimitadoresLargo()
{
StringCalculator sc = new StringCalculator();
Assert.AreEqual(6, sc.Add("//[***]\n1***2***3"));
}

[Test]
public void TestVariosDelimitadores()
{
StringCalculator sc = new StringCalculator();
Assert.AreEqual(6, sc.Add("//[*][%]\n1*2%3"));
}
}
}