Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignment not finished #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Binary file modified bin/Spreadsheet.class
Binary file not shown.
Binary file modified bin/SpreadsheetTest.class
Binary file not shown.
122 changes: 117 additions & 5 deletions src/Spreadsheet.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,130 @@
import java.util.HashMap;

public class Spreadsheet {

HashMap<String, String> spreadsheet;

public Spreadsheet() {
this.spreadsheet = new HashMap<String, String>();
}
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;
}

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


}