From 4cae56665025ef20af7c829532c5296a0dbc2cab Mon Sep 17 00:00:00 2001 From: Pepetti Date: Mon, 1 Oct 2018 21:58:50 +0300 Subject: [PATCH 1/2] Assignment Finished --- bin/StringCalculator.class | Bin 405 -> 2190 bytes bin/StringCalculatorTest.class | Bin 505 -> 3978 bytes src/StringCalculator.java | 62 +++++++++-- src/StringCalculatorException.java | 2 +- tests/StringCalculatorTest.java | 158 ++++++++++++++++++++++++++++- 5 files changed, 212 insertions(+), 10 deletions(-) diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c43633d36f133837cd90ddd10fb14ac782..9a09888fbae0067d9ae496adde7a8a7fd2a8938f 100644 GIT binary patch literal 2190 zcmaJ>TUQfT7~Lm#!YGn}Ap{V_N=O0$M5W zbFHrSr4RNc+RLKv{Wtvqz3ekHD+EIOkj%M!XYc*(?>py@zkfLdFpIq~d;;e-ZObr= z%W7HMFROOd3M0VPUG=$|FRNxTzp=fmYqmh>hG81^qJTd={y-qGT;0*5@S{UP5FvrC zRm0TR_bc1Fbx++c>jJUWs-~77sFoq;-eSNm8FjgLl6Qf~>duZpZ+i8J-HlopF9<|e z4m7=H8&$I&Mob`i5}~;tg+PyjUYr*=V^`g{CCgG@2#lSEzSYhsG5QoF(NAW(S}<)7 zAdx;LdVH(Ui2)3SF(}aQ$lte(a=va`x?0KeG(~Wv-~xsPeAQb7nh8iUsLqG}t@^$6xEJr~6dEQayUv9@*D6uc{&AXu-J z4fb%*)(`Ay?>pmtuXx|tC|2-(7`G|=$WxY1FY1cor_9+A1U}4n*`9RrmY$#&!k%ITrR2BrvAmgZfg3%_~R(L z(GdafnQ)FjRPYFo1;UnIE2|nW*RB6N-BOX`s4W|=JG^oM=|}`mNHi6}=K_#wRR!C! z>q2GSEZU_AcrC+QQxn&7PqK3VnSvrpT&t+IR??Zwsh5_5qnp)zOVe)~@fIgSII zbvrNzTnTceKa*`BzLv=~kXWC1gVfI$5ts|~27ctOiyh-R@KlIW6m%d#M#(elB6v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(C*6}Hx7D=Ix z5NMX|NNX@sNph4%knvy8Ry6&(U9dTZu8OqG&>L)YdtP%EpkuI2(R6l6&MKywL}KI0 zB3j0(;-?J4k1`v&NLgm1-L_#ZUnp5FP}_#4hW&&CT@^x>tRojqrFbbZ`>P^dryCGS zP9b9z=8>huC0orBas_Luc_>9okR# zuxD_*kS(s@FBrN3#@lz)C=2;kaoGyhd6q_gWinwHhL$@%SJ!oLJx0(UM99QXsJ9LFOIC%N9F=5fl6PCd5x1u|oy1rMDg8nj+N`Ug^Zl zM%aVE9tP$JAPysdBgpYl7j}pXz26bKj`ncu4hOpjLKm>3n_%BVz^tk}O0dV`(kX}X zbg8($W8Fv0>h`^xs58iiff$?3=9k=4AK+1X0o!Bq!yPez$?cQ6QV1hSYIxPT-VkY-@u1nTEPGK~#xnj~1I6aX0qCk6lj diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..9d59a7b 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,59 @@ +import java.util.Scanner; +import java.util.stream.IntStream; public class StringCalculator { - public int add(String numbersStr) { - // Returns the sum of the numbers given in numbersStr - - // not yet implemented - return 0; + + public int add(String numbersStr) throws StringCalculatorException { + String[] strArray = toStringArray(numbersStr); + int[] intArray = toIntArray(strArray); + int sum = IntStream.of(intArray).sum(); + return sum; } -} + + public String[] toStringArray (String numbersStr) throws StringCalculatorException { + String text1 = handleUnknownNumbers(numbersStr); + String text2 = handleEmptyString(text1); + String text3 = handleLineBreakAndComma(text2); + String text4 = handleAlphabeticAndNegative(text3); + String[] split = text4.split(","); + return split; + } + + public int[] toIntArray (String[] arr) { + int[] ints = new int [arr.length]; + for (int i = 0; i < arr.length; i++) { + ints[i] = Integer.parseInt(arr[i]); + } + return ints; + } + + public String handleLineBreakAndComma (String numbersStr) throws StringCalculatorException{ + if (numbersStr.contains(",\n")) { + throw new StringCalculatorException(); + }else{ + String text = numbersStr.replace("\n", ","); + return text; + } + } + public String handleEmptyString (String numbersStr) { + if (numbersStr == "0") { + return ""; + }else { + return numbersStr; + } + } + public String handleUnknownNumbers(String numbersStr) throws StringCalculatorException { + if (numbersStr.length() > 3 && numbersStr.contains("\n") == false) { + throw new StringCalculatorException(); + }else { + return numbersStr; + } + } + public String handleAlphabeticAndNegative(String numbersStr) throws StringCalculatorException { + if (numbersStr.matches("[0-9,]+") == false) { + throw new StringCalculatorException(); + }else { + return numbersStr; + } + } +} \ No newline at end of file diff --git a/src/StringCalculatorException.java b/src/StringCalculatorException.java index da71147..0856125 100644 --- a/src/StringCalculatorException.java +++ b/src/StringCalculatorException.java @@ -1,4 +1,4 @@ public class StringCalculatorException extends Exception { - + } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..e3f2dcc 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -1,12 +1,164 @@ import static org.junit.Assert.*; +import java.util.Arrays; + import org.junit.Test; public class StringCalculatorTest { @Test - public void test() { - fail("Not yet implemented"); + public void testOneNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String[] result = {"1"}; + int[] intArray = calculator.toIntArray(result); + int[] ints2 = {1}; + //Assert + assertArrayEquals(intArray, ints2); + } + + @Test + public void testTwoIntNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String[] result = {"1","2"}; + int[] intArray = calculator.toIntArray(result); + int[] ints2 = {1,2}; + //Assert + assertArrayEquals(intArray, ints2); + } + + @Test + public void testTwoStringNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1,2"; + String[] stringArray = calculator.toStringArray(result); + String[] stringArray2 = {"1", "2"}; + //Assert + assertArrayEquals(stringArray, stringArray2); + } + + @Test + public void testThreeStringNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1\n2,3"; + String[] stringArray = calculator.toStringArray(result); + String[] stringArray2 = {"1","2","3"}; + //Assert + assertArrayEquals(stringArray, stringArray2); + } + + @Test + public void testLineBreak() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1\n2,3"; + String string = calculator.handleLineBreakAndComma(result); + String string2 = "1,2,3"; + //Assert + assertEquals(string, string2); + } + + @Test (expected = StringCalculatorException.class) + public void testComma() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1,\n2,3"; + // This statement should cause an exception + calculator.handleLineBreakAndComma(result); + } + @Test + public void testEmptyString() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "0"; + String string = calculator.handleEmptyString(result); + String string2 = ""; + //Assert + assertEquals(string, string2); + System.out.println(string); + } + + @Test (expected = StringCalculatorException.class) + public void testUnknowNumbers() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1,2,3"; + // This statement should cause an exception + calculator.handleUnknownNumbers(result); + } + @Test (expected = StringCalculatorException.class) + public void testAlphapeticAndNegative() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "A"; + // This statement should cause an exception + calculator.handleAlphabeticAndNegative(result); + } + @Test (expected = StringCalculatorException.class) + public void testAlphapeticAndNegative2() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "-2"; + // This statement should cause an exception + calculator.handleAlphabeticAndNegative(result); + System.out.println(result); + } + + @Test + public void testAddLineBreak() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1\n2,3"; + int toInt = calculator.add(result); + int sum = 6; + //Assert + assertEquals(toInt, sum); + } + @Test + public void testAddTwoNumbers() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1,2"; + int toInt = calculator.add(result); + int sum = 3; + //Assert + assertEquals(toInt, sum); + } + @Test + public void testAddZeroAndNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "0,2"; + int toInt = calculator.add(result); + int sum = 2; + //Assert + assertEquals(toInt, sum); + } + @Test + public void testAddOneNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "2"; + int toInt = calculator.add(result); + int sum = 2; + //Assert + assertEquals(toInt, sum); } - } From ef3410c77945aab59ffa1551cb9530812924f23a Mon Sep 17 00:00:00 2001 From: ainaliantti Date: Fri, 1 Nov 2019 00:23:46 +0200 Subject: [PATCH 2/2] Removed unused import --- bin/StringCalculatorTest.class | Bin 3978 -> 3978 bytes tests/StringCalculatorTest.java | 2 -- 2 files changed, 2 deletions(-) diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index cb1958228ccd8e0b1fd4759e2ed856db7630049d..e9eabbc8c2b4b1aa9c869e898514a97058d2192f 100644 GIT binary patch delta 377 zcmW;HOD_Xq7{&2JqSJORHR7ekq|vxlkh*juDr`hpG$mz0goOwT5toG^V=;q_CenFA z;!>B5wJ+cc*jf5E4%`3lJ#X>^=^*{#Md}*Fj~uBuqA<_|6G>R;L?e1(OG0~BEtDi$ zk%EIZ^rHhgIP#^{REE+89o?|e!%ZJIS#BJ;kNRqenxhU=N9bc1Kwd^;RW(kTpcE*R zP?$nHrsXGAF=r^VlsU@03>sN^jIX4MtR>b3)kc6sHwl*~|hlncrwe|GP(4tdC^gD6<(2aGx6?cT_3(cIW1c^u delta 377 zcmW;H+bhFi9LMnw7yE5?GCY~XERw@e%N%Z;;zlAZ7E%%y5*L!W5sNml=3;vYIZTvm z|A2qMolF0XH+N6(*Z2F>Hy_PMKeDb0$L3sVGNQb;khSbGQj8_jk5qs~>nK;^yCeQ)qhSaNJoA4CP2r`C@BNH;3 z