From eabf76ec88a08667c9430887b1f43b4f528f2495 Mon Sep 17 00:00:00 2001 From: janneannunen Date: Mon, 1 Oct 2018 11:38:50 +0300 Subject: [PATCH] Assignment finnnnished --- bin/StringCalculator.class | Bin 405 -> 2749 bytes bin/StringCalculatorTest.class | Bin 505 -> 2031 bytes src/StringCalculator.java | 107 +++++++++++++++++++++++++++++++- tests/StringCalculatorTest.java | 63 ++++++++++++++++++- 4 files changed, 166 insertions(+), 4 deletions(-) diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c43633d36f133837cd90ddd10fb14ac782..09277388ae04671d5d951f5d61b336a704cd48a9 100644 GIT binary patch literal 2749 zcmb_dTUQfT6#h<<$xK3!>nIUXq_id)MN!jAG+wCMXt*feDxwU@KnBBvCKGGl`zQ3F zYx!g!cpDULy$fF5 zao47;YN=VZyjopHgs1D)Gi#!1xoZ=1E9-X26NsL7T*u1`gj4Bz0+H!j*^VQOSOOZN z0zE~?wP%|fD|Y>kwNkYO`ir%aRlR4`9k~wPSJ}}Z zv{c;A5U3J})$Ovs5zuU(k$A`i)#lTtfS~N|1)C74#>?norcJBfuz41MR=6-KR(ZO% zv0;&`_kE2nv%MM;|5^gqms8@3vls&&cpu z*>IgA2yVOOg3Qj^$a>Ht^_-Lhxqrd~f5DTUHog0fSD7`yOCsVVLkSC2t5G469#qQ! zYaGhN4LoA47c=oC$>(tbPq0jo#%9%__#-?0^b43vFYeaD9{FIC0wSxjU3E5Cl7Wul ziu5`?)f!FZt#EXdXz@a=Sufew9C>?9AF(|cPRPQsg77ZU7t$x6Hhdrj2@fGY-1ol@&a0TYvd(* zGvRF{-(VygF(X^yEyjcu!WfBwSwbqq0Fefne;;>4ywOQs;9-S+#?Kp|69J!LoVX!I ze9l#f_(^<$Bg{L@-xSj1b(Fc|2>pd_9fP8m@y9x_e#Y|%BYQq#9cq)@tH2KtWRx(6 zNob5D#}%=o{--TsJ;(&ax^X-pX5fS(Ch5c=0y;#U=)h))Fp^C-*#xHLM*Y z5}7x+kkw3WYlklfRnbrI+X=2tGVduCVG@UMnhu>+-12_j7Ps*hx3PemjuBj;k}$tF zF-@l;%;1+$3rZ;7VN8}#cInr<`HLaO%O;)O#s39zl7Chyx|-E8VN=`2H&fA!sYRBz zQJm6~QK`*H>hcjY4Kuc!jhpeEN+-!5I7g*ZRGK4$^Hh3)y^trvi~N6liKmyT;|dC7 zSj08OXVYiZQa{^LUu>zr-co-tP=A9z2;TBgCy$`qf*5G$>EN~Sg zrteq0?N{-GUw8hIUw4CgxBcSDfAs4H{kloUMKYM7U$gXUjtu9i<`#9_rh)}>zsrdG z|H`lXs$2eFzaDh(D@T9jK%D<*L}dEEZMmj}w)z*Rv`muSwKNq?M&D-jaMlQCW3Mq_ z8p)^`n`D12XN@0Y!i<^vtM`AHMtjEOi3%*zu}5@liEcdRj9KQ5u&^I1IDj&zkIh@Z z%DJ+}KB^!KhaFR)w@;OOt3Jn8x6QY@ZMM~IgP3v5lFHEXIA6}U=Re#GcpK))e8dv&QuGe diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index d44b83f48579a95c814a0008fdfcca590443d2de..27be85db6f2d7f45a599a296f9a2cab7298faa68 100644 GIT binary patch literal 2031 zcmb7@>vG#v6ouEx&9bACMs>)orKcqnyG`TTxfa)@scYH>lMGGBP%ZCNL(DJ`6nDX_U=ot=%*O z*E_QOAb|;i%V*{*GuJeoM(*I`%&G+fQx9y%4ju}OXR^lv6J@tX-m;Wn+4`F)qZfI$zt}oh7Wk*eK(M%47W=^jBN@MHxsx)w?4wI?^|A= z<3p?n=w>*o{ME$U;kOe)oN4VwaE`A~0L z$4;o{gs)CR4WBSTJw3hv!x61^mGoF?L&K-+<_oKl*shMpN-Pz;cB3rvH9X<7avV$e zvSs@!2r5~vdRLLp6|!w=;N@p27m89>Nk{6R>!>RALa z>iOADttnHo>+~#k+R|{yI@noFY4}QD<EkzTY*!#Mm6VZLem1W~=J@Pc5j# zUQ?-w8z5cyZW?>6PW}@d=h!m0F?stfwBOXTGS3k%a}v|+>(K_bmpBr5kE`RDVSj?7 z%P`{oSJ*1a6P{C63x6X0@-55yjxpcCV#&Ccy!z|!xZZa$r~o9P(PxJ0^VC+3GCChS zYV`OBx9By)y&oe(MuF`AP}F0Jr9Q>&4pvJ>E{*&^j#+YCp~oB+Nz&(7>Twu7j_Dr9 z`sf@R9c-11`)L#hax9bM8ab|$Lk`VR8lB@|2aifdIgQFdjump;B1eWC*`Ya}j?VF{ zgT0dRMH&YKIo8N=j~oSZtPjoc<>(wQLXM*}jt6pVljA-)isX1OG{@JYbG!^WzD?u% XfgF#>@t7P>$Wa-Z;|JE&7=HX0nZ{F+ delta 208 zcmaFQ|C5>P)W2Q(7#J8#7^Ek1&17XMNi8m!cuPNmgF%RqLD(<9M4>XZL?JV`ASX39 zHLoN!g^NLiL5!V2l#xLozbIWlt28gOMBlNvIJKyRhe4b{f{}qGEip5PkwH|$Co8cm zQ9ma!FI_*lq$o2l-C8qj@?K^WMyAQ1m|X&y82Et}voQco0@CbYM=%0;Krtb~9Sp)7 mfh;BlE+EMTq!}1E8CZcVZXk;Xq6$d!0(p!Kl0cG)K?(r-QYbP2 diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..2b45972 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,112 @@ +/*Implement a simple String calculator with a method int Add(string numbers) +1. The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will +return 0) for example “” or “1” or “1,2” +2. Add method to handle an unknown amount of numbers +3. Add method to handle new lines between numbers (instead of commas). +1. the following input is ok: “1\n2,3” (will equal 6) +2. the following input is NOT ok: “1,\n” +4. Calling Add with a negative numbers or invalid input (not numbers) will throw an +StringCalculatorException*/ public class StringCalculator { - public int add(String numbersStr) { + + + public int add(String numbersStr) throws StringCalculatorException { // Returns the sum of the numbers given in numbersStr + String redString = numbersStr; + boolean empty = redString.isEmpty(); - // not yet implemented + if(empty) { + return 0; + } + + else if(!empty) { + int luku = Character.getNumericValue(redString.charAt(0)); + return luku; + } + return 0; + + } + public int emptyString(String numbersStr0) throws StringCalculatorException { + String redString0 = numbersStr0; + boolean empty0 = redString0.isEmpty(); + if(empty0) { return 0; + } + return -1; + } + public int oneString(String numbersStr1) throws StringCalculatorException { + String redString1 = numbersStr1; + int luku = Character.getNumericValue(redString1.charAt(0)); + return luku; + + } + public int numberOne(String numbersStr1) throws StringCalculatorException { + String redString1 = numbersStr1; + int lenght = redString1.length(); + boolean hadComma = redString1.contains(","); + if(!hadComma && (lenght >=1)) { + int luku = Integer.parseInt(redString1); + return luku; + } + else return -1; + } + + public int twoNumbers(String numbersStr1) throws StringCalculatorException { + String redString1 = numbersStr1; + int lenght = redString1.length(); + boolean hadComma = redString1.contains(","); + if(hadComma && (lenght >=1)) { + int endIndex = redString1.indexOf(","); + String luku1 = redString1.substring(0, endIndex); + String luku2 = redString1.substring(endIndex+1); + int luku_1 = Integer.parseInt(luku1); + int luku_2 = Integer.parseInt(luku2); + int luku = luku_1 + luku_2; + + return luku; + } + else return -1; + } + + public int twoNumbersWithN(String numbersStr1) throws StringCalculatorException { + String redString1 = numbersStr1; + int lenght = redString1.length(); + boolean hadSlashN = redString1.contains("\n"); + if(hadSlashN && (lenght >=1)) { + int endIndex = redString1.indexOf("\n"); + String luku1 = redString1.substring(0, endIndex); + String luku2 = redString1.substring(endIndex+1); + int luku_1 = Integer.parseInt(luku1); + int luku_2 = Integer.parseInt(luku2); + int luku = luku_1 + luku_2; + + return luku; + } + else return -1; + } + + public int numbers(String numbersStr1) throws StringCalculatorException { + String redString1 = numbersStr1; + boolean empty = redString1.isEmpty(); + if(empty) { + return 0; + } + boolean hadCommaSlash = redString1.contains(",\n"); + if(hadCommaSlash) { + return -1; + } + String delims = "[,\n]"; + String[] numbers = redString1.split(delims); + int lukuja = numbers.length; + int summa = 0; + for (int i = 0; i < lukuja ; i++ ) { + summa = summa+ Integer.parseInt(numbers[i]); + } + return summa; + + + } + } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..5ad5b14 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -5,8 +5,67 @@ public class StringCalculatorTest { @Test - public void test() { - fail("Not yet implemented"); + public void test1Empty() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.emptyString(""); + // Assert + assertEquals("Not a empty string", 0, result); + } + @Test + public void test2OneChar() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.oneString("2"); + // Assert + assertEquals("Empty or more than one char", 2, result); + + } + + @Test + public void test3() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.numberOne("12"); + // Assert + assertEquals("Empty or not a number", 12, result); } + @Test + public void test4() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.twoNumbers("12,12"); + // Assert + assertEquals("two numbers miscalculated", 24, result); + } + + + @Test + public void test5() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.numbers("12\n12,12,12,12\n12"); + // Assert + assertEquals("calculation error with 6 numbers incl \n ", 72, result); + } + + @Test + public void test6() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.numbers("8,\n"); + // Assert + assertEquals("Sign for invalid input (-1) expected", -1, result); + } + + @Test + public void test7() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.numbers("8,5\n"); + // Assert + assertEquals("Counting 2 digits incl N", 13, result); + } + }