Skip to content
Browse files

Merge pull request #109 from manute/master

Kata Java
  • Loading branch information...
2 parents 8f1b624 + 1bbc372 commit 12782e40132494409686d7204ecfd31e1111c33a @12meses12katas committed Apr 8, 2012
View
8 manute/.classpath
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src/main/java"/>
+ <classpathentry kind="src" path="src/test/java"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
View
17 manute/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>StringCalculator</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
View
53 manute/src/main/java/com/kata/calculator/StringCalculator.java
@@ -0,0 +1,53 @@
+package com.kata.calculator;
+
+public class StringCalculator {
+
+ private static final String DELIMITER = ",|\n";
+ private static final String PREFIX = "//";
+
+ public int add(String text) {
+ if (text.length() == 0)
+ return 0;
+ checkNegatives(values(text));
+ return sum(values(text));
+ }
+
+ private String[] values(String text) {
+ return numbers(text).split(delimiter(text));
+ }
+
+ private String delimiter(String text) {
+ if (text.startsWith(PREFIX))
+ return text.substring(2, 3);
+ return DELIMITER;
+ }
+
+ private String numbers(String text) {
+ if (text.startsWith(PREFIX))
+ return text.substring(3);
+ return text;
+ }
+
+ private int sum(String[] values) {
+ int total = 0;
+ for (String value : values) {
+ total += toInt(value);
+ }
+ return total;
+ }
+
+ private void checkNegatives(String[] values) {
+ String negatives = "";
+ for (String value : values) {
+ if (toInt(value) < 0)
+ negatives += "," + value;
+ }
+ if (negatives.length() > 0)
+ throw new RuntimeException(negatives.substring(1));
+ }
+
+ private int toInt(String value) {
+ return Integer.parseInt(value);
+ }
+
+}
View
61 manute/src/test/java/com/kata/calculator/StringCalculatorTest.java
@@ -0,0 +1,61 @@
+package com.kata.calculator;
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+public class StringCalculatorTest {
+
+ private StringCalculator stringCalculator = new StringCalculator();;
+
+ @Test
+ public void shouldBeZeroOnEmptyString() {
+
+ assertEquals(0, stringCalculator.add(""));
+ }
+
+ @Test
+ public void shouldBeSameOneValue() {
+
+ assertEquals(2, stringCalculator.add("2"));
+ }
+
+ @Test
+ public void shouldSumMultipleValuesCommaDelimited() {
+
+ assertEquals(3, stringCalculator.add("1,2"));
+ }
+
+ @Test
+ public void shouldSumMultipleValuesBreakLineDelimited() {
+
+ assertEquals(3, stringCalculator.add("1\n2"));
+ }
+
+ @Test
+ public void shouldSumMultipleValuesStartsWith2LinesAndSemiColonDelimited() {
+
+ assertEquals(3, stringCalculator.add("1\n2"));
+ }
+ @Test
+ public void shouldErrorNegativeValue() {
+
+ try {
+ stringCalculator.add("-1");
+ fail();
+ } catch (Exception e) {
+ assertEquals("-1", e.getMessage());
+ }
+
+ }
+ @Test
+ public void shouldErrorSumNegativeValues() {
+
+ try {
+ stringCalculator.add("-1,2,-2");
+ fail();
+ } catch (Exception e) {
+ assertEquals("-1,-2", e.getMessage());
+ }
+
+ }
+}

0 comments on commit 12782e4

Please sign in to comment.
Something went wrong with that request. Please try again.