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.
75 changes: 70 additions & 5 deletions src/Spreadsheet.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,83 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Spreadsheet {

private String excel[][] = new String[26][26];

private int AlphabetToNum(String str){
char[] ch = str.toCharArray();
for(char c : ch)
{
int temp = (int)c;
int temp_integer = 64; //for upper case
if(temp<=90 & temp>=65)
return temp-temp_integer;
}
return 0;
}

private boolean isCell(String split) {
// TODO Auto-generated method stub
String[] str = split.split("");

Pattern p = Pattern.compile("{upper}");
Pattern p2 = Pattern.compile("{digit}");
Matcher m = p.matcher(str[0]);
Matcher m2 = p2.matcher(str[1]);
if (m.find() & m2.find())
return true;
return false;
}

public String get(String cell) {
// to be implemented
return null;

String[] str = cell.split("");

return excel[AlphabetToNum(str[0])][Integer.parseInt(str[1])];

}

public void set(String cell, String value) {
// to be implemented

String[] str = cell.split("");
excel[AlphabetToNum(str[0])][Integer.parseInt(str[1])] = value;
}

public String evaluate(String cell) {
// to be implemented
return null;
int resultNum=0;
int count=0;
String result="";

String data = get(cell);
String[] str = data.split("");

for(int i=0; i<str.length; i++){

switch(str[i]){
case "=":
if(isCell(data.split("=")[1]))

break;
case "-":
if(i-1<0)
resultNum = 0-Integer.parseInt(str[1]);
result = result + String.valueOf(resultNum);
break;
case "'":
String spl1 = data.split("'")[1];
if(count==1)
result = result + spl1;
count++;
break;
}
}

if(result!="")
return result;

return "#Error";

}

}
59 changes: 58 additions & 1 deletion tests/SpreadsheetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,66 @@
public class SpreadsheetTest {

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

}

@Test
public void testSetup(){
Spreadsheet sp = new Spreadsheet();

sp.set("A1", "1");
assertEquals("1",sp.get("A1"));
}

@Test
public void testNumber_handling(){
Spreadsheet sp = new Spreadsheet();

sp.set("A1", "-1");
assertEquals("-1",sp.evaluate("A1"));

}

@Test
public void testWrongly_formatted_integers(){
Spreadsheet sp = new Spreadsheet();

sp.set("B1", "A%");
assertEquals("#Error",sp.evaluate("B1"));
}

@Test
public void testString_handling(){
Spreadsheet sp = new Spreadsheet();

sp.set("B3", "'a string'");
assertEquals("a string",sp.evaluate("B3"));
}

@Test
public void testUnquoted_strings(){
Spreadsheet sp = new Spreadsheet();

sp.set("B3", "'a string");
assertEquals("#Error",sp.evaluate("B3"));
}

@Test
public void testSimple_formulas(){
Spreadsheet sp = new Spreadsheet();

sp.set("B3", "='a string'");
assertEquals("a string",sp.evaluate("B3"));
}

@Test
public void testCell_references(){
Spreadsheet sp = new Spreadsheet();

sp.set("A1", "=A5");
assertEquals("#Circular",sp.evaluate("A1"));
}

}