Skip to content
Open

done #23

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Spreadsheet.class
/SpreadsheetTest.class
/Cell.class
Binary file modified bin/Spreadsheet.class
Binary file not shown.
Binary file modified bin/SpreadsheetTest.class
Binary file not shown.
68 changes: 68 additions & 0 deletions src/Cell.java
Original file line number Diff line number Diff line change
@@ -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);
}
*/
}
74 changes: 67 additions & 7 deletions src/Spreadsheet.java
Original file line number Diff line number Diff line change
@@ -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<Cell> cells;

public Spreadsheet() {
cells = new HashSet<Cell>();
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;
}

}
68 changes: 64 additions & 4 deletions tests/SpreadsheetTest.java
Original file line number Diff line number Diff line change
@@ -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("");