diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index 39d4cdf..bdb2500 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -20,8 +20,9 @@ public class Axel { spr.cells[sc.nextInt()-1][5].setFormula(sc.next()); spr.updateSpreadsheet(); - + System.out.println(spr.toStringShowFormula()); System.out.println(spr); + System.out.println(spr.cells[0][5]); // TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods. diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index b86d043..83aa9d3 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -56,7 +56,7 @@ public class Spreadsheet { } public String getCellName(int row, int col) { - return "" + ((char) ('A' + col)) + (row+1); + return ("" + ((char) ('A' + col))) + (row+1); } private void put(int row, int col, String value) { @@ -79,7 +79,7 @@ public class Spreadsheet { private int getRow(String cellName) { if(cellName.length()>2) - return Integer.parseInt(""+cellName.charAt(1)+cellName.charAt(2)) - 1; + return Integer.parseInt(""+cellName.charAt(1) + cellName.charAt(2)); return cellName.charAt(1) - '1'; } @@ -91,7 +91,7 @@ public class Spreadsheet { * * @param path The file to read. * @param separator The char used to split up the input, e.g. a comma or a semicolon. - * @param starCellName The upper left cell where data from the CSV file should be inserted. + * @param startCellName The upper left cell where data from the CSV file should be inserted. * @return Nothing. * @throws IOException If path does not exist. */ @@ -144,7 +144,7 @@ public class Spreadsheet { /** * This method does the actual evaluation/calcluation of a specific cell * - * @param cellName the name of the cell to be evaluated + * @param row,col the name of row and column of the cell * @return Nothing. */ private void evaluateCell(int row, int col) { @@ -153,83 +153,73 @@ public class Spreadsheet { if(formula.length()>10) { String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4); - String substringStart2 = formula.substring(formula.length() - 6, formula.length() - 3); - String substringEndCell = formula.substring(formula.length() - 3, formula.length() - 1); + String substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5); + String substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5); + String substringEndCell1 = formula.substring(formula.length() - 3, formula.length() - 1); + String substringEndCell2 = formula.substring(formula.length() - 4, formula.length() - 1); - if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) + if (formula.startsWith("SUMME("))// e.g. SUMME(A3:A8) if (formula.length() < 13) - result = "" + sum(substringStartCell1, substringEndCell); - else if (formula.length() < 14 && substringStart2.contains(":")) - result = "" + sum(substringStartCell1, substringEndCell); - else if (formula.length() < 14 && !substringStart2.contains(":")) - result = "" + sum(substringStart2, substringEndCell); + result = "" + sum(substringStartCell1, substringEndCell1); + else if (formula.length() < 14) + result = "" + sum(substringStartCell2, substringEndCell2); else if (formula.length() < 15) - result = "" + sum(substringStart2, substringEndCell); + result = "" + sum(substringStartCell3, substringEndCell2); else result = "0"; else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) if (formula.length() < 15) - result = "" + product(substringStartCell1, substringEndCell); - else if (formula.length() < 16 && substringStart2.contains(":")) - result = "" + product(substringStartCell1, substringEndCell); - else if (formula.length() < 16 && !substringStart2.contains(":")) - result = "" + product(substringStart2, substringEndCell); + result = "" + product(substringStartCell1, substringEndCell1); + else if (formula.length() < 16 && !substringEndCell2.contains(":")) + result = "" + product(substringStartCell2, substringEndCell2); else if (formula.length() < 17) - result = "" + product(substringStart2, substringEndCell); + result = "" + product(substringStartCell3, substringEndCell2); else result = "0"; else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) if (formula.length() < 17) - result = "" + average(substringStartCell1, substringEndCell); - else if (formula.length() < 18 && substringStart2.contains(":")) - result = "" + average(substringStartCell1, substringEndCell); - else if (formula.length() < 18 && !substringStart2.contains(":")) - result = "" + average(substringStart2, substringEndCell); + result = "" + average(substringStartCell1, substringEndCell1); + else if (formula.length() < 18) + result = "" + average(substringStartCell2, substringEndCell2); else if (formula.length() < 19) - result = "" + average(substringStart2, substringEndCell); + result = "" + average(substringStartCell3, substringEndCell2); else result = "0"; else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung if (formula.length() < 13) - result = "" + standardDeviation(substringStartCell1, substringEndCell); - else if (formula.length() < 14 && substringStart2.contains(":")) - result = "" + standardDeviation(substringStartCell1, substringEndCell); - else if (formula.length() < 14 && !substringStart2.contains(":")) - result = "" + standardDeviation(substringStart2, substringEndCell); + result = "" + standardDeviation(substringStartCell1, substringEndCell1); + else if (formula.length() < 14) + result = "" + standardDeviation(substringStartCell2, substringEndCell2); else if (formula.length() < 15) - result = "" + standardDeviation(substringStart2, substringEndCell); + result = "" + standardDeviation(substringStartCell3, substringEndCell2); else result = "0"; - else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> kleinster Wert + else if (formula.startsWith("MIN(")) // e.g. MIN(C1:H13) -> kleinster Wert if (formula.length() < 11) - result = "" + min(substringStartCell1, substringEndCell); - else if (formula.length() < 12 && substringStart2.contains(":")) - result = "" + min(substringStartCell1, substringEndCell); - else if (formula.length() < 12 && !substringStart2.contains(":")) - result = "" + min(substringStart2, substringEndCell); + result = "" + min(substringStartCell1, substringEndCell1); + else if (formula.length() < 12) + result = "" + min(substringStartCell2, substringEndCell2); else if (formula.length() < 13) - result = "" + min(substringStart2, substringEndCell); + result = "" + min(substringStartCell3, substringEndCell2); else result = "0"; else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert if (formula.length() < 11) - result = "" + max(substringStartCell1, substringEndCell); - else if (formula.length() < 12 && substringStart2.contains(":")) - result = "" + max(substringStartCell1, substringEndCell); - else if (formula.length() < 12 && !substringStart2.contains(":")) - result = "" + max(substringStart2, substringEndCell); + result = "" + max(substringStartCell1, substringEndCell1); + else if (formula.length() < 12) + result = "" + max(substringStartCell2, substringEndCell2); else if (formula.length() < 13) - result = "" + max(substringStart2, substringEndCell); + result = "" + max(substringStartCell3, substringEndCell2); else result = "0"; } @@ -259,22 +249,28 @@ public class Spreadsheet { for(int c = getCol(startCellName); c0) + result *= Long.parseLong(cells[r][c].getValue()); + else { + result = Long.parseLong(cells[r][c].getValue()); + counter++; + } + } catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){} return result; } - private double average(String startCellName, String endCellName){ + private double average(String startCellName, String endCellName) { double result = 0; int counter = 0; for(int r = getRow(startCellName); r cellNames = new ArrayList<>(); - long avg = 0; + double avg = 0; int counter = 0; - for(int r = getRow(startCellName); r copyCellNames = new ArrayList<>(cellNames); double[] frequency = new double[cellNames.size()]; Arrays.fill(frequency,1); @@ -308,19 +305,7 @@ public class Spreadsheet { for(int i = 0; i< cellNames.size(); i++) { for(int t = 0; t13) + sb.append(" " + String.format("%13s", cells[r][c].getValue()).substring(0,13) + " |"); + else + sb.append(" " + String.format("%13s", cells[r][c].getValue()) + " |"); + } + + } + sb.append(System.lineSeparator()); + for (int i = 0; i < cells[0].length; i++) { + sb.append("----------------"); + } + sb.append("------"); + sb.append(System.lineSeparator()); + return sb.toString(); + + + } + public String toStringShowFormula() { + StringBuilder sb = new StringBuilder(); + + sb.append(System.lineSeparator()); + sb.append(" |"); + for (int i = 0; i < cells[0].length; 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(System.lineSeparator()); + 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()) + " |"); } }