From 2624d58c723052ead340b4115784f919dec9b5e2 Mon Sep 17 00:00:00 2001 From: Laiq Date: Sat, 29 Sep 2018 23:28:16 +0300 Subject: [PATCH] assignment finished --- bin/StringCalculator.class | Bin 405 -> 1709 bytes bin/StringCalculatorTest.class | Bin 505 -> 2445 bytes src/StringCalculator.java | 72 ++++++++++++++++++++++++++++++-- tests/StringCalculatorTest.java | 66 +++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 3 deletions(-) diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c43633d36f133837cd90ddd10fb14ac782..c9211840c0492e33ef3ad2a6bfe52cb26defeb53 100644 GIT binary patch literal 1709 zcmZ`(+j1L45Iv(^S+cb9MMyRdHUfu>?&jhI62$>WIAD-%5(kkuP{ml^eh4p03R3xi{0+PU31@aylI=L9s-2$cp3|pK_vp#fKOO^E#!d(UfvIiR zvD%HYv}-+Ex*aD3ou@bDExBaNc4KL4_oi8M1;)-^$Fu^Hd0yFW#Ol9l|hK@PS2^C7cKP)l779T2Ub-4@H%Hs<2wY# zXz>N{8{%$>tKb^UK0x?)8UaKGLCTKOHv9mtG42E=FiFo8ZJoPm4`!d>L7rr_m0TY$ z{fwZtpQSaosh!q;Lu|f~)QjJ(+|fl+FFd~Y?C<$~Mr&TKMTBVwi8+RGqDA?gre~Jc zt6bB}t9UH?83sI<3k&oM-b*8e*SJn&j)g7~H^JXYp6PglHz%O|i>UYy(GcDg4x6*c**8@^F`)Vp4TT{DVsY7QQ)@_nU#dsa$-k rk4twkn~PuZjEP*l>KU9keI&S=Z=iHQ=+ux>>afrY486+W0IvNH=y5j! delta 104 zcmZ3>JC&L1)W2Q(7#J8#7(^y=ZJ6xNs5m*9S&xxv@+4*#cSZ(wpp+Q{BLfqVW@26g nA{n@VBpXnS0VvB2B*h@I3_J|HKpsei0D~YQa%o7Fx%P0$< diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index d44b83f48579a95c814a0008fdfcca590443d2de..1f94b50b0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.java index 487916b..2e77640 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,75 @@ public class StringCalculator { - public int add(String numbersStr) { + public int add(String numbersStr) throws StringCalculatorException { // Returns the sum of the numbers given in numbersStr - + int sum = 0; + if (numbersStr.isEmpty()) { + return 0; + } else { + int numericArray[] = getIntArray(numbersStr); + for (int i = 0; i < numericArray.length; i++) { + sum = sum + numericArray[i]; + } + return sum; + } // not yet implemented - return 0; + + } + + public int getLength(String str) { + return str.length(); + + } + + public boolean handleLength(String str) { + if (str.length() < 20) { + return true; + } else { + return false; + } + + } + + public int[] getIntArray(String str) { + int arr[] = new int[str.length()]; + int index = 0; + // System.out.println(str.length() + " "); + // if (isValid(str)) { + for (int i = 0; i < str.length(); i++) { + // if ((str.charAt(i)+str.charAt(i+1)+"") != "\n" || + // (str.charAt(i) + // + "") != ",") { + if (!(str.charAt(i) + "").equals(",")) { + if (!(str.charAt(i) + "").equals("\n")) { + // System.out.println(str.charAt(i) + ""); + arr[index++] = Integer.parseInt(str.charAt(i) + ""); + + } + // System.out.println(str.length() + " " + str.charAt(i) + + // ""); + } + } + // } + return arr; + + } + + public boolean isValid(String str) { + + return str.matches("(.*),\n(.*)") || str.matches("(.*)-(.*)") || str.matches("(.*)[a-z](.*)"); + // return str.matches(",\n") || str.matches("-"); + // boolean isvalid = true; + // System.out.println(str.length() + ""); + /* + * if (str.length() >= 2) { for (int i = 0; i < str.length() - 1; i++) { + * if (((char)str.charAt(i) + (char)str.charAt(i + 1) + + * "").equals(",\n")) { //System.out.println(str.charAt(i) + + * str.charAt(i + 1) + ""); isvalid = false; break; } + * + * } } + */ + + // return isvalid; + } } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..48c29de 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -9,4 +9,70 @@ public void test() { fail("Not yet implemented"); } + @Test + public void simpleAddition() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("output is correct", 12, st1.add("1236")); + } + + @Test + public void additionWithComma() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("output is correct", 12, st1.add("1,2,36")); + } + + @Test + public void additionWithNewLine() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("output is correct", 12, st1.add("1,2\n36")); + } + + @Test + public void checkInValidAgainstnewlineWithcomma() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + boolean res = st1.isValid("45,\n67"); + assertTrue("String is Invalid", res); + } + + @Test + public void checkInValidAgainstNegativeNumber() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + boolean res = st1.isValid("45-67"); + assertTrue("String is Invalid", res); + } + + @Test + public void checkInValidAgainstAnyInvalidInput() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + boolean res = st1.isValid("457bcb"); + assertTrue("String is Invalid", res); + } + + @Test + public void isLenghtCorrect() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("Length is correct", 5, st1.getLength("56556")); + } + @Test + public void testLenghtLimit() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + boolean range=st1.handleLength("0463563465456760000"); + assertTrue("Length is within range", range); + } + + @Test + public void testEmptyResult() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("Empty output", 0, st1.add("")); + } + }