From 347d36a669067207831952ef72a3e0e592f1661f Mon Sep 17 00:00:00 2001 From: selim Date: Wed, 27 Dec 2023 19:14:30 +0100 Subject: [PATCH] Some method descriptions, toString/seeFormula updated, readCsv fix. --- Axel/resources/zahlen.csv | 19 ++- .../informatik/spreadsheet/Spreadsheet.java | 130 ++++++++++++++---- 2 files changed, 114 insertions(+), 35 deletions(-) diff --git a/Axel/resources/zahlen.csv b/Axel/resources/zahlen.csv index 6c59c73..9167e5d 100644 --- a/Axel/resources/zahlen.csv +++ b/Axel/resources/zahlen.csv @@ -1,10 +1,9 @@ -,,,,,,,,, -,,,,,,,,, -,,,,,,,,, -,,,,,,,,, -,,,,,,,,, -,,,,,,,,, -,,,,,,,,, -,,,,,,,,, -,,,,,,,,, -,,,,,,,,, +=2,,,=SUMME(A1:A6),,,,,,,, +,,,,,,,,,,, +,,,,,,,,,,, +,,,,,,,,,,, +,,,,,,,,,,, +,,,,,,,,,,, +,,,,,,,,,,, +,,,,,,,,,,, +,,,,,,,,,,, diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index d6771b6..de45b73 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -110,7 +110,7 @@ public class Spreadsheet { for (int r = getRow(startCellName)-1; r < memList.size(); r++) for (int c = getCol(startCellName)-1; c < memList.get(0).length(); c++) if(c cellNames = new ArrayList<>(); @@ -344,6 +376,14 @@ public class Spreadsheet { return Math.sqrt(mem); //standardDeviation formula } + + /** + * Method to find out the lowest number of a rectangular block of cells, such as from A1 to B3. + * + * @param startCellName The name of the cell in the upper left corner of the rectangle. + * @param endCellName The name of the cell in the lower right corner of the rectangle. + * @return The lowest number. + */ private double min(String startCellName, String endCellName){ ArrayList cellNames = new ArrayList<>(); @@ -363,6 +403,14 @@ public class Spreadsheet { } catch(NumberFormatException n){} return result; } + + /** + * Method to find out the highest number of a rectangular block of cells, such as from A1 to B3. + * + * @param startCellName The name of the cell in the upper left corner of the rectangle. + * @param endCellName The name of the cell in the lower right corner of the rectangle. + * @return The highest number. + */ private double max(String startCellName, String endCellName){ ArrayList cellNames = new ArrayList<>(); @@ -429,27 +477,49 @@ public class Spreadsheet { return result; } - private double evaluateOperator(double result, double currentOperand, String currentOperator) { + + /** + * This method does the actual calculation with the current operator, + * the left and right operands provided by the calculate method. + * + * @param leftOperand The first operand used to calculate. + * @param rightOperand The second operand used to calculate. + * @param currentOperator The current operator used to calculate. + * @return The partial result calculated. + */ + private double evaluateOperator(double leftOperand, double rightOperand, String currentOperator) { switch (currentOperator) { case "+": - return (result + currentOperand); + return (leftOperand + rightOperand); case "-": - return (result - currentOperand); + return (leftOperand - rightOperand); case "*": - return (result * currentOperand); + return (leftOperand * rightOperand); case "/": - return (result / currentOperand); + return (leftOperand / rightOperand); default: throw new IllegalArgumentException("Invalid operator: " + currentOperator); } } ///--- ui methods + + /** + * This method updates all cells value by provided formula. + * + * @return nothing. + */ public void updateSpreadsheet() { for (int r = 0; r < cells.length; r++) for (int c = 0; c < cells[r].length; c++) evaluateCell(r, c); } + + /** + * This method scans and evaluates the user input. + * + * @return The boolean true, when the programm is finished by the user input. + */ public boolean cellInput() throws FileNotFoundException { Scanner cs = new Scanner(System.in); @@ -491,36 +561,42 @@ public class Spreadsheet { inputs.clear(); return false; } + + /** + * This method outputs the cell spreadsheet with all the values. + * + * @return nothing. + */ public String toString() { StringBuilder sb = new StringBuilder(); sb.append(System.lineSeparator()); sb.append(System.lineSeparator()); - sb.append(" |"); + sb.append(" |"); for (int i = 0; i < cells[0].length; i++) { - sb.append(" " + (char) ('A' + i) + " |"); + sb.append(" " + (char) ('A' + i) + " |"); } int rc = 1; for (int r = 0; r < cells.length; r++) { sb.append(System.lineSeparator()); for (int i = 0; i < cells[0].length; i++) { - sb.append("----------------"); + sb.append("----------------------"); } sb.append("------"); sb.append(System.lineSeparator()); - sb.append(String.format(" " + "%2s", rc++) + " |"); + sb.append(String.format(" " + "%2s", rc++) + " |"); for (int c = 0; c < cells[r].length; c++) { - if((cells[r][c].getValue()).length()>13) - sb.append(" " + String.format("%13s", cells[r][c].getValue()).substring(0,13) + " |"); + if((cells[r][c].getValue()).length()>19) + sb.append(" " + String.format("%19s", cells[r][c].getValue()).substring(0,19) + " |"); else - sb.append(" " + String.format("%13s", cells[r][c].getValue()) + " |"); + sb.append(" " + String.format("%19s", cells[r][c].getValue()) + " |"); } } sb.append(System.lineSeparator()); for (int i = 0; i < cells[0].length; i++) { - sb.append("----------------"); + sb.append("----------------------"); } sb.append("------"); sb.append(System.lineSeparator()); @@ -528,36 +604,40 @@ public class Spreadsheet { } + + + /** + * This method outputs the cell spreadsheet with all the formulas instead of the values. + * + * @return nothing. + */ public String toStringShowFormula() { StringBuilder sb = new StringBuilder(); sb.append(System.lineSeparator()); sb.append(System.lineSeparator()); - sb.append(" |"); + sb.append(" |"); for (int i = 0; i < cells[0].length; i++) { - sb.append(" " + (char) ('A' + i) + " |"); + sb.append(" " + (char) ('A' + i) + " |"); } int rc = 1; for (int r = 0; r < cells.length; r++) { sb.append(System.lineSeparator()); for (int i = 0; i < cells[0].length; i++) { - sb.append("----------------"); + sb.append("----------------------"); } sb.append("------"); sb.append(System.lineSeparator()); - sb.append(String.format(" " + "%2s", rc++) + " |"); + sb.append(String.format(" " + "%2s", rc++) + " |"); for (int c = 0; c < cells[r].length; c++) { - if((cells[r][c].getFormula()).length()>13) - sb.append(" " + String.format("%13s", cells[r][c].getFormula()).substring(0,13) + " |"); - else - sb.append(" " + String.format("%13s", cells[r][c].getFormula()) + " |"); + sb.append(" " + String.format("%19s", cells[r][c].getFormula()) + " |"); } } sb.append(System.lineSeparator()); for (int i = 0; i < cells[0].length; i++) { - sb.append("----------------"); + sb.append("----------------------"); } sb.append("------"); sb.append(System.lineSeparator()); @@ -565,4 +645,4 @@ public class Spreadsheet { } -} \ No newline at end of file +}