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/StringCalculatorException.class
Binary file not shown.
Binary file modified bin/StringCalculatorTest.class
Binary file not shown.
109 changes: 106 additions & 3 deletions src/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,112 @@
//import java.text.ParseException;

public class StringCalculator {
public int add(String numbersStr) {



public int add(String numbersStr) throws StringCalculatorException {

// Returns the sum of the numbers given in numbersStr

// not yet implemented
return 0;
numbersStr=checkIfEmpty(numbersStr);
numbersStr=LinesToCommas(numbersStr); //replaces "\n" with ","
checkInvalidCommas(numbersStr); //Checks if there is more than 1 comma in a row

String [] StrArray=removeCommas(numbersStr); //removes commas from the string

checkForNumeric(StrArray);//checks if is numeric, if not, throws exception
int[] intArray=ChangeToInt(StrArray);//Strings to integer
checkForNegatives(intArray);//Checks that numbers are positive, if not, throws exception
checkNumberCount(intArray);//Checks how many numbers given, if more than 2, throws exception

int sum=0;
for (int i : intArray) {
sum=sum+i;
}
System.out.print("Sum is: ");
System.out.print(sum);

return sum;
}

public String checkIfEmpty(String numbersStr) {
if (numbersStr=="") {
numbersStr="0";
}
return numbersStr;
}

public void checkNumberCount(int[] intArray) throws StringCalculatorException {

if (intArray.length>2) {
throw new StringCalculatorException("");

}

}

public int[] ChangeToInt(String[] strArray) {
String[] arrOfStr =strArray;

int[] arrOfInt = new int[arrOfStr.length];

for (int i = 0; i < arrOfStr.length; i++) {

arrOfInt[i] = Integer.parseInt(arrOfStr[i]);
}

return arrOfInt;
}

public void checkForNegatives(int[] intArray) throws StringCalculatorException {
int[] array = intArray;
for (int i=0; i<array.length;i++)
{
if (array[i]<0) {
throw new StringCalculatorException("");
}
}

}

public void checkForNumeric(String[] strArray) throws StringCalculatorException {

String[] arrOfStr =strArray;


int[] arrOfInt = new int[arrOfStr.length];

for (int i = 0; i < arrOfStr.length; i++) {
try {
arrOfInt[i] = Integer.parseInt(arrOfStr[i]);
}
catch (NumberFormatException e) {
throw new StringCalculatorException("");
}
}
}



public String LinesToCommas(String numbersStr) {
String NoLines=numbersStr.replace("\n",",");
return NoLines;

}

public String[] removeCommas(String numbersStr) {
String [] NoCommas=numbersStr.split(",",-1);
return NoCommas;
}

public void checkInvalidCommas(String numbersStr) throws StringCalculatorException {
int a=numbersStr.indexOf(",,");

if (a>=0) {
throw new StringCalculatorException("");
}
}



}
4 changes: 4 additions & 0 deletions src/StringCalculatorException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

public class StringCalculatorException extends Exception {

public StringCalculatorException(String string) {
System.out.println(string);
}

}
79 changes: 77 additions & 2 deletions tests/StringCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,87 @@
import static org.junit.Assert.*;

import java.util.Arrays;

import org.junit.Test;

import junit.framework.Assert;

public class StringCalculatorTest {



@Test
public void test() {
fail("Not yet implemented");
public void TestStringCalculator_ChangeLinesToCommas() throws StringCalculatorException{
//Arrange
StringCalculator calculator = new StringCalculator();
//act
assertEquals("Fail","1,2,66",calculator.LinesToCommas("1\n2,66"));//
}

@Test(expected = StringCalculatorException.class)
public void TestStringCalculator_CommasInaRowShouldThrowException() throws StringCalculatorException {
StringCalculator calculator = new StringCalculator();
calculator.checkInvalidCommas("1,,2,3");
}

@Test
public void TestStringCalculator_RemoveCommas() throws StringCalculatorException {
//Arrange
StringCalculator calculator = new StringCalculator();

String input=("1,2,3");
String[] expected= {"1","2","3"};
assertArrayEquals(expected,calculator.removeCommas(input));
}


@Test(expected=StringCalculatorException.class)
public void TestStringCalculator_NotNumericThrowException() throws StringCalculatorException{
//Arrange
StringCalculator calculator = new StringCalculator();
//act
String[] input= {"a","2"};
calculator.checkForNumeric(input);
}

@Test
public void TestStringCalculator_ChangeToIntegerArray() throws StringCalculatorException {
//Arrange
StringCalculator calculator = new StringCalculator();

String[] input= {"1","2","3"};
int[] expected= {1,2,3};

assertArrayEquals(expected,calculator.ChangeToInt(input));
}

@Test(expected = StringCalculatorException.class)
public void TestStringCalculator_NegativeThrowsException() throws StringCalculatorException {
StringCalculator calculator = new StringCalculator();
int[] input= {-2,3,5};
calculator.checkForNegatives(input);
}


@Test (expected=StringCalculatorException.class)
public void TestStringCalculator_TooManyNumbersThrowException() throws StringCalculatorException {
StringCalculator calculator = new StringCalculator();
int[] input= {1,55,5};
calculator.checkNumberCount(input);
}


@Test
public void TestStringCalculator_NoNumbersReturnsZero() throws StringCalculatorException {
StringCalculator calculator = new StringCalculator();
//act
assertEquals("Fail", 0, calculator.add(""));
}
@Test
public void TestStringCalculator_AddsGivenNumbers() throws StringCalculatorException {
StringCalculator calculator = new StringCalculator();
//act
assertEquals("Fail", 20, calculator.add("14,6"));
}
}