diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..2079672 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,2 @@ +/Spreadsheet.class +/SpreadsheetTest.class diff --git a/bin/Spreadsheet.class b/bin/Spreadsheet.class index 92b411f..6d3f4b5 100644 Binary files a/bin/Spreadsheet.class and b/bin/Spreadsheet.class differ diff --git a/bin/SpreadsheetTest.class b/bin/SpreadsheetTest.class index e7086bd..de27e72 100644 Binary files a/bin/SpreadsheetTest.class and b/bin/SpreadsheetTest.class differ diff --git a/src/Spreadsheet.java b/src/Spreadsheet.java index e4f120b..8d5088d 100644 --- a/src/Spreadsheet.java +++ b/src/Spreadsheet.java @@ -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]; } } diff --git a/tests/SpreadsheetTest.java b/tests/SpreadsheetTest.java index 9e0936a..a82aa10 100644 --- a/tests/SpreadsheetTest.java +++ b/tests/SpreadsheetTest.java @@ -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")); } + }