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
2 changes: 2 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/Spreadsheet.class
/SpreadsheetTest.class
Binary file modified bin/Spreadsheet.class
Binary file not shown.
Binary file modified bin/SpreadsheetTest.class
Binary file not shown.
46 changes: 37 additions & 9 deletions src/Spreadsheet.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
import java.util.ArrayList;

public class Spreadsheet {
public class Spreadsheet
{

public String get(String cell) {
// to be implemented
return null;
// the limit of array is hard coded to 128 x 128
private String[][] cells = new String[128][128];

public String get(String cell)
{
return cells[0][0];
}

public void set(String cell, String value) {
// to be implemented
public void set(String cell, String value)
{
cells[0][0] = value;
}

public String evaluate(String cell) {
// to be implemented
return null;
public String evaluate(String cell)
{
String tempCell = cells[0][0];
if (tempCell.substring(0,1).equals("-"))
{
tempCell = tempCell.substring(1);
}
else if (tempCell.substring(0, 1).equals("'"))
{
tempCell = tempCell.substring(1);
if (tempCell.indexOf('\'') == tempCell.length() - 1)
{
tempCell = tempCell.substring(0, tempCell.length() - 1);
return tempCell;
}
else
{
return "#Error";
}
}
if (!tempCell.matches("\\d+"))
{
return "#Error";
}
return cells[0][0];
}

}
47 changes: 43 additions & 4 deletions tests/SpreadsheetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,51 @@
import org.junit.Test;


public class SpreadsheetTest {
public class SpreadsheetTest
{
Spreadsheet spreadsheet = new Spreadsheet();

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

public void setA1GetMinusOneIntTest()
{
spreadsheet.set("A1", "-1");
assertSame("Get result doesn't match to the set value", "-1", spreadsheet.get("A1"));
}

@Test
public void setA1EvaluatePlusOneIntTest()
{
spreadsheet.set("A1", "1");
assertSame("Evaluate result doesn't match to the set value", "1", spreadsheet.evaluate("A1"));
}

@Test
public void setA1EvaluateMinusOneIntTest()
{
spreadsheet.set("A1", "-1");
assertSame("Evaluate result doesn't match to the set value", "-1", spreadsheet.evaluate("A1"));
}

@Test
public void setA1EvaluateMinusTwoIntTest()
{
spreadsheet.set("A1", "-2");
assertSame("Evaluate result doesn't match to the set value", "-2", spreadsheet.evaluate("A1"));
}

@Test
public void setA1EvaluateA5Test()
{
spreadsheet.set("A1", "A5");
assertSame("Evaluate result doesn't match to the set value", "#Error", spreadsheet.evaluate("A1"));
}

@Test
public void setA1EvaluateCorrectString()
{
spreadsheet.set("A1", "'a string'");
assertEquals("a string", spreadsheet.evaluate("A1"));
}


}