From 6b90d8e9be144ee02a5c1abc23d6ed2f5b2cb6d5 Mon Sep 17 00:00:00 2001 From: usern12 Date: Thu, 13 Oct 2016 16:22:28 +0300 Subject: [PATCH] assignment not finished --- bin/Spreadsheet.class | Bin 613 -> 3811 bytes bin/SpreadsheetTest.class | Bin 490 -> 3314 bytes src/Spreadsheet.java | 122 +++++++++++++++++++++++++++++++++++-- tests/SpreadsheetTest.java | 98 +++++++++++++++++++++++++++-- 4 files changed, 211 insertions(+), 9 deletions(-) diff --git a/bin/Spreadsheet.class b/bin/Spreadsheet.class index 92b411f05752a6cee6d06c9769e1b70d40aa0840..cbecc399ace007537fc0dd72268a449f90c279ed 100644 GIT binary patch literal 3811 zcmai0TT@%t75)wqNQh%w$i@aFH^g?hNDyMzNsR#|HnD3GE>1|WfwH(eK{}a1!?ds4`GDHt*WjOg?Sfg#i2p!q=^vR(HnA&UTMpy>6!p+>wc_QU@tl&D?+1{O!B>mL%Ia#NW zG~jFR7&qV_$j#Ux_^`)BEkXh8Gf=mb-&BP<_+N z6iH#wmKUpF2hm{wA@ZG&z#Bt|q07YM=w|*>Y0Evs1O4rT9RsrGxQP>JG!ROq?bO`4 zg`$-SvHYG2;7Jx!In)6fTB(BVn)nn>83;J}XPsF`xflM0o_zVhBwux()W#6%L z*mkz-q!a=vgMkiiaWEIa^{w7jV_+~_uxBOP8QdVSLe8VYKm#jhCuuy2pndt`4Jqyg z6E3)}nX}&*lg_Ab*EVV{QPf+9B3=vNCbM527*v>e9dAfZnNO=A)p2l}?Vy&^!7Y3? zfZJre>|jf4;&WJHcv<^(o_=7aH!l1 z9u;ScPG-h-#m`qvd=+0~Q>b1Xz4rfSO{uV!@eP@s-+bt7t(RG`jBlHG3vUyqH9v1> zXAE?G9Lya_(Eu6gGTxCye)nN2^eI7npI5|DUC^#D`$H2y!U`iwTlo=1A}9VNGt7)K z*SYO;P%{<8&!}r-m5=3$ZpuFI$ZQW+jPJ*VL_ErN@$r`9n34;De*=80LQqXDUL=&% zQF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J%0 zDM>9ZnfTH$Oo)*|*e}0Cp)$2ZAv3oiCp9-UuOu~vi$R1zjGaN0kwGB8C|y6R6lj#b zV{vh6Q3(%&ID-VxytKs397YCF4WF#UvPAuy#JqI<;F6-uymV{Lu*sgRy7deUObm<+ ztPE^Gk{#$SeIU&Uq*=ALGjMDKGME^+fFwwo0jPi*Nb*1w0BK$zkCA~7D9gma4^<@q pRtNGR#2_{y!5s|32$Q&gY8im$umW{+Gf2XWV}lz96ap%d0sskYE)W0! 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"); + } + + }