public class StringCalculator{ public static int add(String numbers){ int result = 0; if(numbers == "") return result; for(String n: numbers.split(",")) { result = result + Integer.parseInt(n); } return result; } }
- public class StringCalculator{
- public static int add(String numbers){
if(numbers == "") {return 0;}- int result = 0;
String[] numbersArr = numbers.split(",");if (numbersArr.length > 1) {int result = Integer.parseInt(numbersArr[0]) + Integer.parseInt(numbersArr[1]);return result;}return Integer.parseInt(numbers);- if(numbers == "")
- return result;
- for(String n: numbers.split(","))
- {
- result = result + Integer.parseInt(n);
- }
- return result;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class StringCalculatorTest { @Test void returnsZeroForEmptyString() { int result = StringCalculator.add(""); assertEquals(0, result); } @Test void returnsOneForInputOne() { int result = StringCalculator.add("1"); assertEquals(1, result); } @Test void returnsTwoForInputTwo() { int result = StringCalculator.add("2"); assertEquals(2, result); } @Test void returnsSumForInputOneTwo() { int result = StringCalculator.add("1,2"); assertEquals(3, result); } @Test void returnsSumForInputOneTwoAndThree() { int result = StringCalculator.add("1,2,3"); assertEquals(6, result); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class StringCalculatorTest {
- @Test
- void returnsZeroForEmptyString() {
- int result = StringCalculator.add("");
- assertEquals(0, result);
- }
- @Test
- void returnsOneForInputOne() {
- int result = StringCalculator.add("1");
- assertEquals(1, result);
- }
- @Test
- void returnsTwoForInputTwo() {
- int result = StringCalculator.add("2");
- assertEquals(2, result);
- }
- @Test
- void returnsSumForInputOneTwo() {
- int result = StringCalculator.add("1,2");
- assertEquals(3, result);
- }
- @Test
- void returnsSumForInputOneTwoAndThree() {
- int result = StringCalculator.add("1,2,3");
- assertEquals(6, result);
- }
- }