diff --git a/src/Spreadsheet.java b/src/Spreadsheet.java index e4f120b..f35a768 100644 --- a/src/Spreadsheet.java +++ b/src/Spreadsheet.java @@ -1,18 +1,105 @@ +import java.util.HashMap; public class Spreadsheet { - + private String error = "#Error"; + HashMap sheet = new HashMap(); + + public static boolean isNumeric(String str) + { + return str.matches("-?\\d+"); //match a number with optional '-' and decimal. + } + + public static boolean isAlphaNumeric(String str) { + return str.matches("-?[a-zA-Z0-9]+"); + } + + public static boolean containsNumeric(String str) { + return str.matches(".*\\d.*"); + } + + public static boolean isString(String str) { + return str.matches("'.*'"); + } + + public static boolean isBroken(String str) { + if(str.matches("'.*[^\\']")) { + return true; + } + if(str.matches("[^\\'=].*'")) { + return true; + } + if(str.matches("='.*[^\\']")) { + return true; + } + if(str.matches("=[^\\'].*'")) { + return true; + } + return false; + } + + public static boolean isFormula(String str) { + return str.matches("=.*"); + } + + public static boolean isStringFormula(String str) { + return str.matches("=\\'.*\\'"); + } + + public static boolean isIntegerFormula(String str) { + return str.matches("=\\d+"); + } + + public static boolean isReference(String str) { + return str.matches("=[A-Za-z][0-9]+"); + } + public String get(String cell) { // to be implemented - return null; + return sheet.get(cell).toString(); } public void set(String cell, String value) { // to be implemented + sheet.put(cell, value); + } + + private Object evaluateFormula(String formula) { + if(Spreadsheet.isIntegerFormula(formula)) { + return Integer.parseInt(formula.substring(1, formula.length())); + } else { + if(Spreadsheet.isReference(formula)) { + String reffed = get(formula.substring(1, formula.length())); + return reffed.substring(1, reffed.length() - 1); + } else { + return formula.substring(2, formula.length() - 1); + } + } } - public String evaluate(String cell) { + public Object evaluate(String cell) { // to be implemented - return null; + Object v = sheet.get(cell); + String sv = v.toString(); + if(Spreadsheet.isAlphaNumeric(sv)) { + if(Spreadsheet.isNumeric(sv)) { + return Integer.parseInt(sv); + } else { + if(Spreadsheet.containsNumeric(sv)) { + return error; + } + } + } else { + if(Spreadsheet.isBroken(sv)) { + return error; + } + if(Spreadsheet.isFormula(sv)) { + return evaluateFormula(sv); + } + if(Spreadsheet.isString(sv)) { + return sv.substring(1, sv.length()- 1); + } + } + return v; } } diff --git a/tests/SpreadsheetTest.java b/tests/SpreadsheetTest.java index 9e0936a..06dadff 100644 --- a/tests/SpreadsheetTest.java +++ b/tests/SpreadsheetTest.java @@ -1,15 +1,81 @@ import static org.junit.Assert.*; - +import org.junit.Before; import org.junit.Test; public class SpreadsheetTest { + Spreadsheet shiaat; + @Before + public void initialization() { + shiaat = new Spreadsheet(); + } + + @Test + public void test_setGetFunctionInteger() { + shiaat.set("A1", "45"); + assertEquals(45, shiaat.evaluate("A1")); + } + + @Test + public void test_setGetFunctionIntegerNegative() { + shiaat.set("A1", "-45"); + assertEquals(-45, shiaat.evaluate("A1")); + } + @Test - public void test() { - fail("Not yet implemented"); - + public void test_setGetFunctionIntegerBad() { + shiaat.set("A1", "45A"); + assertEquals("#Error", shiaat.evaluate("A1")); + } + + @Test + public void test_setGetFunctionString() { + shiaat.set("A1", "'45A'"); + assertEquals("45A", shiaat.evaluate("A1")); + } + + @Test + public void test_setGetFunctionInvalidString1() { + shiaat.set("A1", "'45A"); + assertEquals("#Error", shiaat.evaluate("A1")); } + @Test + public void test_setGetFunctionInvalidString2() { + shiaat.set("A1", "45A'"); + assertEquals("#Error", shiaat.evaluate("A1")); + } + + @Test + public void test_setGetFunctionIntegerFormula() { + shiaat.set("A1", "=45"); + assertEquals(45, shiaat.evaluate("A1")); + } + + @Test + public void test_setGetFunctionStringFormula() { + shiaat.set("A1", "='45A'"); + assertEquals("45A", shiaat.evaluate("A1")); + } + + @Test + public void test_setGetFunctionStringFormulaBroken1() { + shiaat.set("A1", "=45A'"); + assertEquals("#Error", shiaat.evaluate("A1")); + } + + @Test + public void test_setGetFunctionStringFormulaBroken2() { + shiaat.set("A1", "='45A"); + assertEquals("#Error", shiaat.evaluate("A1")); + } + + @Test + public void test_setGetFunctionReference() { + shiaat.set("A1", "'Yo'"); + shiaat.set("A2", "=A1"); + assertEquals("Yo", shiaat.evaluate("A2")); + } }