From 5a69f8b0f5fae97b4e5f4f3c7de9af5e34889e93 Mon Sep 17 00:00:00 2001 From: <3010678@stud.hs-mannheim.de> Date: Thu, 4 Jan 2024 17:18:38 +0100 Subject: [PATCH] //implemented sum() method & adapted to cells with two digits --- .../informatik/spreadsheet/Spreadsheet.java | 77 ++++++++++++++++++- 1 file changed, 74 insertions(+), 3 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 2de5c46..acf6548 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -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) @@ -156,8 +172,31 @@ public class Spreadsheet { */ private long sum(String startCellName, String endCellName) { // TODO implement + + 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 0; + return sum; } /** @@ -209,5 +248,37 @@ 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; + } + } \ No newline at end of file