diff --git a/bin/Spreadsheet.class b/bin/Spreadsheet.class index 92b411f..cbecc39 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..dcc0df0 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..5eed6e9 100644 --- a/src/Spreadsheet.java +++ b/src/Spreadsheet.java @@ -1,18 +1,130 @@ +import java.util.HashMap; public class Spreadsheet { + HashMap spreadsheet; + + public Spreadsheet() { + this.spreadsheet = new HashMap(); + } public String get(String cell) { - // to be implemented - return null; + + return spreadsheet.get(cell); } public void set(String cell, String value) { - // to be implemented + spreadsheet.put(cell, value); } public String evaluate(String cell) { - // to be implemented - return null; + String value = ""; + if(get(cell).charAt(0) == '-') { + value = get(cell); + } + else if(get(cell).charAt(0) == '=') { + value = checkEqual(get(cell)); + } + else if(Character.isDigit(get(cell).charAt(0))) { + value = checkDigit(get(cell)); + } + else if(get(cell).charAt(0) == '\'') { + value = checkString(get(cell)); + } else { + value = "#Error"; + } + return value; + } + + private String checkEqual(String string) { + if(string.charAt(1) == '\'') { + return checkForStringOperations(string); + } else if(Character.isLetter(string.charAt(1))) { + String otherValue = get(string.replaceAll("=", "")); + if(otherValue.charAt(0) == '\'') { + return checkString(otherValue); + } else if(Character.isDigit(otherValue.charAt(0))) { + return checkDigit(otherValue); + } else if (otherValue.charAt(0) == '-') { + return otherValue; + } else if(otherValue.charAt(0) == '=') { + return checkForCirculation(string, otherValue); + } + } else if(Character.isDigit(string.charAt(1))) { + return checkForOperations(string); + } + + return "#Error"; + } + private String checkForStringOperations(String string) { + if((string.contains("&")) && (string.charAt(1) == '\'') && (string.charAt(string.length()-1) == '\'')) { + for(int i = 0; i < string.length(); i++) { + if(string.charAt(i) == '&' && string.charAt(i-1) == '\'' && string.charAt(i+1) == '\'') { + return string.replaceAll("['=&]", ""); + } + + } + + } else if ( string.charAt(string.length()-1) == '\'') { + return checkString(string.replaceAll("=", "")); + } + + return "#Error"; + } + private String checkForOperations(String string) { + String s = string; + int sum = 0; + for(int i = 1; i < s.length(); i++) { + if(Character.isLetter(s.charAt(i))) { + return "#Error"; + } + switch(string.charAt(i)) { + case '+': + sum += Character.getNumericValue(s.charAt(i-1)) + Character.getNumericValue(s.charAt(i+1)); + break; + case '-': + sum += Character.getNumericValue(s.charAt(i-1)) - Character.getNumericValue(s.charAt(i+1)); + break; + case '*': + sum += Character.getNumericValue(s.charAt(i-1)) * Character.getNumericValue(s.charAt(i+1)); + break; + case '/': + sum += Character.getNumericValue(s.charAt(i-1)) / Character.getNumericValue(s.charAt(i+1)); + break; + case '%': + sum += Character.getNumericValue(s.charAt(i-1)) % Character.getNumericValue(s.charAt(i+1)); + break; + } + } + return Integer.toString(sum); + } + private String checkForCirculation(String string, String otherValue) { + String key = "="; + for(Object o : spreadsheet.keySet()) { + if(spreadsheet.get(o).equals(string)) { + key += o; + } + if(key.equals(otherValue)) { + return "#Circular"; + } + } + return otherValue; + } + + private String checkString(String string) { + if(string.charAt(string.length()-1) == '\'') { + return string.replaceAll("'", ""); + } else { + return "#Error"; + } + } + + private String checkDigit(String string) { + for(int i = 0 ; i < string.length(); i++) { + if(!Character.isDigit(string.charAt(i))) { + return "#Error"; + } + } + return string; } } diff --git a/tests/SpreadsheetTest.java b/tests/SpreadsheetTest.java index 9e0936a..f17e993 100644 --- a/tests/SpreadsheetTest.java +++ b/tests/SpreadsheetTest.java @@ -6,10 +6,100 @@ public class SpreadsheetTest { - @Test - public void test() { - fail("Not yet implemented"); - + Spreadsheet spreadsheet = new Spreadsheet(); + + @Test public void testSetandGet_1_1() { + spreadsheet.set("A1", "1"); + String value = spreadsheet.get("A1"); + assertEquals(value,"1"); } + @Test public void testNegativeIntegers_minus1_minus1() { + spreadsheet.set("A1", "-1"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"-1"); + } + + @Test public void testWronglyFormattedIntegers_5A_Error() { + spreadsheet.set("A1", "5A"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"#Error"); + } + + @Test public void testStringHandling_astring_astring() { + spreadsheet.set("A1", "'a string'"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"a string"); + } + + @Test public void testUnquotedStrings_astring_error() { + spreadsheet.set("A1", "'a string"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"#Error"); + } + + @Test public void testUnquotedStrings2_astring_error() { + spreadsheet.set("A1", "a string'"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"#Error"); + } + + @Test public void testEvaluateSimpleFormulas_isEqualaString_aString() { + spreadsheet.set("A1", "='a string'"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"a string"); + } + + @Test public void testEvaluate_SimpleFormulasWithErrors_equalaString_error() { + spreadsheet.set("A1", "='a string"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"#Error"); + } + + @Test public void testCellReferences_5_5() { + spreadsheet.set("A1", "5"); + spreadsheet.set("A2", "=A1"); + String value = spreadsheet.evaluate("A2"); + assertEquals(value,"5"); + } + + @Test public void testWrongCellReferences_5A_error() { + spreadsheet.set("A1", "5A"); + spreadsheet.set("A2", "=A1"); + String value = spreadsheet.evaluate("A2"); + assertEquals(value,"#Error"); + } + + @Test public void testDetectCircularReferences_A1_circular() { + spreadsheet.set("A1", "=A2"); + spreadsheet.set("A2", "=A1"); + String value = spreadsheet.evaluate("A2"); + assertEquals(value,"#Circular"); + } + + @Test public void testIntegerOperations_1plus1multi2_4() { + spreadsheet.set("A1", "=1+1*2"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"4"); + } + + @Test public void testErrorsInIntegerOperations_A1_error() { + spreadsheet.set("A1", "=1+1A"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"#Error"); + } + + @Test public void testStringOperations_A1_astring() { + spreadsheet.set("A1", "='a'&'string'"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"astring"); + } + + @Test public void testErrorsInStringOperations_A1_error() { + spreadsheet.set("A1", "='a&'string'"); + String value = spreadsheet.evaluate("A1"); + assertEquals(value,"#Error"); + } + + }