1
0
Fork 0

//implemented sum() method & adapted to cells with two digits

main
Emelie Schneider 2024-01-04 17:18:38 +01:00
parent 6849d198ad
commit 5a69f8b0f5
1 changed files with 74 additions and 3 deletions

View File

@ -124,9 +124,25 @@ public class Spreadsheet {
private void evaluateCell(int row, int col) { private void evaluateCell(int row, int col) {
String formula = cells[row][col].getFormula(); String formula = cells[row][col].getFormula();
String result = ""; String result = "";
String sum1;
String sum2;
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) if (formula.startsWith("SUMME(")) { // e.g. SUMME(A3:A8) //SUMME(A13:B22)
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); // TODO adapt to cells with two digits /* 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) else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
result = "TODO"; // TODO result = "TODO"; // TODO
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
@ -157,7 +173,30 @@ public class Spreadsheet {
private long sum(String startCellName, String endCellName) { private long sum(String startCellName, String endCellName) {
// TODO implement // 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(); 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;
}
} }