From ed5c0f2e88abccf96f033860669e92e41a50c1c2 Mon Sep 17 00:00:00 2001 From: LENOVO Date: Sun, 30 Sep 2018 02:21:35 +0530 Subject: [PATCH 1/3] Assignment Finished --- bin/StringCalculator.class | Bin 405 -> 1428 bytes bin/StringCalculatorTest.class | Bin 505 -> 2259 bytes src/StringCalculator.java | 28 ++++++++++--- tests/StringCalculatorTest.java | 72 +++++++++++++++++++++++++++++--- 4 files changed, 89 insertions(+), 11 deletions(-) diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c43633d36f133837cd90ddd10fb14ac782..6cc6923ce3e6ab555f7cef3293f69638ed741fd5 100644 GIT binary patch literal 1428 zcmZuxU2_vv7=BK^c9Smja|@+dgIGyQLy(VvNo<8uG?=zZu}-TOZj(b<-0o&Jn*t8w zc)_n=9A~`p#v5-^urqk+j6cfwoZUFlwl{muc|V``eV=##`uq1c0IuN+4Kaa{^}x5? zoh8d@bQ~-2d<_XoA6t7?(Xrf};@b9O*$4zuRok_L1%Y@jzafxV@|rS(IMN1^NC^zr zZC9>#cDJSfz}j}Cz*yaDSk8v!+v?pHCW1$HTj>txtuV6HYzmmU`XGB`Rms?+9pidwI7NJPVt(mBG7sPs6Oh=%BEg8^|5$8@PaB z0lj7UZAqE3%^R4*MMl}PoQ_<3C@`6;{WoLZw=#qRiW)8noE)H6zzv+l)2n71(<+8_}Shb-f^KIga;4Hp|%zZlJ2j^5G$5|ITq;TA5kIbYGuA)LLW^f%d znNb%8`*}&BCe1iCt}zhfjSI-H##fQ2;F+A;hyGir&hV?$KmtSj8j%6d6MUsfm0?Qg zEL;XkQ}#o=IyW}b#n{RLPH*nx%uG@j6>PRS@dtYDS7W^oZ~v`aARB%|G9zR&6PHIhBdK-VxX oG~Cy?IznB59_EoAh}BIg*ybLi9ODq{ajO#p4@MVQ50O3i2T!s*ssI20 delta 141 zcmbQjJ(Zd3)W2Q(7#J8#7(^y=trcKo;L0n_O-d~)4lXHTWDuI{$Sg8>5p!fB69YF; z6%PX=kYokYyg-_ffscV7NU{UjW4#oGFRJY`yUVhrP&tR)nb~K4^MBsv&%b{E1HdM}i6O!;U$;$FZ|^Hw z)6o>$FdN*mV~8@O&Xu=HPE+)D?&$QKH*JQg&sANuA2Cd1R!CNae@ zTUB*lb1qJ~*-%b3&LCHfrlOrFrYh$CV$?oUEr!MFsI!X<8{IRmH@b#LvPvee=pDyy z&}z*nziaZ2tr~j8YMhxyS7%r{c63|4;3ul3(kUffH*7_i&>^X+VYYMU4(ZMb*NPHk z(x1;hh+`g$F)WbHu#N;i!Zor|S}lf!Omz?m&$75$iDMZnFd`vHj4|Y_+5LXB>U+N)naQcXk}{+bNi56meQssna`|-D&222q4HQ% zXPx10xD|a>p&fxvhH2ivCaC5JovhnR^M^yF<6lAXy5X2j{#+H8)Y5SOi;Lo((2kp+ zvUBSM&bf@1ZPL)_=v9J$(IviX#Gvs8zBR z-EhON@CY8$`V;b_L|w2C8PBLn%I^7Tk4xV~mqe)98*bAjX#A2!5uDKaRYd+K^GFOV PMtJdv&~dNaEwBFpa-PLJ delta 179 zcmcaC_>-CI)W2Q(7#J8#7^Ek1&17XMNi8m!c+o^g*e}0Cp)$2ZAv3oiCp9-UuOu~v zi$R1zjGaMrvOl9JmpFq2BLhoXVrI@{eMWJ{u*oc}CX7szEm^h1SsA#2Ch{;a0!daN z%?qR%8TckEaERKQ09hb$HX*?s48j|MEG7mnAjt)!85lT$>ba0~v%z&sf>lZZ03JFU AoB#j- diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..92d1a15 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,27 @@ +import java.util.ArrayList; +import java.util.List; public class StringCalculator { - public int add(String numbersStr) { - // Returns the sum of the numbers given in numbersStr - - // not yet implemented - return 0; + + public static int add(final String numbers) { + int returnValue = 0; + String[] numbersArray = numbers.split(",|n");; + List negativeNumbers = new ArrayList(); + for (String number : numbersArray) { + if (!number.trim().isEmpty()) { + int numberInt = Integer.parseInt(number.trim()); + if (numberInt < 0) { + negativeNumbers.add(numberInt); + } + returnValue += numberInt; + } + } + if (negativeNumbers.size() > 0) { + throw new RuntimeException("Negative Numbers not allowed: " + negativeNumbers.toString()); + } + return returnValue; } } + + + diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..b63d64f 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -1,12 +1,72 @@ import static org.junit.Assert.*; +import org.junit.Assert; import org.junit.Test; public class StringCalculatorTest { - - @Test - public void test() { - fail("Not yet implemented"); - } - + //when more than 2 numbers are used (Failure case) +// @Test(expected = RuntimeException.class) +// public final void whenMoreThanTwoNumbersAreUsedThenThrowAnException() { +// StringCalculator.add("1,2,3"); +// } + //when 2 numbers are input (Success case) + @Test + public final void whenTwoNumbersAreInputThenNoExceptionIsThrown() { + StringCalculator.add("1,2"); + Assert.assertTrue(true); + } + //when non number is input (Failure case) + @Test(expected = RuntimeException.class) + public final void whenNonNumberIsInputThenThrowAnException() { + StringCalculator.add("1,X"); + } + //when empty string is input (Success case) + @Test + public final void whenEmptyStringInputIsUsedThenReturnOutputIsZero() { + Assert.assertEquals(0, StringCalculator.add("")); + } + + //when one number is used as input then output is that same number + @Test + public final void whenOneNumberInputIsUsedThenReturnedOutputIsThatSameNumber() { + Assert.assertEquals(3, StringCalculator.add("3")); + } + //when two numbers are input then output is their sum + @Test + public final void whenTwoNumbersAreInputThenOutputIsTheirSum() { + Assert.assertEquals(3+6, StringCalculator.add("3,6")); + } + + //when more than 2 numbers are used (Failure case) + //when multiple number of inputs are used then output is their sum + @Test + public final void whenMultipleNumberOfInputsAreUsedThenOutputIsTheirSum() { + Assert.assertEquals(3+6+15+18+46+33, StringCalculator.add("3,6,15,18,46,33")); + } + + //when new line is used between input numbers then output is their sum + @Test + public final void whenNewLineIsUsedBetweenInputNumbersThenOutputIsTheirSum() { + Assert.assertEquals(3+6+15, StringCalculator.add("3,6n15")); + } + + //when negative number is in input (Failure case) + @Test(expected = RuntimeException.class) + public final void whenNegativeNumberIsInInputThenThrowRuntimeException() { + StringCalculator.add("3,-6,15,18,46,33"); + } + //when multiple negative numbers are in input (Failure case) + @Test + public final void whenMultipleNegativeNumbersAreInInputThenThrowRuntimeException() { + RuntimeException exception = null; + try { + StringCalculator.add("3,-6,15,-18,46,33"); + } catch (RuntimeException e) { + exception = e; + } + Assert.assertNotNull(exception); + Assert.assertEquals("Negatives not allowed: [-6, -18]", exception.getMessage()); + } } + + From 804bce713a41ed665ef2a4efdb32e29c24699328 Mon Sep 17 00:00:00 2001 From: LENOVO Date: Sun, 30 Sep 2018 03:05:46 +0530 Subject: [PATCH 2/3] Assignment Finished with changes --- bin/StringCalculator.class | Bin 1428 -> 1432 bytes bin/StringCalculatorTest.class | Bin 2259 -> 2254 bytes src/StringCalculator.java | 2 +- tests/StringCalculatorTest.java | 21 ++++++++++----------- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index 6cc6923ce3e6ab555f7cef3293f69638ed741fd5..d24395ccf2c5c280c26d870df581304c9a933dce 100644 GIT binary patch delta 34 pcmbQjJ%f8g6qBfuUut?{NoHB9f?sKFQfg7LLSj+s=4>WSW&p^Q3t0dF delta 30 lcmbQiJ%xKi6qBHwUut?{NoHB9f?sKFQfg80=6ohiW&oRk3H<;7 diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index aafd21ef91e79acd64d130a6740390779d324c04..65aa696fd1d9c5addb25fa7ad74996c6d5fdaf6d 100644 GIT binary patch delta 523 zcmYk2%TE(g6vn@E?=Uk=TS%vEOsJ1b;zH#XI>QuNst8^LD%E;#_q<8Sk<3D zeH`yRQ;nTiZEf$W#^&bsUc7c)t(+PwHhAP=8nX^&%oFR9oI~BbwyxLt&AJ0S=BfVBY)wn^jQ8Z$qgY{xKclufstqbh5AUrscUS$+q5PS5r=QS)#2eZ<`+<_|f6 zfXiv7AvqUt5f0Z8ren;1Ml-j#J#`+z6m_!c?RMS T%Y_5VIP6bc1gjQ#Cb0Au%xPY8 delta 534 zcmY*VOHUI~6#mY=ow>s>NM~9f0hMXOLg9jp(>5&zDHeQDP(TGSE=X*M34w(~;>xv4 za~A#roo6 zzjR`+avyz{zQI4}UEUE7H+eX8?$!_@p5~6 zYybJn_KSFTZ$IAJ+1Wd2Kbwx9NMtO&r#@O(Hdx~A+=#x1`}`(%$z2U_8Ec;9;-yy4f>*fV*T{<6SI7cg7E~*u_dm)C!(B zThLkFMezbNg3b!wkeoT3LK6|@F^q-mMl4$3{J$GYzU%=?k`##Rp-c*ogfBq-5xFv; Pf{}cZqC~YWoWjOms?Jzw diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 92d1a15..4f0f4e0 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -17,7 +17,7 @@ public static int add(final String numbers) { } } if (negativeNumbers.size() > 0) { - throw new RuntimeException("Negative Numbers not allowed: " + negativeNumbers.toString()); + throw new RuntimeException("Negative Numbers are not allowed: " + negativeNumbers.toString()); } return returnValue; } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index b63d64f..0d01732 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -7,18 +7,18 @@ public class StringCalculatorTest { //when more than 2 numbers are used (Failure case) // @Test(expected = RuntimeException.class) // public final void whenMoreThanTwoNumbersAreUsedThenThrowAnException() { -// StringCalculator.add("1,2,3"); +// StringCalculator.add("9,8,7"); // } //when 2 numbers are input (Success case) @Test public final void whenTwoNumbersAreInputThenNoExceptionIsThrown() { - StringCalculator.add("1,2"); + StringCalculator.add("4,5"); Assert.assertTrue(true); } //when non number is input (Failure case) @Test(expected = RuntimeException.class) public final void whenNonNumberIsInputThenThrowAnException() { - StringCalculator.add("1,X"); + StringCalculator.add("6,a"); } //when empty string is input (Success case) @Test @@ -29,43 +29,42 @@ public final void whenEmptyStringInputIsUsedThenReturnOutputIsZero() { //when one number is used as input then output is that same number @Test public final void whenOneNumberInputIsUsedThenReturnedOutputIsThatSameNumber() { - Assert.assertEquals(3, StringCalculator.add("3")); + Assert.assertEquals(1, StringCalculator.add("1")); } //when two numbers are input then output is their sum @Test public final void whenTwoNumbersAreInputThenOutputIsTheirSum() { - Assert.assertEquals(3+6, StringCalculator.add("3,6")); + Assert.assertEquals(5+2, StringCalculator.add("5,2")); } //when more than 2 numbers are used (Failure case) //when multiple number of inputs are used then output is their sum @Test public final void whenMultipleNumberOfInputsAreUsedThenOutputIsTheirSum() { - Assert.assertEquals(3+6+15+18+46+33, StringCalculator.add("3,6,15,18,46,33")); + Assert.assertEquals(10+20+3+40+50, StringCalculator.add("10,20,3,40,50")); } //when new line is used between input numbers then output is their sum @Test public final void whenNewLineIsUsedBetweenInputNumbersThenOutputIsTheirSum() { - Assert.assertEquals(3+6+15, StringCalculator.add("3,6n15")); + Assert.assertEquals(4+1+22, StringCalculator.add("4,1n22")); } - //when negative number is in input (Failure case) @Test(expected = RuntimeException.class) public final void whenNegativeNumberIsInInputThenThrowRuntimeException() { - StringCalculator.add("3,-6,15,18,46,33"); + StringCalculator.add("10,-20,3,40,50"); } //when multiple negative numbers are in input (Failure case) @Test public final void whenMultipleNegativeNumbersAreInInputThenThrowRuntimeException() { RuntimeException exception = null; try { - StringCalculator.add("3,-6,15,-18,46,33"); + StringCalculator.add("10,-20,3,-40,50"); } catch (RuntimeException e) { exception = e; } Assert.assertNotNull(exception); - Assert.assertEquals("Negatives not allowed: [-6, -18]", exception.getMessage()); + Assert.assertEquals("Negative Numbers are not allowed: [-20, -40]", exception.getMessage()); } } From 71469b164e3bd4d7d3164ec6b88f85ad0a40b698 Mon Sep 17 00:00:00 2001 From: LENOVO Date: Sun, 30 Sep 2018 03:12:59 +0530 Subject: [PATCH 3/3] Assignment Finished --- bin/StringCalculatorTest.class | Bin 2254 -> 2255 bytes tests/StringCalculatorTest.java | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index 65aa696fd1d9c5addb25fa7ad74996c6d5fdaf6d..cdc59ec4fea98284fb54b1a14abcbae82026c969 100644 GIT binary patch delta 18 ZcmX>ncwTS=D;N*%1jzsZ delta 17 YcmX>vcusHwD