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

1-10 task done #26

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
Empty file.
Empty file.
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.
122 changes: 114 additions & 8 deletions src/Spreadsheet.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,124 @@
import java.awt.Event;
import java.util.ArrayList;

public class Spreadsheet {

public String[][] matrix = new String[27][1000];
public static char[] abecedario = new char[26];




public Spreadsheet() {
super();

int a = (int) 'A';

for(int i = 0; i < abecedario.length; i++)
{
abecedario[i] = (char) (a + i);
}

}


public char intToAbc(int num){
return abecedario[num];

}
public int abcToInt(char a){
int num=-1;
for(int i = 0; i < abecedario.length; i++)
{
if (abecedario[i]== a) {
num=i;
break;
}
}

return num;

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

int row= abcToInt(cell.charAt(0));
cell= cell.substring(1);
int column = Integer.valueOf(cell);

return matrix[row][column];
}

public void set(String cell, String value) {
// to be implemented
int row= abcToInt(cell.charAt(0));
cell= cell.substring(1);
int column = Integer.valueOf(cell);
matrix[row][column]= value;

}

public String evaluate(String cell) {
// to be implemented
return null;
String value="";

if (get(cell).startsWith("=")) {

if (get(cell).startsWith("='") && get(cell).endsWith("'")) {
value+= get(cell).substring(2, get(cell).length()-1);
}else if (isCell(get(cell).substring(1))) {
String nextCell=get(cell).substring(1);

if (isNumeric(get(nextCell))) {
value+=get(nextCell);
}else value.equals("#Error1");


if (get(nextCell).substring(1).equals(cell)) {
value+="#Circula";

}


}else return "#Error2";



}else{

if (isNumeric(get(cell))) {

value+= get(cell);
}else if (get(cell).startsWith("'") && get(cell).endsWith("'")) {
value+= get(cell).substring(1, get(cell).length()-1);
}else return "#Error3";

}
return value;
}


// si = + String '' --> devuelve el string
// si = + String a medias 'o' --> devuelve #error
// si = + Letra+Numeros A13--> devuelve le valor de esa celda

private static boolean isNumeric(String cadena){
try {
Integer.parseInt(cadena);
return true;
} catch (NumberFormatException nfe){
return false;
}
}
private static boolean isCell(String cadena){
if (cadena.charAt(0)>='A'&&cadena.charAt(0)<='Z' && isNumeric(cadena.substring(1))&& cadena.charAt(1)!='-') {
return true;
}else return false;
}

public static void main(String[] args) {

Spreadsheet sp= new Spreadsheet();
sp.set("B13","=B16");
sp.set("B16", "5");
System.out.println(sp.evaluate("B13"));

}

}
32 changes: 31 additions & 1 deletion tests/SpreadsheetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,37 @@ public class SpreadsheetTest {
@Test
public void test() {
fail("Not yet implemented");


}



@Test
public void testGet() {
boolean ok=false;
Spreadsheet sp= new Spreadsheet();
sp.set("B13", "Jon");
if ("Jon"== sp.get("B13")) {
ok=true;
}
assertTrue(ok);

}
@Test
public void testEvaluate() {
boolean ok=false;
Spreadsheet sp= new Spreadsheet();
sp.set("B13", "-234");
sp.evaluate("B13");
if (sp.evaluate("B13").equals("true") ) {
ok=true;
System.out.println("iep");
}
assertTrue(ok);

}




}