Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Final del ejercicio

  • Loading branch information...
commit eb5c9691f633ec9f19c209fc56628caa59d5caa7 1 parent 3fb9c1b
@yodogi yodogi authored committed
View
107 yodogi/src/org/kata/enero/StringCalculator.java
@@ -1,17 +1,17 @@
package org.kata.enero;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class StringCalculator {
- char delimiter;
+ private List<String> delimiters;
public StringCalculator() {
- delimiter = ',';
- }
-
- public StringCalculator(char delimiter) {
- setDelimiter(delimiter);
+ delimiters = new ArrayList<String>();
}
public int add(String numbers) throws PatternSyntaxException {
@@ -19,12 +19,11 @@ public int add(String numbers) throws PatternSyntaxException {
String[] operands = numbers.split("\n");
if (operands.length == 0) return result;
- int indx = 0;
- String defDelimiter = operands[indx].replaceAll("\\s+", "");
- if ((defDelimiter.indexOf("//") == 0) && (defDelimiter.length() == 3)) { // Initializate StringCalculator
- delimiter = defDelimiter.charAt(2);
- indx++;
- }
+ // Verificamos si se define el delimitador de operaciones
+ int indx = parseDelimiter(operands[0].replaceAll("\\s+", ""));
+
+ if (indx == 0)
+ delimiters.add(",");
while (indx < operands.length) {
int cal = calcutate(operands[indx].replaceAll("\\s+", ""));
@@ -43,33 +42,95 @@ public int add(String numbers) throws PatternSyntaxException {
return result;
}
+
+ private boolean parseSimpleDelimiter(String candidateDelimiter) { // Parseo delimitador simple
+ Pattern pattern;
+ Matcher matcher;
+
+ pattern = Pattern.compile("//([^0-9\\-\\[])");
+ matcher = pattern.matcher(candidateDelimiter);
+ if (matcher.find()) {
+ delimiters.add(matcher.group(1));
+ return true;
+ }
+
+ return false;
+ }
+
+ private boolean parseComplexDelimiter(String candidate) { // Parseo delimitador complejo
+ Pattern pattern;
+ Matcher matcher;
+
+ pattern = Pattern.compile("(\\/\\/)(\\[(.*?)\\])");
+ matcher = pattern.matcher(candidate);
+ if (matcher.find()) {
+ delimiters.add(matcher.group(3));
+ parseComplexDelimiter(candidate.substring(0, matcher.start(2)) + candidate.substring(matcher.end(2)));
+ return true;
+ }
+
+ return false;
+ }
+
+ private int parseDelimiter(String candidateDelimiter) {
- public void setDelimiter(char delimiter) {
- this.delimiter = delimiter;
+ delimiters.clear();
+
+ if (parseSimpleDelimiter(candidateDelimiter))
+ return 1;
+ else if (parseComplexDelimiter(candidateDelimiter))
+ return 1;
+ else
+ return 0;
}
- private int calcutate(String operands) {
+ private int calcutate(String expresion) {
int result = 0;
- String patternStr = "[" + delimiter + "]";
+
+ // Valida operadores
+ int numOperadores = 0;
+ String[] operadores = expresion.split("[\\d-]");
+ for (String operador: operadores)
+ if (!operador.equals(""))
+ if (delimiters.contains(operador))
+ numOperadores++;
+ else
+ throw new PatternSyntaxException("the follow delimiter not defined", operador, expresion.indexOf(operador));
- String[] operators = operands.split(patternStr);
+ // Determino expresión
+ String patternStr = "";
+ if (delimiters.size() > 1)
+ for (Object delimiter: delimiters.toArray())
+ patternStr += "[" + delimiter + "]";
+ else
+ patternStr = delimiters.get(0);
- if (operators.length == 1 && operands.contains("" + delimiter))
- throw new PatternSyntaxException("the following input is NOT ok", operands, operands.indexOf(delimiter) + 1);
+ // Determino operandos
+ int numOperandos = 0;
+ patternStr = "[" + patternStr + "]";
+ String[] operandos = expresion.split(patternStr);
+ for (String operando : operandos)
+ if (!operando.equals("")) numOperandos++;
+
+ // Valido la correspondencia entre operandos y operadores (cadena vacia no se procesa)
+ if ((numOperandos > 0) && (numOperandos - 1 != numOperadores))
+ throw new PatternSyntaxException("the following input is NOT ok", expresion, 0);
- for (String numStr : operands.split(patternStr))
+ for (String numStr : operandos) {
+ if (numStr.equals("")) continue;
try {
int num = Integer.parseInt(numStr);
if (num < 0)
- throw new PatternSyntaxException("negatives not allowed", operands, operands.indexOf(delimiter) + 1);
-
- result += num;
+ throw new PatternSyntaxException("negatives not allowed", expresion, expresion.indexOf(numStr));
+ else if (num <= 1000) // the numbers bigger than 1000 should be ignored
+ result += num;
} catch (NumberFormatException ex) {
// On exceptions numStr equal zero
}
+ }
return result;
View
22 yodogi/test/org/kata/enero/StringCalculatorTest.java
@@ -65,4 +65,26 @@ public void negativeNumberException() throws Exception {
}
+ @Test
+ public void numbersBiggerThanOnethousand() throws Exception {
+
+ assertEquals(7, cal.add("2,1001, 5"));
+
+ }
+
+ @Test
+ public void delimiterCanBeAnyLength() throws Exception {
+
+ assertEquals(16, cal.add("//[***]\n1***12***3"));
+
+ }
+
+ @Test
+ public void delimiterMultiple() throws Exception {
+
+ assertEquals(16, cal.add("//[***][aaaa]\n1***12aaaa3"));
+// assertEquals(48, cal.add("//[*][%]\n17*28%3"));
+// assertEquals(1, cal.add("1, \n"));
+
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.