diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c..d2f6bcf 100644 Binary files a/bin/StringCalculator.class and b/bin/StringCalculator.class differ diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index d44b83f..a7384b5 100644 Binary files a/bin/StringCalculatorTest.class and b/bin/StringCalculatorTest.class differ diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..723d208 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,147 @@ +/*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 x = Character.getNumericValue(element.charAt(2)); + + 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; + + + } + public int numbersNochar(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++ ) { + char[] c = numbers[i].toCharArray(); + int merkkeja = c.length; + if(Character.isAlphabetic(c[merkkeja-1])) { + //do nothing + + }else if (Character.isDigit(c[merkkeja-1])) { + summa = summa+ Integer.parseInt(numbers[i]); + } + } + return summa; + + + } + } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..5296beb 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -5,8 +5,87 @@ public class StringCalculatorTest { @Test - public void test() { - fail("Not yet implemented"); + public void testEmpty() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.emptyString(""); + // Assert + assertEquals("No empty string", 0, result); + } + @Test + public void testOneChar() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.oneString("2"); + // Assert + assertEquals("No empty string", 2, result); + + } + @Test + public void test2() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.numberOne("12"); + // Assert + assertEquals("No empty string", 12, result); } + @Test + public void test3() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.twoNumbers("12,12"); + // Assert + assertEquals("No empty string", 24, result); + } + @Test + public void test31() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.twoNumbers("12,"); + // Assert + assertEquals("No empty string", 12, result); + } + @Test + public void test4() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.twoNumbersWithN("12\n12"); + // Assert + assertEquals("No empty string", 24, result); + } + @Test + public void test5() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.numbers("12\n12,12,12,12\n12"); + // Assert + assertEquals("No empty string", 72, result); + } + + @Test + public void test6() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.numbers("8,\n"); + // Assert + assertEquals("No empty string", -1, result); + } + @Test + public void test7() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.numbers("8,5\n"); + // Assert + assertEquals("No empty string", 13, result); + } + + @Test + public void test8() throws StringCalculatorException { + StringCalculator calc = new StringCalculator(); + + int result = calc.numbersNochar("8,5\n6,s,1,g"); + // Assert + assertEquals("No empty string", 20, result); + } }