//implemented sum() method & adapted to cells with two digits
parent
6849d198ad
commit
5a69f8b0f5
|
@ -124,9 +124,25 @@ public class Spreadsheet {
|
|||
private void evaluateCell(int row, int col) {
|
||||
String formula = cells[row][col].getFormula();
|
||||
String result = "";
|
||||
String sum1;
|
||||
String sum2;
|
||||
|
||||
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8)
|
||||
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); // TODO adapt to cells with two digits
|
||||
if (formula.startsWith("SUMME(")) { // e.g. SUMME(A3:A8) //SUMME(A13:B22)
|
||||
/* if(Character.isDigit(formula.charAt(8))) {
|
||||
sum1= formula.substring(6, 9);
|
||||
|
||||
if(Character.isDigit(formula.charAt(12))) {
|
||||
sum2=formula.substring(10,13);
|
||||
}else sum2=formula.substring(10,12);
|
||||
}else {
|
||||
sum1= formula.substring(6, 8);
|
||||
if(Character.isDigit(formula.charAt(11))) {
|
||||
sum2=formula.substring(9,12);
|
||||
}else sum2=formula.substring(9,11);
|
||||
|
||||
} */
|
||||
result = "" + sum(getStartCellName(formula), getEndCellName(formula)); // TODO adapt to cells with two digits
|
||||
}
|
||||
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
||||
result = "TODO"; // TODO
|
||||
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
||||
|
@ -157,7 +173,30 @@ public class Spreadsheet {
|
|||
private long sum(String startCellName, String endCellName) {
|
||||
// TODO implement
|
||||
|
||||
return 0;
|
||||
int startRow=0;
|
||||
int startCol=0;
|
||||
|
||||
int endRow=0;
|
||||
int endCol;
|
||||
int sum=0;
|
||||
int temp;
|
||||
|
||||
startRow= getRow(startCellName);
|
||||
startCol= getCol(startCellName);
|
||||
|
||||
endRow= getRow(endCellName);
|
||||
endCol= getCol(endCellName);
|
||||
|
||||
for(int i= startRow; i<=endRow; i++) {
|
||||
for(int j=startCol; j<=endCol; j++) {
|
||||
|
||||
temp= Integer.parseInt(cells[i][j].getValue());
|
||||
sum=sum+temp;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -210,4 +249,36 @@ public class Spreadsheet {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//methods to get correct cell names if they have more then 1 digit
|
||||
|
||||
public String getStartCellName(String formula) {
|
||||
|
||||
String startCell="";
|
||||
if(Character.isDigit(formula.charAt(8))) {
|
||||
startCell= formula.substring(6, 9);
|
||||
}else startCell= formula.substring(6, 8);
|
||||
|
||||
return startCell;
|
||||
}
|
||||
|
||||
public String getEndCellName(String formula) {
|
||||
|
||||
String endCell="";
|
||||
|
||||
if(Character.isDigit(formula.charAt(8))) {
|
||||
if(Character.isDigit(formula.charAt(12))) {
|
||||
endCell=formula.substring(10,13);
|
||||
}else endCell=formula.substring(10,12);
|
||||
}else {
|
||||
if(Character.isDigit(formula.charAt(11))) {
|
||||
endCell=formula.substring(9,12);
|
||||
}else endCell=formula.substring(9,11);
|
||||
|
||||
}
|
||||
|
||||
return endCell;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue