From 143d5167070b7a092901cd6bd66ed7a4c265a13b Mon Sep 17 00:00:00 2001 From: nmarkuks18 <43221549+nmarkuks18@users.noreply.github.com> Date: Mon, 1 Oct 2018 23:31:53 +0300 Subject: [PATCH] Assignment finished --- bin/StringCalculator.class | Bin 405 -> 2806 bytes bin/StringCalculatorException.class | Bin 303 -> 489 bytes bin/StringCalculatorTest.class | Bin 505 -> 2963 bytes src/StringCalculator.java | 109 +++++++++++++++++++++++++++- src/StringCalculatorException.java | 4 + tests/StringCalculatorTest.java | 79 +++++++++++++++++++- 6 files changed, 187 insertions(+), 5 deletions(-) diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c43633d36f133837cd90ddd10fb14ac782..7dc706f1e4d4e8e28530476bbd7e9b6926f786f1 100644 GIT binary patch literal 2806 zcmZ`*U3U{z6x}zOWRi4BNlH>aT4)jJN75Dw6f97LLbcE|h14Pzk!dn*r{s%CQsJFv z|A6|yS}yzIgZL0!>Zb|Z2p}lXHE))Tg?e$-u$J`If*}x_FX!~avTm7j?k*~|4YMlu-i~{bp7nfQAU-tT zjP6)n9G(-1+<1~RDmAlQs;Yw;zGP0S$2^1hm|Sk$W_^cB5FSJry|F&iUSW z(!)Uwhopy2nOJqHJX0i_{;LxF%A6~ASBBumbZ*I z2L{_gT4|5z9StY&F3Gkd&z3C`ZdhhcATiYD$gmxEP{R<01v+LnSWsiBJXc~pd)gjY znUhG98d4Y)hLWny_n&t6DYOuz78hY_!2p0rG*?O_xtWNbuFojDhsE%%16}yc!T$Y6h zROESSdPT0hPk9azBsEjA1zDwQskuG6l7VX0Rm;+;jqWxK(tw^_1K$c|=&#m`0)iu9 zQXJWuo_mzmE4I8<%|@ZhNkqF9oR(-*-qK2~v;WH-^VK*qXZ zX}E_jBBO-v|+!J%d?Mt@cHfsov1$-$RbX7x5vZ02Bzc6 zcMRpXfld<)2s21{5@3|`9zJ^!!*O(D5C?b-B$#IpW^fQU`Ch_d+{Y2*(Pt+(&C`VL zCMZW&agC|9-F?lRJTL(D^f>_hOls$o?wJ+!}3|(MEW9tR-61ix#yr7j~A6CUfCU z?B?<{i3rf$iDWvp^GjrbCEZ4JG_IuL%EXWODDzAaoa{V*{r6}y$-_3`r-|+iNj%50 zoo8vzG36;6!db_s2jK)hVHwy3v{AYO`;sc({y!(h_4dyHOk`(zlF0l;t|K8hj_px z&4*+7lr}!@_u4r9HqH+C|Ai_w(&Zyceh>Gc{GCUnE+<;4?QfxW8(mX@W>r0=|9C-< zouuUm9=LTrX*cIh#z+%>hP$`9e}@)X61l`K$>22Z+Nn6*FyW;#;gNsJBR}bpKj@O* z$8LNG-R*`Llx7*&3J*GDmi#Ber@VF2#3gmk7Fdnt8(2%Hq&l3tC&D)vAWY_@{QDhK z56~O=hiSLTI!M?2PvW|scU?!4vAnN=M|(Z5@&lG@@f`6yN8H8=(P!D#nUH^yINRM> z1vf?_8LKp4UGDv|RX$DCkOtykXUZnyZSoE%w8Tw#aT9J_m6unYTK3r*;T8Q4;_`al Ivbp*EKXUB*B>(^b delta 130 zcmew+I+dC0)W2Q(7#J8#7(^y=ZJ7LyNpUhCs~#iMWLs9PL?#AqpcD@SBambT(!4;L zk%5naA4swT*=9hR2}mm^fx2?Z^J~zM(SE=qUyyopwFc1835Doo)@-m8soz_5+>Bf(t z#wKUTq7`}VOm~A6<$oj7w_d-8B>;21=IDcbd&S98xJ6ZNLB>NykR^}9 WnXu~K&p9<=lH`IqN^mL6qVWrkl{`%V delta 106 zcmaFKyq<~c)W2Q(7#J8#7z8GAIg2uDXofK|usG+Zq;fGZGjQ@SurjbseCjtjkkQpj spMenw85mf#wli>S1TvTyxPT-ZSdfW<8_4E?C;-yDKprClA6OMX072Rf*#H0l diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index d44b83f48579a95c814a0008fdfcca590443d2de..2df168e9ec9d9ee401a1aa0dc0d7f8d1a5b6dee2 100644 GIT binary patch literal 2963 zcmbW2TUQfT9L4v6gbX1{94-NGh*sqipj<>PUP=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32Kh9_IR7-U{f>F)LaVCl*TnW z{fV81C^y}ZQ@udpk~01a4brfOnes5VCE(*0o$3=P z^fiGb&STCcyW|yVl8yLe2{LSwr-wY*0G@W!eqn8C+G+zOz_IPVKcdCnP_`HfXN%#! ztmoiY$0)QIJqMTQ;0pO}kn$#fZ?!qN+T@(~InU#)&zW=`eC9g%O#pG1gNE*06*pGcY(zu6-CzLV$y zi8e{JMWToQnMeyDYEL!C`P{MS`)AeNcPRgSjJ+0vx9HPk$Z*kTUHy+Q`j`KrTJTlJ k8lk9(VN_>T^CP-B>J~VopS0XZL?JV`ASX39 zHLoN!g^NLiL5!V2bn<#eQ7&-?2}TB%w8YGu$p@IECY!MtF)~fgWYfxKW#9%H%EQ12 zBw2wpFOX(r;A3E8U}E40ve|$<0U*r|#3n#CNFAGy;0^}ijX)L?0~e6w0@4f&oIw3t ONT#vDO_KzxlmY+`kQ^QW diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..b69f21a 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,112 @@ +//import java.text.ParseException; public class StringCalculator { - public int add(String numbersStr) { + + + + public int add(String numbersStr) throws StringCalculatorException { + // Returns the sum of the numbers given in numbersStr - // not yet implemented - return 0; + numbersStr=checkIfEmpty(numbersStr); + numbersStr=LinesToCommas(numbersStr); //replaces "\n" with "," + checkInvalidCommas(numbersStr); //Checks if there is more than 1 comma in a row + + String [] StrArray=removeCommas(numbersStr); //removes commas from the string + + checkForNumeric(StrArray);//checks if is numeric, if not, throws exception + int[] intArray=ChangeToInt(StrArray);//Strings to integer + checkForNegatives(intArray);//Checks that numbers are positive, if not, throws exception + checkNumberCount(intArray);//Checks how many numbers given, if more than 2, throws exception + + int sum=0; + for (int i : intArray) { + sum=sum+i; + } + System.out.print("Sum is: "); + System.out.print(sum); + + return sum; + } + + public String checkIfEmpty(String numbersStr) { + if (numbersStr=="") { + numbersStr="0"; + } + return numbersStr; + } + + public void checkNumberCount(int[] intArray) throws StringCalculatorException { + + if (intArray.length>2) { + throw new StringCalculatorException(""); + + } + } + + public int[] ChangeToInt(String[] strArray) { + String[] arrOfStr =strArray; + + int[] arrOfInt = new int[arrOfStr.length]; + + for (int i = 0; i < arrOfStr.length; i++) { + + arrOfInt[i] = Integer.parseInt(arrOfStr[i]); + } + + return arrOfInt; + } + + public void checkForNegatives(int[] intArray) throws StringCalculatorException { + int[] array = intArray; + for (int i=0; i=0) { + throw new StringCalculatorException(""); + } + } + + + } diff --git a/src/StringCalculatorException.java b/src/StringCalculatorException.java index da71147..7d840a9 100644 --- a/src/StringCalculatorException.java +++ b/src/StringCalculatorException.java @@ -1,4 +1,8 @@ public class StringCalculatorException extends Exception { + public StringCalculatorException(String string) { + System.out.println(string); + } + } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..0899865 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -1,12 +1,87 @@ import static org.junit.Assert.*; +import java.util.Arrays; + import org.junit.Test; +import junit.framework.Assert; + public class StringCalculatorTest { + + @Test - public void test() { - fail("Not yet implemented"); + public void TestStringCalculator_ChangeLinesToCommas() throws StringCalculatorException{ + //Arrange + StringCalculator calculator = new StringCalculator(); + //act + assertEquals("Fail","1,2,66",calculator.LinesToCommas("1\n2,66"));// + } + + @Test(expected = StringCalculatorException.class) + public void TestStringCalculator_CommasInaRowShouldThrowException() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + calculator.checkInvalidCommas("1,,2,3"); } + + @Test + public void TestStringCalculator_RemoveCommas() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + + String input=("1,2,3"); + String[] expected= {"1","2","3"}; + assertArrayEquals(expected,calculator.removeCommas(input)); + } + + + @Test(expected=StringCalculatorException.class) + public void TestStringCalculator_NotNumericThrowException() throws StringCalculatorException{ + //Arrange + StringCalculator calculator = new StringCalculator(); + //act + String[] input= {"a","2"}; + calculator.checkForNumeric(input); + } + + @Test + public void TestStringCalculator_ChangeToIntegerArray() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + + String[] input= {"1","2","3"}; + int[] expected= {1,2,3}; + + assertArrayEquals(expected,calculator.ChangeToInt(input)); + } + + @Test(expected = StringCalculatorException.class) + public void TestStringCalculator_NegativeThrowsException() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + int[] input= {-2,3,5}; + calculator.checkForNegatives(input); + } + + @Test (expected=StringCalculatorException.class) + public void TestStringCalculator_TooManyNumbersThrowException() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + int[] input= {1,55,5}; + calculator.checkNumberCount(input); + } + + + @Test + public void TestStringCalculator_NoNumbersReturnsZero() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + //act + assertEquals("Fail", 0, calculator.add("")); + } + @Test + public void TestStringCalculator_AddsGivenNumbers() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + //act + assertEquals("Fail", 20, calculator.add("14,6")); + } } +