From deabe361d464034291da2477aec4dc5c97755f72 Mon Sep 17 00:00:00 2001 From: "Dr.Janson" Date: Sat, 6 Jan 2024 15:26:14 +0100 Subject: [PATCH] SUMME fertig --- .../informatik/spreadsheet/Spreadsheet.java | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index ceacf6f..2241b80 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -37,12 +37,10 @@ public class Spreadsheet { private String get(int row, int col) { return cells[row][col].getValue(); } - public String get(String cellName) { cellName = cellName.toUpperCase(); return get(getRow(cellName), getCol(cellName)); } - private void put(int row, int col, String value) { if (!value.startsWith("=")) cells[row][col].setValue(value); @@ -51,20 +49,16 @@ public class Spreadsheet { evaluateCell(row, col); } } - public void put(String cellName, String value) { cellName = cellName.toUpperCase(); put(getRow(cellName), getCol(cellName), value); } - private int getCol(String cellName) { return cellName.charAt(0) - 'A'; } - private int getRow(String cellName) { return cellName.charAt(1) - '1'; } - // ----- // business logic @@ -114,19 +108,19 @@ public class Spreadsheet { String formula = cells[row][col].getFormula(); String result = ""; - 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 - else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) + if (formula.startsWith("SUMME(")) { // e.g. SUMME(A3:A8) + result += sum(formula.substring(6, 8), formula.substring(9, 11)); + }else if (formula.startsWith("PRODUKT(")) { // e.g. PRODUKT(A3:B9) 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 - 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 - 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 - 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 - else if (!formula.isEmpty()) { + }else if (!formula.isEmpty()) { try { result = "" + calculate(formula); } catch(ArithmeticException ae) { @@ -144,9 +138,14 @@ public class Spreadsheet { * @return The sum calculated. */ private long sum(String startCellName, String endCellName) { - // TODO implement - - return 0; + long res = 0; + for(int j = getRow(startCellName); j< (getRow(endCellName)+1);j++){ + 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; } /**