1
0
Fork 0

SUMME fertig

main
Dr.Janson 2024-01-06 15:26:14 +01:00
parent 98026d2e20
commit deabe361d4
1 changed files with 16 additions and 17 deletions

View File

@ -37,12 +37,10 @@ public class Spreadsheet {
private String get(int row, int col) { private String get(int row, int col) {
return cells[row][col].getValue(); return cells[row][col].getValue();
} }
public String get(String cellName) { public String get(String cellName) {
cellName = cellName.toUpperCase(); cellName = cellName.toUpperCase();
return get(getRow(cellName), getCol(cellName)); return get(getRow(cellName), getCol(cellName));
} }
private void put(int row, int col, String value) { private void put(int row, int col, String value) {
if (!value.startsWith("=")) if (!value.startsWith("="))
cells[row][col].setValue(value); cells[row][col].setValue(value);
@ -51,20 +49,16 @@ public class Spreadsheet {
evaluateCell(row, col); evaluateCell(row, col);
} }
} }
public void put(String cellName, String value) { public void put(String cellName, String value) {
cellName = cellName.toUpperCase(); cellName = cellName.toUpperCase();
put(getRow(cellName), getCol(cellName), value); put(getRow(cellName), getCol(cellName), value);
} }
private int getCol(String cellName) { private int getCol(String cellName) {
return cellName.charAt(0) - 'A'; return cellName.charAt(0) - 'A';
} }
private int getRow(String cellName) { private int getRow(String cellName) {
return cellName.charAt(1) - '1'; return cellName.charAt(1) - '1';
} }
// ----- // -----
// business logic // business logic
@ -114,19 +108,19 @@ public class Spreadsheet {
String formula = cells[row][col].getFormula(); String formula = cells[row][col].getFormula();
String result = ""; String result = "";
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) 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 result += sum(formula.substring(6, 8), formula.substring(9, 11));
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)
result = "TODO"; // TODO result = "TODO"; // TODO
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung }else if (formula.startsWith("STABW(")) { // e.g. STABW(C6:D8) -> Standardabweichung
result = "TODO"; // TODO result = "TODO"; // TODO
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert }else if (formula.startsWith("MIN(")) {// e.g. MIN(C13:H13) -> größter Wert
result = "TODO"; // TODO result = "TODO"; // TODO
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung }else if (formula.startsWith("MAX(")) { // e.g. MAX(A1:A10) -> Standardabweichung
result = "TODO"; // TODO result = "TODO"; // TODO
else if (!formula.isEmpty()) { }else if (!formula.isEmpty()) {
try { try {
result = "" + calculate(formula); result = "" + calculate(formula);
} catch(ArithmeticException ae) { } catch(ArithmeticException ae) {
@ -144,9 +138,14 @@ public class Spreadsheet {
* @return The sum calculated. * @return The sum calculated.
*/ */
private long sum(String startCellName, String endCellName) { private long sum(String startCellName, String endCellName) {
// TODO implement long res = 0;
for(int j = getRow(startCellName); j< (getRow(endCellName)+1);j++){
return 0; for(int i = getCol(startCellName); i< (getCol(endCellName)+1);i++) {
if(!cells[j][i].getValue().isBlank())
res += Long.parseLong(cells[j][i].getValue());
}
}
return res;
} }
/** /**