diff --git a/bin/Spreadsheet.class b/bin/Spreadsheet.class index 92b411f..0008f88 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..44b074c 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..e9f4085 100644 --- a/src/Spreadsheet.java +++ b/src/Spreadsheet.java @@ -1,18 +1,85 @@ public class Spreadsheet { + String[][] matrix = new String [26][1000]; + char[] alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; + + public Spreadsheet(){} + + public Spreadsheet(String[][] matrix, char[] alphabet) { + super(); + this.matrix = matrix; + this.alphabet = alphabet; + } + + public String[][] getMatrix() { + return matrix; + } + + public void setMatrix(String[][] matrix) { + this.matrix = matrix; + } + + public char[] getAlphabet() { + return alphabet; + } + + public void setAlphabet(char[] alphabet) { + this.alphabet = alphabet; + } + public String get(String cell) { // to be implemented + char letra = cell.charAt(0); + char num = cell.charAt(1); + for (int i = 0; i < alphabet.length; i++) { + if(alphabet[i] == letra){ // You know if its A, B ... + + String value = matrix[i][num]; + + if(value.startsWith("'") && value.endsWith("'")){ + return value.substring(1, value.length()-1); + }else if(value.startsWith("'") && value.endsWith("")){ + return "#Error"; + }else if(value.startsWith("") && value.endsWith("'") && !value.startsWith("=")){ + return "#Error"; + }else if(value.startsWith("='") && value.endsWith("'")) + return value.substring(2, value.length()-1); + + + try { + Integer.parseInt(value); + return value; + } catch (NumberFormatException nfe){ + return "#Error"; + } + } + + } return null; } - + + public void set(String cell, String value) { // to be implemented + + char letra = cell.charAt(0); + char num = cell.charAt(1); + for (int i = 0; i < alphabet.length; i++) { + if(alphabet[i] == letra){ + matrix[i][num] = value; + } + } } - + public String evaluate(String cell) { // to be implemented + return null; + + } - + + + } diff --git a/tests/SpreadsheetTest.java b/tests/SpreadsheetTest.java index 9e0936a..3274694 100644 --- a/tests/SpreadsheetTest.java +++ b/tests/SpreadsheetTest.java @@ -7,9 +7,50 @@ public class SpreadsheetTest { @Test - public void test() { - fail("Not yet implemented"); + public void testGet() { + Spreadsheet ss = new Spreadsheet(); + ss.set("A3", "3"); + assertEquals("3", ss.get("A3")); } - + + @Test + public void testGet2() { + Spreadsheet ss = new Spreadsheet(); + + ss.set("A3", "3A"); + assertEquals("#Error", ss.get("A3")); + } + @Test + public void testGet3() { + Spreadsheet ss = new Spreadsheet(); + + ss.set("A3", "'is a String'"); + assertEquals("is a String", ss.get("A3")); + } + + @Test + public void testGet4() { + Spreadsheet ss = new Spreadsheet(); + + ss.set("A3", "'3A"); + assertEquals("#Error", ss.get("A3")); + } + + @Test + public void testGet5() { + Spreadsheet ss = new Spreadsheet(); + + ss.set("A3", "3A'"); + assertEquals("#Error", ss.get("A3")); + } + + @Test + public void testGet6() { + Spreadsheet ss = new Spreadsheet(); + + ss.set("A3", "='a String'"); + assertEquals("a String",ss.get("A3")); + } + }