diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..6f06ffe --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,3 @@ +/Spreadsheet.class +/SpreadsheetTest.class +/Cell.class diff --git a/bin/Spreadsheet.class b/bin/Spreadsheet.class index 92b411f..d46d90e 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..d7d8ded 100644 Binary files a/bin/SpreadsheetTest.class and b/bin/SpreadsheetTest.class differ diff --git a/src/Cell.java b/src/Cell.java new file mode 100644 index 0000000..aa45c8f --- /dev/null +++ b/src/Cell.java @@ -0,0 +1,68 @@ + +public class Cell { + + private String cellName; + private String cellContent; + private boolean isNumber; + private boolean isFormula; + + public Cell(String name, String content) { + this.cellName = name; + this.cellContent = content; + this.isNumber = false; + this.isFormula = false; + } + + public String getCellContent() { + if(isFormula || isNumber) { + return this.cellContent; + } + if(cellContent.charAt(0) == '\'' && cellContent.charAt(cellContent.length()-1) == '\'') { + StringBuilder build = new StringBuilder(this.cellContent); + build.deleteCharAt(0); + build.deleteCharAt(cellContent.length()-2); + String result = build.toString(); + return result; + } + return this.cellContent; + } + + public String getCellName() { + return this.cellName; + } + + public void setCellContent(String content) { + try { + int i = Integer.parseInt(content); + } catch(NumberFormatException nfe) { + this.cellContent = content; + this.isNumber = false; + return; + } + this.cellContent = content; + this.isNumber = true; + } + + public void setFormula(boolean isFormula) { + this.isFormula = isFormula; + } + + public boolean isFormula() { + return this.isFormula; + } + + /* + public void setCellContent(String content) { + try { + int i = Integer.parseInt(content); + } catch(NumberFormatException nfe) { + this.cellContent = content; + this.isNumber = false; + return; + } + this.cellContent = ""; + this.isNumber = true; + this.cellInt = Integer.parseInt(content); + } + */ +} diff --git a/src/Spreadsheet.java b/src/Spreadsheet.java index e4f120b..5560466 100644 --- a/src/Spreadsheet.java +++ b/src/Spreadsheet.java @@ -1,18 +1,78 @@ +import java.util.HashSet; +import java.util.Set; public class Spreadsheet { - - public String get(String cell) { - // to be implemented - return null; + + private Set cells; + + public Spreadsheet() { + cells = new HashSet(); + cells.add(new Cell("A1", "")); + cells.add(new Cell("A2", "")); + cells.add(new Cell("A3", "")); + cells.add(new Cell("A4", "")); + cells.add(new Cell("A5", "")); } public void set(String cell, String value) { - // to be implemented + boolean isFormula = false; + if(value.charAt(0) == '=') { + isFormula = true; + } + value = checkErrors(value); + for(Cell c : cells) { + if(c.getCellName() == cell) { + c.setCellContent(value); + c.setFormula(isFormula); + return; + } + } + throw new IllegalArgumentException("Cell doesn't exist"); } public String evaluate(String cell) { - // to be implemented - return null; + for(Cell c : cells) { + //System.out.println("Cell: " + c.getCellName() + ", Content: " + c.getCellContent() + ", isFormula: " + c.isFormula()); + if(c.getCellName() == cell) { + System.out.println(c.getCellName()); + if(c.isFormula()) { + evaluate(c.getCellContent()); + } + + return c.getCellContent(); + } + } + throw new IllegalArgumentException("Cell doesn't exist"); + } + + private String checkErrors(String value) { + System.out.println(value); + if(value.charAt(0) == '=') { + value = value.substring(1, value.length()); + System.out.println(value); + } + if(value.charAt(0) == '\'' && value.charAt(value.length()-1) == '\'') { + return value; + } + if(isCellReference(value)) { + return value; + } + try { + int i = Integer.parseInt(value); + } catch(NumberFormatException nfe) { + value = "#Error"; + return value; + } + return value; + } + + private boolean isCellReference(String value) { + for(Cell c : cells) { + if(c.getCellName().equals(value)) { + return true; + } + } + return false; } } diff --git a/tests/SpreadsheetTest.java b/tests/SpreadsheetTest.java index 9e0936a..a895052 100644 --- a/tests/SpreadsheetTest.java +++ b/tests/SpreadsheetTest.java @@ -1,15 +1,75 @@ import static org.junit.Assert.*; - +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class SpreadsheetTest { + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); @Test - public void test() { - fail("Not yet implemented"); - + public void set1Get1() { + Spreadsheet sheet = new Spreadsheet(); + sheet.set("A1", "1"); + assertEquals("1", "1", sheet.evaluate("A1")); + } + + @Test + public void nonExistingCellException() throws IllegalArgumentException { + expectedEx.expect(IllegalArgumentException.class); + expectedEx.expectMessage("Cell doesn't exist"); + Spreadsheet sheet = new Spreadsheet(); + sheet.evaluate("A12"); + } + + @Test + public void set1AGetError() { + Spreadsheet sheet = new Spreadsheet(); + sheet.set("A1", "Error"); + assertEquals("Error", "#Error", sheet.evaluate("A1")); + } + + @Test + public void setRandomAndOneApostropheGetError() { + Spreadsheet sheet = new Spreadsheet(); + sheet.set("A1", "124odskjfopd'421421,,,,,'''"); + assertEquals("Error", "#Error", sheet.evaluate("A1")); + } + + @Test + public void setStringGetString() { + Spreadsheet sheet = new Spreadsheet(); + sheet.set("A1", "'Text'"); + assertEquals("Text", "Text", sheet.evaluate("A1")); + } + + @Test + public void setEqualReturnString() { + Spreadsheet sheet = new Spreadsheet(); + sheet.set("A1", "='Text'"); + assertEquals("Text", "Text", sheet.evaluate("A1")); + } + + @Test + public void setEqualReturnErroneusString() { + Spreadsheet sheet = new Spreadsheet(); + sheet.set("A1", "='Text"); + assertEquals("Text", "#Error", sheet.evaluate("A1")); + } + + @Test + public void checkCellReference() { + Spreadsheet sheet = new Spreadsheet(); + sheet.set("A5", "99"); + sheet.set("A1", "=A5"); + assertEquals("99", sheet.evaluate("A1")); } + } + +// expectedEx.expect(IllegalArgumentException.class); +// expectedEx.expectMessage(""); \ No newline at end of file