Skip to content
Open
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
2 changes: 2 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/Spreadsheet.class
/SpreadsheetTest.class
Binary file modified bin/Spreadsheet.class
Binary file not shown.
Binary file modified bin/SpreadsheetTest.class
Binary file not shown.
118 changes: 114 additions & 4 deletions src/Spreadsheet.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,128 @@

public class Spreadsheet {
String [][] sheet = new String [20][20];
String [] abcd = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T"};

public String get(String cell) {
// to be implemented
return null;
String letter = cell.substring(0, 1);
String column = cell.substring(1, 2);
int columnNumber, rowNumber;

rowNumber = searchCellRow(letter);
columnNumber = searchCellColumn(column);

return sheet[rowNumber][columnNumber];
}

private int searchCellColumn(String column) {
boolean found = false;
int counter = 0;
while (!found && counter < 20) {
if (column.equals(String.valueOf(counter))) {
found = true;
}
else {
++counter;
}
}
return counter;
}

private int searchCellRow(String letter) {
boolean found = false;
int counter = 0;

while (!found && counter < 20) {
if (letter.equals(abcd[counter])) {
found = true;
}
else {
++counter;
}
}
return counter;
}

public void set(String cell, String value) {
// to be implemented
String letter = cell.substring(0, 1);
String column = cell.substring(1, 2);

sheet[searchCellRow(letter)][searchCellColumn(column)] = value;
}

public String evaluate(String cell) {
// to be implemented
String value = get(cell);
for (int i = 0; i < value.length(); i++) {
if ((value.charAt(i) == '+') || (value.charAt(i) == '-') || (value.charAt(i) == '*') || (value.charAt(i) == '/')) {
return calculator(value);
}
}
if (value.charAt(0) == '=') {
if ((Character.isAlphabetic(value.charAt(1)) && (Character.isDigit(value.charAt(2))))) {
String refCell = value.substring(1, 3);
String circular = "=" + cell;
if (get(refCell) != circular) {
return get(refCell);
}
else {
return "#Circular";
}
}
String realValue = "";
for (int i = 1; i < value.length(); i++) {
realValue = realValue + value.charAt(i);
}
value = realValue;
}

if (isANumber(value)) {
return value;
}
else if (isAString(value)) {
String realString = "";
for (int i = 1; i < value.length() - 1; i++) {
realString = realString + value.charAt(i);
}
return realString;
}
else {
return "#Error";
}
}

private String calculator(String value) {
int calc;
return null;
}

private boolean isAString(String value) {
if ((value.charAt(0) == '\'') && (value.charAt(value.length() - 1) == '\'')){
return true;
}
else {
return false;
}
}

private Boolean isANumber(String value) {
int counter = 0;
if (value.isEmpty()) {
return false;
}
if (value.charAt(counter) == '-') {
if (value.length() > 1) {
++counter;
}
else {
return false;
}
}
for (; counter < value.length(); ++counter) {
if (!Character.isDigit(value.charAt(counter))) {
return false;
}
}
return true;
}

}
69 changes: 65 additions & 4 deletions tests/SpreadsheetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,72 @@


public class SpreadsheetTest {
Spreadsheet sheet = new Spreadsheet();

@Test
public void test() {
fail("Not yet implemented");

@Test public void testSetA4_12() {
String cell = "A4";
sheet.set(cell, "12");
assertEquals("12", sheet.get(cell));
}

@Test public void testSetB12_4() {
String cell = "B12";
sheet.set(cell, "4");
assertEquals("4", sheet.get(cell));
}

@Test public void evaluateString_StringWithQuotes() {
String cell = "B12";
sheet.set(cell, "'thisIsAString'");
assertEquals("thisIsAString", sheet.evaluate(cell));
}
@Test public void evaluateUnquotedString_Error() {
String cell = "A1";
sheet.set(cell, "'thisIsNotAString");
assertEquals("#Error", sheet.evaluate(cell));
}
@Test public void evaluateInvalidNumber_Error() {
String cell = "A1";
sheet.set(cell, "324as");
assertEquals("#Error", sheet.evaluate(cell));
}
@Test public void evaluateNegativeNumber_minus10() {
String cell = "F2";
sheet.set(cell, "-10");
assertEquals("-10", sheet.evaluate(cell));
}
@Test public void evaluateStringEquals_StringWithoutEqual() {
String cell = "D9";
sheet.set(cell, "='thisIsAString'");
assertEquals("thisIsAString", sheet.evaluate(cell));
}
@Test public void evaluateNumberEquals_NumberWithoutEqual() {
String cell = "C5";
sheet.set(cell, "=-15");
assertEquals("-15", sheet.evaluate(cell));
}
@Test public void evaluateStringEqualsWithError_Error() {
String cell = "C5";
sheet.set(cell, "=='thisIsAString");
assertEquals("#Error", sheet.evaluate(cell));
}
@Test public void evaluateRefCell_valueInF2() {
String cell = "C5";
sheet.set(cell, "=F2");
assertEquals(sheet.get("F2"), sheet.evaluate(cell));
}
@Test public void evaluateIncorrectRefCell_Error() {
String cell = "C6";
sheet.set(cell, "=2F");
assertEquals("#Error", sheet.evaluate(cell));
}
@Test public void evaluateCircularRefCell_Circular() {
String cell = "F2";
sheet.set("C5", "=F2");
sheet.set(cell, "=C5");
assertEquals("#Circular", sheet.evaluate(cell));
}



}