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.
81 changes: 75 additions & 6 deletions src/Spreadsheet.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,87 @@
import java.util.HashMap;

public class Spreadsheet {

static HashMap<String, String> hashMap = new HashMap<String, String>();

public String get(String cell) {
// to be implemented
return null;
public static String get(String cell) {
String value = hashMap.get(cell);
return value;
}

public void set(String cell, String value) {
// to be implemented
hashMap.put(cell,value);
}

public String evaluate(String cell) {
// to be implemented
return null;
String value = hashMap.get(cell);
int i = 0;
int j = value.length()-1; // j=2
//first char is "="
if(value.charAt(i)=='='){
//second or last char is "'"
if(value.charAt(i+1)=='\'' || value.charAt(value.length()-1)=='\''){
//second and last chars are both "'"
if(value.charAt(i+1)=='\'' && value.charAt(value.length()-1)=='\''){
//return chars between ''
return value.substring(2, value.length()-1);
}
//one of second and last char is not "'"
else
//return error
return "#Error";
}
//chars after "=" can be found in other cells
else if(hashMap.containsKey(value.substring(1,value.length()))){
//find its value in next cells
while(hashMap.containsKey(value.substring(1,value.length()))){
//this value become next cell to be found
value = hashMap.get(value.substring(1,value.length()));
//circular
if(value.substring(1,value.length()).equals(cell))
return "#Circular";
//value of this cell doesn't start with a "="
else if(value.charAt(i) != '='){
//value of this cell start or end with a "'"
if(value.charAt(i)=='\'' || value.charAt(value.length()-1)=='\''){
//first and last chars are both "'"
if(value.charAt(i)=='\'' && value.charAt(value.length()-1)=='\''){
//return chars between ''
return value.substring(1, value.length()-1);
}
//one of second and last char is not "'"
else
//return error
return "#Error";
}
//value doesn't start with a "'"
else
//check every char in value
for(i=0;i<value.length();i++){
//there are illegal char in value
if( !(value.charAt(i)>='0' && value.charAt(i)<='9' || value.charAt(i)=='-'))
//return error
return "#Error";
}
//there isn't illegal char in value
return value;
}
}
}
//including calculating
else
return value.substring(1, j);
}
else if(value.charAt(i)=='\'' && value.charAt(j)=='\'')
return value.substring(1, j);
else if((value.charAt(i)=='\'' && value.charAt(j)!='\'') || (value.charAt(i)!='\'' && value.charAt(j)=='\''))
return "#Error";
else
for(i=0;i<value.length();i++){
if(! (value.charAt(i)>='0' && value.charAt(i)<='9' || value.charAt(i)=='-'))
return "#Error";
}
return value;
}

}
84 changes: 81 additions & 3 deletions tests/SpreadsheetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,89 @@


public class SpreadsheetTest {

Spreadsheet s = new Spreadsheet();

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

public void test_1() {
s.set("A1","1");
String result = s.get("A1");
assertEquals("1",result);
}

@Test
public void test_2() {
s.set("A1","-1");
String result = s.evaluate("A1");
assertEquals("-1",result);
}

@Test
public void test_3() {
s.set("A1","5A");
String result = s.evaluate("A1");
assertEquals("#Error",result);
}

@Test
public void test_4() {
s.set("A1","'a string'");
String result = s.evaluate("A1");
assertEquals("a string",result);
}

@Test
public void test_5() {
s.set("A1","'a string");
String result = s.evaluate("A1");
assertEquals("#Error",result);
}

@Test
public void test_6() {
s.set("A1","='a string'");
String result = s.evaluate("A1");
assertEquals("a string",result);
}

@Test
public void test_7() {
s.set("A1","='a string");
String result = s.evaluate("A1");
assertEquals("#Error",result);
}

@Test
public void test_8() {
s.set("A5","5");
s.set("A1","=A5");
String result = s.evaluate("A1");
assertEquals("5",result);
}

@Test
public void test_9() {
s.set("A5","5A");
s.set("A1","=A5");
String result = s.evaluate("A1");
assertEquals("#Error",result);
}

@Test
public void test_10() {
s.set("A5","=A1");
s.set("A1","=A5");
String result = s.evaluate("A1");
assertEquals("#Circular",result);
}

@Test
public void test_11() {
s.set("A1","=1+1*2");
String result = s.evaluate("A1");
assertEquals("4",result);
}



}