From dcaf3d683dd85135aaaf1b59b08a442d6c759614 Mon Sep 17 00:00:00 2001 From: Nelson-Lemercier Date: Mon, 1 Oct 2018 21:33:46 +0300 Subject: [PATCH] Assignment finished --- bin/StringCalculator.class | Bin 405 -> 2721 bytes bin/StringCalculatorException.class | Bin 303 -> 470 bytes bin/StringCalculatorTest.class | Bin 505 -> 4071 bytes src/StringCalculator.java | 184 +++++++++++++++++++++++++++- src/StringCalculatorException.java | 4 + tests/StringCalculatorTest.java | 168 ++++++++++++++++++++++++- 6 files changed, 349 insertions(+), 7 deletions(-) diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c43633d36f133837cd90ddd10fb14ac782..b9341074e41e2535939e1b58924871cb00d6319f 100644 GIT binary patch literal 2721 zcmb7G-)|IU6g{&uv$Ne!1-6}*%C=Ax*xfEviY!uS1u8!ZZ4p|w{3zJ%b}S3aZrz== zglM9qA^PBhF}|QCkf2SB4;qtVB;vb&f&YT#{gc%goS{H^&?&>SD5b4_#-v*s%}nN94UxfOHj__h zN;!G18V&Dit}J^iksM&;%+!w23)H0QslJ%{I1(v0nL(7O%Sn<-FC#OL&>i)2^qi z8g{G&`O<0{l54w-9TK*V$9=iP8n=-^CnYFXZfG!L0|W5^vG268OS~N-8!DFE(W{xl z=xotHNFBokrFfYXMbuTov2G=|T*?I_ICGxWS>;V@-axGG*Q-{sdrh0NV@OCj{rD;dTi z9F;~owzin7iN(`$=7fy_oD`Vu&8Nj^ip5vEi}Rq3Aq!lgwc$&jWN-~EYIX~UKkf}$pQm!SSrdu!E>kGl8tfI zZ8Mot*`+OvCfKayNU>7Ny2o-dJkHAQ-X&qsLE&`{bbxmgL9Pfw-K8WPL|((RcJZ$9 zZYCb0{s(oSf!Cli>Ue=`+jqb^tmhMPBEqnZTpN69k}%pBlSz^}y;tuGqzv;HH20W^ z&IN2V9-u)Mn^ML+rexKA7j?QkrPY&h53RE3;DJ_C?_R*R-l%NvQ`_b}>{z)?KSTWb z64AB=#Kbu)KFO5f7~c_x4q$a}>R~#@M9Q2CYECeEfAJ5;T+(Md+W>f-hz?VoesVabNlN8PfZfaXp+!Dz5D*(@||_RSg|ImYxdDqdC!P zIU&_9j?p%eGUh^>6LL(7p;SpezQ6c*dE4|T!siGz%}E(!^PH#UB3pf&O>%{eF+tLk ztkD$3b1B*+MY_mAzOGJ=`&esIjn||~qnAYwFzTohNN4ObN+5$LhuM50)drlXO0|u9 z(k2$KZK_ZqC0_@OMZ&4d=c`Bgo?wftBY&y!=0}Ng%8-A`jx%`gc!Wb2IwMCO;tgsz zkNP=XOD^E}7%!*pF8{@4JE(lgK+a-Ex>2g>T{KHi|<%dq^_z6|5H&Bsk|ot304O_ zvS#2f82GE_4$OZGOz_NmnQtr~yQBAZ{)F?r(JxW|2u&9r;_|Q2rD`{`>wpJz{+cPj zp{(-^^zE|LPK}dOB+I#Waed-*fBCNYiNcsGO#%3r=)T*Ff(xSFt9SP zP0UD{{E$&`@)~BHdIkn221bw|kYooMpbw-Ofi$bub_R}(Kn4>77mx%=gV@|ak_Vyy yNb>@Dj0}7X{6G?<*$hZCfz&at0g*^L*}*!+AhJ-MAQk)q41z#Sj0{3x4Z;Am;u9zU diff --git a/bin/StringCalculatorException.class b/bin/StringCalculatorException.class index 4bd989452e88efe466640937d540edeb3220a693..096b1469f1a906b1cf2f3f3c9c7ac322701f9fc4 100644 GIT binary patch delta 249 zcmZ3_bd8zo)W2Q(7#J8#8002$MbvXLa5M0-Gw?7n2xKLeCFL)_v1s985MvN$WMD43&5}|qfF)#y379eD0;9+26kOcC0AQ~8?7^HzbMg|!m$;2QF E0Pznr%m4rY delta 80 zcmcb{yq<~c)W2Q(7#J8#7z8GAMNIrT*-D>*5eOL=Shcn@aBKuJm>9T#BpX7m*yF=)My;a8^LaB@P_P&KBq)aU zGw>!qIxMqB0SM{uq-WmtT=^z^yD-HkL}pF$q#w)azMM znGND94 z&q6n570ihc?HuG1F~w|r)>RM7xX7syEzESCAw8mxmiN{#<8?0PycW?$MYuN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?NOA#b1_n+BRv?QT$l`&h0@A!d9wUP! JkYr+z0sstWApigX diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..618ac83 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,183 @@ +import java.util.StringTokenizer; -public class StringCalculator { - public int add(String numbersStr) { - // Returns the sum of the numbers given in numbersStr +public class StringCalculator{ + + public int int1; + public int int2; + + public int add(String numbersStr) throws StringCalculatorException{ + + int int1 = 0; + int int2 = 0; + int result = -1; + String[] token = new String[2]; + + if(countLines(numbersStr) > 1) { + + result = addlines(numbersStr); + + } + + else { + + if(countTokens(numbersStr) == 0) { + + + result = 0; + + } + + else if(countTokens(numbersStr) == 1) { + + + token = getToken(numbersStr); + int1 = getNumber(token[0]); + result = int1; + + } + + + else if(countTokens(numbersStr) == 2) { + + token = getToken(numbersStr); + int1 = getNumber(token[0]); + int2 = getNumber(token[1]); + result = sum(int1, int2); + + } + + else{ + + result = addMoreThanTwoNumbers(numbersStr); + + } + + } + + return result; + + } + + public int addMoreThanTwoNumbers(String s) throws StringCalculatorException{ + + int numTokens = countTokens(s); + + String[] token = getToken(s); + + int number = 0; + + int sum = 0; + + for(int i = 0; i < numTokens; i++) { + + number = getNumber(token[i]); + + sum += number; + + } + + return sum; + + } + + public int addlines(String s) throws StringCalculatorException{ + + String[] line = getLine(s); + int number = 0; + int sum = 0; + int numToken = 0; + + for(int i = 0; i < line.length; i++) { + + numToken = countTokens(line[i]); + + String[] token = new String[numToken]; + + token = getToken(line[i]); + + for(int j = 0; j < numToken; j++) { + + number = getNumber(token[j]); + sum += number; + + } + + } + + return sum; + + } + + public int countTokens(String s) { + + StringTokenizer st = new StringTokenizer(s, ","); + + int tokenNum = 0; + + while(st.hasMoreTokens()) { + + tokenNum ++; + st.nextToken(); + + } + + return tokenNum; + + } + + public int countLines(String s) { + + StringTokenizer st = new StringTokenizer(s, "\n"); + + int lineNum = 0; + + while(st.hasMoreTokens()) { + + lineNum ++; + st.nextToken(); + + } + + return lineNum; + + } + + public String[] getLine(String s) { + + String[] st = s.split("\n"); + + return st; + + } + + public String[] getToken(String s) { + + String[] st = s.split(","); + + return st; + } + + public int getNumber(String s) + throws StringCalculatorException + { + + int number = 0; + number = Integer.parseInt(s); + if(number < 0) + throw new StringCalculatorException(); + else + { + return number; + } + + + + } + + public int sum(int int1, int int2) { + + return int1 + int2; - // not yet implemented - return 0; } + } diff --git a/src/StringCalculatorException.java b/src/StringCalculatorException.java index da71147..8e6b853 100644 --- a/src/StringCalculatorException.java +++ b/src/StringCalculatorException.java @@ -1,4 +1,8 @@ public class StringCalculatorException extends Exception { + public StringCalculatorException(){ + System.out.println("Invalid number"); + } + } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..3221eff 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -3,10 +3,174 @@ import org.junit.Test; public class StringCalculatorTest { + + //Test countToken method + //------------------------------------------------------------------- + + @Test + public void countToken_0Number() { + StringCalculator sc = new StringCalculator(); + assertEquals(0, sc.countTokens("")); + } + + @Test + public void countToken_1Number() { + StringCalculator sc = new StringCalculator(); + assertEquals(1, sc.countTokens("2,")); + } + + @Test + public void countToken_2Number() { + StringCalculator sc = new StringCalculator(); + assertEquals(2, sc.countTokens("2, 3")); + } + + @Test + public void countToken_3Number() { + StringCalculator sc = new StringCalculator(); + assertEquals(3, sc.countTokens("2, 3, 4")); + } + + //Test getToken method + //------------------------------------------------------------------- + + @Test + public void getToken_1Number() { + StringCalculator sc = new StringCalculator(); + String st[] = new String[1]; + st = sc.getToken("2,"); + assertEquals("2", st[0]); + } + + @Test + public void getToken_2Number() { + StringCalculator sc = new StringCalculator(); + String st[] = new String[2]; + st = sc.getToken("2,3"); + assertEquals("3", st[1]); + } + + //Test getNumber method + //------------------------------------------------------------------- + + @Test + public void getNumber_1() throws StringCalculatorException { + StringCalculator sc = new StringCalculator(); + assertEquals(1, sc.getNumber("1")); + } + + //Test getNumber method + //------------------------------------------------------------------- @Test - public void test() { - fail("Not yet implemented"); + public void sum_1_2() { + StringCalculator sc = new StringCalculator(); + assertEquals(3, sc.sum(1, 2)); } + + //Test add method + //------------------------------------------------------------------- + @Test + public void add_None() throws StringCalculatorException { + StringCalculator sc = new StringCalculator(); + assertEquals(0, sc.add("")); + } + + @Test + public void add_1() throws StringCalculatorException { + StringCalculator sc = new StringCalculator(); + assertEquals(3, sc.add("3")); + } + + @Test + public void add_1_2() throws StringCalculatorException { + StringCalculator sc = new StringCalculator(); + assertEquals(3, sc.add("1,2")); + } + + //Test addMoreThanTwoNumbers method + //------------------------------------------------------------------- + + @Test + public void add_1_2_3_4() throws StringCalculatorException { + StringCalculator sc = new StringCalculator(); + assertEquals(10, sc.addMoreThanTwoNumbers("1,2,3,4")); + } + + @Test + public void add_1_2_3_4_5_6_7_8() throws StringCalculatorException { + StringCalculator sc = new StringCalculator(); + assertEquals(36, sc.addMoreThanTwoNumbers("1,2,3,4,5,6,7,8")); + } + + //Test lineNum method + //------------------------------------------------------------------- + + @Test + public void countLine_1() { + StringCalculator sc = new StringCalculator(); + assertEquals(1, sc.countLines("1,2,3,4")); + } + + @Test + public void countLine_2() { + StringCalculator sc = new StringCalculator(); + assertEquals(2, sc.countLines("1,2\n3,4")); + } + + @Test + public void countLine_3() { + StringCalculator sc = new StringCalculator(); + assertEquals(3, sc.countLines("1\n2\n3,4")); + } + + //Test getLine method + //------------------------------------------------------------------- + + @Test + public void getLine_1() { + StringCalculator sc = new StringCalculator(); + String[] line = sc.getLine("1,2\n3"); + assertEquals("1,2", line[0]); + } + + @Test + public void getLine_2() { + StringCalculator sc = new StringCalculator(); + String[] line = sc.getLine("1,2\n3"); + assertEquals("3", line[1]); + } + + @Test + public void getLine_3() { + StringCalculator sc = new StringCalculator(); + String[] line = sc.getLine("1,2\n3,4,5\n6,7,8"); + assertEquals("6,7,8", line[2]); + } + + //Test addLine method + //------------------------------------------------------------------- + + @Test + public void addLine_2_lines() throws StringCalculatorException { + StringCalculator sc = new StringCalculator(); + assertEquals(6, sc.addlines("1,2\n3")); + } + + @Test + public void addLine_3_lines() throws StringCalculatorException { + StringCalculator sc = new StringCalculator(); + assertEquals(21, sc.addlines("1,2\n3,4\n5,6")); + } + + //Test exceptions + //------------------------------------------------------------------- + + @Test(expected = StringCalculatorException.class) + public void add_exception_negative_input() throws StringCalculatorException { + StringCalculator sc = new StringCalculator(); + sc.addlines("-1"); + } + }