Skip to content
This repository
Browse code

Merge pull request #109 from manute/master

Kata Java
  • Loading branch information...
commit 12782e40132494409686d7204ecfd31e1111c33a 2 parents 8f1b624 + 1bbc372
authored
8 manute/.classpath
... ... @@ -0,0 +1,8 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<classpath>
  3 + <classpathentry kind="src" path="src/main/java"/>
  4 + <classpathentry kind="src" path="src/test/java"/>
  5 + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  6 + <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
  7 + <classpathentry kind="output" path="bin"/>
  8 +</classpath>
17 manute/.project
... ... @@ -0,0 +1,17 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<projectDescription>
  3 + <name>StringCalculator</name>
  4 + <comment></comment>
  5 + <projects>
  6 + </projects>
  7 + <buildSpec>
  8 + <buildCommand>
  9 + <name>org.eclipse.jdt.core.javabuilder</name>
  10 + <arguments>
  11 + </arguments>
  12 + </buildCommand>
  13 + </buildSpec>
  14 + <natures>
  15 + <nature>org.eclipse.jdt.core.javanature</nature>
  16 + </natures>
  17 +</projectDescription>
53 manute/src/main/java/com/kata/calculator/StringCalculator.java
... ... @@ -0,0 +1,53 @@
  1 +package com.kata.calculator;
  2 +
  3 +public class StringCalculator {
  4 +
  5 + private static final String DELIMITER = ",|\n";
  6 + private static final String PREFIX = "//";
  7 +
  8 + public int add(String text) {
  9 + if (text.length() == 0)
  10 + return 0;
  11 + checkNegatives(values(text));
  12 + return sum(values(text));
  13 + }
  14 +
  15 + private String[] values(String text) {
  16 + return numbers(text).split(delimiter(text));
  17 + }
  18 +
  19 + private String delimiter(String text) {
  20 + if (text.startsWith(PREFIX))
  21 + return text.substring(2, 3);
  22 + return DELIMITER;
  23 + }
  24 +
  25 + private String numbers(String text) {
  26 + if (text.startsWith(PREFIX))
  27 + return text.substring(3);
  28 + return text;
  29 + }
  30 +
  31 + private int sum(String[] values) {
  32 + int total = 0;
  33 + for (String value : values) {
  34 + total += toInt(value);
  35 + }
  36 + return total;
  37 + }
  38 +
  39 + private void checkNegatives(String[] values) {
  40 + String negatives = "";
  41 + for (String value : values) {
  42 + if (toInt(value) < 0)
  43 + negatives += "," + value;
  44 + }
  45 + if (negatives.length() > 0)
  46 + throw new RuntimeException(negatives.substring(1));
  47 + }
  48 +
  49 + private int toInt(String value) {
  50 + return Integer.parseInt(value);
  51 + }
  52 +
  53 +}
61 manute/src/test/java/com/kata/calculator/StringCalculatorTest.java
... ... @@ -0,0 +1,61 @@
  1 +package com.kata.calculator;
  2 +
  3 +import static org.junit.Assert.*;
  4 +import org.junit.Test;
  5 +
  6 +public class StringCalculatorTest {
  7 +
  8 + private StringCalculator stringCalculator = new StringCalculator();;
  9 +
  10 + @Test
  11 + public void shouldBeZeroOnEmptyString() {
  12 +
  13 + assertEquals(0, stringCalculator.add(""));
  14 + }
  15 +
  16 + @Test
  17 + public void shouldBeSameOneValue() {
  18 +
  19 + assertEquals(2, stringCalculator.add("2"));
  20 + }
  21 +
  22 + @Test
  23 + public void shouldSumMultipleValuesCommaDelimited() {
  24 +
  25 + assertEquals(3, stringCalculator.add("1,2"));
  26 + }
  27 +
  28 + @Test
  29 + public void shouldSumMultipleValuesBreakLineDelimited() {
  30 +
  31 + assertEquals(3, stringCalculator.add("1\n2"));
  32 + }
  33 +
  34 + @Test
  35 + public void shouldSumMultipleValuesStartsWith2LinesAndSemiColonDelimited() {
  36 +
  37 + assertEquals(3, stringCalculator.add("1\n2"));
  38 + }
  39 + @Test
  40 + public void shouldErrorNegativeValue() {
  41 +
  42 + try {
  43 + stringCalculator.add("-1");
  44 + fail();
  45 + } catch (Exception e) {
  46 + assertEquals("-1", e.getMessage());
  47 + }
  48 +
  49 + }
  50 + @Test
  51 + public void shouldErrorSumNegativeValues() {
  52 +
  53 + try {
  54 + stringCalculator.add("-1,2,-2");
  55 + fail();
  56 + } catch (Exception e) {
  57 + assertEquals("-1,-2", e.getMessage());
  58 + }
  59 +
  60 + }
  61 +}

0 comments on commit 12782e4

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