Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bin/StringCalculator.class
Binary file not shown.
Binary file modified bin/StringCalculatorTest.class
Binary file not shown.
47 changes: 43 additions & 4 deletions src/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class StringCalculator {
public int add(String numbersStr) {
// Returns the sum of the numbers given in numbersStr
public int add(String numbersStr) throws StringCalculatorException {

// not yet implemented
return 0;
int size = numOfnums(numbersStr);
String corStr = replaceNewlines(numbersStr);
checkInput(corStr);
Scanner s = new Scanner(corStr).useDelimiter(",");
//ArrayList<Integer> arr = new ArrayList<Integer>();

int j = 0;
int x = 0;
if (size==0) return 0;
else {
while (size > j) {
x += s.nextInt();
j++;
}
return x;
}

}

public int numOfnums(String numberStr) {
if (numberStr.length()==0) return 0;
String commaStr = replaceNewlines(numberStr);
List<String> splittedStr = Arrays.asList(commaStr.split(","));
int size = splittedStr.size();
return size;
}

public String replaceNewlines(String str) {
String commaStr = str.replace("\n", ",");
return commaStr;
}

public void checkInput(String str) throws StringCalculatorException{
String input = replaceNewlines(str);
if (input.length()>0 && !input.matches(".*\\d+.*")) throw new StringCalculatorException();
if (input.length()>1 && !input.matches("\\d.*\\d")) throw new StringCalculatorException();
if (input.matches(".*[a-z].*")) throw new StringCalculatorException();
if (input.contains("-")) throw new StringCalculatorException();
if (input.contains(" ")) throw new StringCalculatorException();
}
}
77 changes: 75 additions & 2 deletions tests/StringCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,85 @@
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class StringCalculatorTest {

@Test
public void test() {
fail("Not yet implemented");
public void testNumOfnums() {
StringCalculator strC = new StringCalculator();

String testStr = "";
int testSize = strC.numOfnums(testStr);
assertEquals(testSize, 0);

String testStr2 = "1,2";
int testSize2 = strC.numOfnums(testStr2);
assertEquals(testSize2, 2);
}

@Test
public void testReplaceNL() {
StringCalculator strC = new StringCalculator();

String testStr = "1\n2";
String testStr2 = "1,2";
String testStr3 = "1";

String newStr = strC.replaceNewlines(testStr);
String newStr2 = strC.replaceNewlines(testStr2);
String newStr3 = strC.replaceNewlines(testStr3);

assertEquals(newStr, "1,2");
assertEquals(newStr2, "1,2");
assertEquals(newStr3, "1");
}

@Test
public void testAdd() throws StringCalculatorException {
StringCalculator strC = new StringCalculator();

String testStr1 = "1,2";
int result1 = strC.add(testStr1);
assertEquals(result1, 3);

String testStr2 = "2\n2";
int result2 = strC.add(testStr2);
assertEquals(result2, 4);

String testStr3 = "1";
int result3 = strC.add(testStr3);
assertEquals(result3, 1);

String testStr4 = "";
int result4 = strC.add(testStr4);
assertEquals(result4, 0);
}

@Test(expected = StringCalculatorException.class)
public void testCheckInput() throws StringCalculatorException {
StringCalculator strC = new StringCalculator();

String testStr1 = "-1,2";
strC.checkInput(testStr1);

String testStr2 = "///";
strC.checkInput(testStr2);

String testStr3 = "1\na";
strC.checkInput(testStr3);

String testStr4 = "abc";
strC.checkInput(testStr4);

String testStr5 = "1, 2";
strC.checkInput(testStr5);

String testStr6 = "1,,";
strC.checkInput(testStr6);
}

}