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.
91 changes: 87 additions & 4 deletions src/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,92 @@

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

public int add(String numbersStr)
{
// System.out.println("This is the string : "+numbersStr);
numbersStr.replaceAll("\\n", ","); // this should reduce the number of checked characters
// System.out.println("This is the string : "+numbersStr); apparently the above line does not function or the feed does not accept \
int[] numbertable = find_numbers(numbersStr);
int i = 0;
int result = 0;
for (i = 0; i<numbertable.length; i++)
{
result = result + numbertable[i];
}



return result;
}

public int cN (String a) // cN = ChangeNumber
{
return Integer.parseInt(a);
}

// this checks only if a character is a digit or one of the three other allowed figures
public int cC (char a)
{
// was used to test why system failed : System.out.println("character is " +a);
if (Character.isDigit(a)) return 1;
// became obsolete with the split - command if (a == ',') return 2;
/* these two might become obsolete when string "\n" can be replaced with "," leaving them in code
to remind me in similar selections in future programs
if (a == '\\') return 3;
if (a == 'n') return 4;
*/
return 0;

}
// completely useless after finding out about split - command for strings
public String separate (String a, int x, int y) //
{
return a.substring(x, y);
}

public int[] find_numbers(String numbersStr)
{
System.out.println("Stringi on "+ numbersStr);
String luvut[] = toStrings(numbersStr); // new String[3]; // Finnish for numbers[]
int numbers = luvut.length;
int brakes[] = new int[numbers]; // used to store the ints changed from strings.
int i, j;
for (i=0; i<numbers; i++) brakes[i]=0;
int passed = 0;

char c;
int digits = 0;

for (j = 0; j<numbers; j++)
{
for (i=0; i<luvut[j].length(); i++)
{
c = luvut[j].charAt(i);
passed = cC(c); // character sent for inspection
if (passed == 1) digits++;
}
if (digits == luvut[j].length())
{
brakes[j] = cN(luvut[j]);
System.out.println("number [j] " + j + " as letters " + luvut[j] + " and as a number " + brakes[j] );
}
else System.out.println("Feed has invalid characters and cannot be calculated");

// not yet implemented
return 0;
digits=0;
}


return brakes;
}

public String[] toStrings (String numbersStr )
// changes the string to multiple strings based on the location on ","
{
String [] feedback = numbersStr.split(",");

return feedback;

}

}

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

import org.junit.Ignore;
import org.junit.Test;

/* the initial task is faulty. It claims that the string calculator can take 0, 1 or 2 numbers but
the example includes 3. Therefore this has been programmed to take up to any number of string based numbers.
*/

public class StringCalculatorTest {

@Test
public void test() {
fail("Not yet implemented");
public void test_entire_calculation_with_invalid_characters() {

StringCalculator laske = new StringCalculator();
int result=laske.add("vmp");
if (result != 0) fail("Supposed to calculate 0 result");
}


@Test
public void test_calculator_with_valid_input()
{

StringCalculator laske = new StringCalculator();
int result=laske.add("18,54,55");
if (result != 18+54+55) fail("Not working, getting wrong result");
}

@Test
public void testForMoreNumbers()
{

StringCalculator laske = new StringCalculator();
int result = laske.add("19,54,55,77");
if (result != 19+54+55+77) fail("Not working, should calculate even 4 numbers");
}


@Test
public void testMethodcC () //tests if a character is valid. should fail with character a that is inserted into the string
{
StringCalculator laske = new StringCalculator();
int result;
String t="5a\\n,15kn";
for (int i=0; i<t.length(); i++)
{
result = laske.cC(t.charAt(i));
if (result == 0) fail("Failed for wrong type of characters");
}
}


@Test
public void testMethodcCwithSeeminglyValidInput ()
//tests if a character is valid for all of the allowed types of characters allowed
{
StringCalculator laske = new StringCalculator();
int result;
String t="5\\n,n15";
for (int i=0; i<t.length(); i++)
{
result = laske.cC(t.charAt(i));
if (result == 0) fail("Not quite");
}
}

@Test
public void testMethod_separate()
//tests if the separate method for finding the an individual number in the feed functions
{
StringCalculator laske = new StringCalculator();
String aaru = "765,254";
int x=4;
int y=7;
aaru=laske.separate(aaru, x, y);
if (aaru != "254") fail ("not the correct result"); // apparently this fails for some reason?
}

@Test
public void testMethod_find_numbers()
//tests if the separate method for finding the an individual number in the feed functions
{
StringCalculator laske = new StringCalculator();
String aaru = "765,254,45";
laske.find_numbers(aaru);
// if (aaru != "254") fail ("not the correct result"); // apparently this fails for some reason?
}

@Test
public void testMethod_add_with_two_numbers()
//tests if the separate method for finding the an individual number in the feed functions
{
StringCalculator laske = new StringCalculator();
String aaru = "76,24";
int result=laske.add(aaru);
if (result != 100) fail ("not the correct result"); // apparently this fails for some reason?
}

@Test
public void test_Method_toStrings_with_feed_different_feeds()
{
StringCalculator laske = new StringCalculator();
String aaru = "76,24,45,58";
laske.toStrings(aaru);
String aaru2 = "76,24,45";
laske.toStrings(aaru2);
String aaru3 = "76,24,1,14,55";
laske.toStrings(aaru3);
String aaru4 = "0,a,b";
laske.toStrings(aaru4);
// this was just used to see what feeds the mechanism built on the 10th+ try can take

}

@Test
public void test_Method_add_with_empty_feed()
{
StringCalculator laske = new StringCalculator();
String aaru = "";
laske.add(aaru);
}

@Test
public void test_Method_add_with_no_commas()
{
StringCalculator laske = new StringCalculator();
String aaru = "25";
int result=laske.add(aaru);
if (result != 25) fail ("not quite the result we wanted");
}


}