From 28f74050b0bccb5d8fc5ba865bb02067b3c0fe12 Mon Sep 17 00:00:00 2001 From: di7chro Date: Mon, 27 Apr 2015 14:51:03 +0200 Subject: [PATCH] Fully functional Added the String Calculator to Github --- .../src/session1/CalculateException.java | 10 +++ .../src/session1/StringCalculator.java | 46 ++++++++++++ .../test/session1/StringCalculatorTest.java | 74 +++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 StringCalculator/src/session1/CalculateException.java create mode 100644 StringCalculator/src/session1/StringCalculator.java create mode 100644 StringCalculator/test/session1/StringCalculatorTest.java diff --git a/StringCalculator/src/session1/CalculateException.java b/StringCalculator/src/session1/CalculateException.java new file mode 100644 index 0000000..61ba6bd --- /dev/null +++ b/StringCalculator/src/session1/CalculateException.java @@ -0,0 +1,10 @@ +package session1; + +public class CalculateException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public CalculateException(String msg) { + super(msg); + } +} diff --git a/StringCalculator/src/session1/StringCalculator.java b/StringCalculator/src/session1/StringCalculator.java new file mode 100644 index 0000000..124f461 --- /dev/null +++ b/StringCalculator/src/session1/StringCalculator.java @@ -0,0 +1,46 @@ +package session1; + +public class StringCalculator { + public int add(String numbers) throws CalculateException { + // Did we get some input? Good! Lets calculate! + if (numbers != "") { + + // Look for negative numbers + if (numbers.contains("-")) { + throw new CalculateException("Negatives not allowed"); + } + + // Do we need another delimiter? + if (numbers.contains("//")) { + numbers = numbers.replace("\n", ""); + String delimiter = String.valueOf(numbers.charAt(2)); + String[] split = numbers.split(delimiter); + return Integer.parseInt(split[1]) + Integer.parseInt(split[2]); + } + + // Replace all those \n with the standard delimiter + numbers = numbers.replace("\n", ","); + + // Two delimiters? We can't have that + if (numbers.contains(",,")) { + throw new CalculateException("Two separators found."); + } + + // A standard delimiter? Go ahead and calculate + if (numbers.contains(",")) { + String[] split = numbers.split(","); + return Integer.parseInt(split[0]) + Integer.parseInt(split[1]); + } + + // No delimiters? OK, you can have your number back them + else { + return Integer.parseInt(numbers); + } + } + + // No input? Then I got nothing to do. + else { + return 0; + } + } +} diff --git a/StringCalculator/test/session1/StringCalculatorTest.java b/StringCalculator/test/session1/StringCalculatorTest.java new file mode 100644 index 0000000..6223b32 --- /dev/null +++ b/StringCalculator/test/session1/StringCalculatorTest.java @@ -0,0 +1,74 @@ +package session1; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class StringCalculatorTest { + public StringCalculator calculator = new StringCalculator(); + + @Test + public void empty_String_Return_Zero() { + // Arrange + String numbers = ""; + + // Act + int result = calculator.add(numbers); + + // Assert + assertEquals(0, result); + } + + @Test + public void non_Empty_String_Return_3() throws Exception { + String numbers = "3"; + + int result = calculator.add(numbers); + + assertEquals(3, result); + } + @Test + public void comma_Separated_Return_Sum() throws Exception { + String numbers = "2,3"; + + int result = calculator.add(numbers); + + assertEquals(5, result); + } + + @Test + public void handle_Newlines_As_Commas() throws Exception { + String numbers = "4\n3"; + + int result = calculator.add(numbers); + + assertEquals(7, result); + } + + @Test + public void double_Separators_Throws_Exception() throws Exception { + try { + calculator.add("4\n,5"); + fail(); + }catch (CalculateException e) { + assertEquals("Two separators found.", e.getMessage()); + } + } + @Test + public void handle_Other_Delimiters() throws Exception { + String numbers = "//;\n5;6"; + + int result = calculator.add(numbers); + + assertEquals(11, result); + } + @Test + public void no_Negatives_Allowed() throws Exception { + try { + calculator.add("3,-4"); + fail(); + }catch (CalculateException e) { + assertEquals("Negatives not allowed", e.getMessage()); + } + } +}