diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a818314 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b113384 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Axel/resources/zahlen.csv b/Axel/resources/zahlen.csv index 1ec60e0..5bce6de 100644 --- a/Axel/resources/zahlen.csv +++ b/Axel/resources/zahlen.csv @@ -1,4 +1,10 @@ -1,2 -3,4 -5,6 -7,8 +=MITTELWERT(B1:C10),=10,=234,,,,,,, +,,,,,,,,, +,,,,,,,,, +,,,,,,,,, +,,,,,,,,, +,,,,,,,,, +,,,,,,,,, +,,,,,,,,, +,,,,,,,,, +,,,,,,,,, diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index df720b3..46d1b7d 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -1,29 +1,59 @@ package de.hs_mannheim.informatik.spreadsheet; import java.io.FileNotFoundException; +import java.util.InputMismatchException; +import java.util.Scanner; /** * Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim. * - * @author Oliver Hummel + * @author Oliver Hummel, Selim Eser (2211482) */ public class Axel { public static void main(String[] args) throws FileNotFoundException { Spreadsheet spr = new Spreadsheet(10,10); - - spr.put("A3", "123"); - spr.put("A2", "1"); - - spr.put("B9", "=41+A2"); - spr.put("J5", "=7*6"); - spr.put("J6", "=3/2"); - - System.out.println(spr); - - spr.saveCsv("/tmp/test.csv"); - - // TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods. + boolean match = false; + boolean stop = false; + Scanner sc = new Scanner(System.in); + + System.out.println("\nAxel(ExcelFakeVon2211482): "); + while (!match) { + try { + System.out.print("\nTabellengröße(ReihenZwischen1-99 SpaltenZwischen1-26): "); + spr = new Spreadsheet(sc.nextInt(), sc.nextInt()); + match = true; + } catch(InputMismatchException i){ + System.out.println("\nGeben Sie zwei Ganzzahlen mit Leerzeichen dazwischen an!"); + System.out.println("\nEnter zum fortfahren."); + sc.nextLine(); + sc.nextLine(); + } + } + while (!stop) { + + try { + spr.updateSpreadsheet(); + System.out.println(spr); + System.out.println("\nHILFE (Für eine Anleitung.)\n"); + System.out.print("KOMMANDO: "); + stop = spr.cellInput(); + } catch(Exception e){ + System.out.println("\nEingabe unzulässig!"); + System.out.println("\nEnter zum fortfahren."); + sc.nextLine(); + sc.nextLine(); + } + + } } + public static void help(){ + System.out.println("\nEinlesen: \nKOMMANDO Dateipfad Trennzeichen StartzelleObenLinks (Alles mit Leerzeichen getrennt.) e.g.:\nEINLESEN Axel/resources/zahlen.csv , A1"); + System.out.println("\nSpeichern: \nKOMMANDO Dateipfad (Mit Leerzeichen getrennt.) e.g.:\nSPEICHERN Axel/resources/zahlen.csv"); + System.out.println("\nFormel Tabellenansicht: \nKOMMANDO e.g.:\nFORMELN"); + System.out.println("\nZellen Werte/Formeln zuweisen: \nZELLE ZUWEISUNG (Mit Leerzeichen getrennt, Zelle beginnend mit Buchstabe und Zuweisung beginnend mit =; Keine Kommazahlen als Eingabe erlaubt.) e.g.:\nA1 =A5+15\n" + + "\nZELLE FORMELZUWEISUNG (Formelzuweisung mit Startzelle bis Endzelle getrennt durch :.) e.g.:" + + "\nA2 =SUMME(A1:H10)\t\tA3 =PRODUKT(A1:H10)\t\tA4 =MITTELWERT(A1:H10)\t\tA5 =STABW(A1:H10) (Standartabweichung)\t\tA6 =MIN(A1:H10)\t\tA7 =MAX(A1:H10)"); + } } \ No newline at end of file diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Cell.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Cell.java index f80e156..b02a610 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Cell.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Cell.java @@ -4,7 +4,7 @@ package de.hs_mannheim.informatik.spreadsheet; * Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim. * A cell needs to be able to hold a formula and a value * - * @author Oliver Hummel + * @author Oliver Hummel, Selim Eser (2211482) */ public class Cell { private String formula = ""; diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index ceacf6f..fcf8845 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -1,8 +1,12 @@ package de.hs_mannheim.informatik.spreadsheet; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -10,19 +14,27 @@ import java.util.regex.Pattern; * A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim. * One aspect worth mentioning is that it only supports long numbers, not doubles. * - * @author Oliver Hummel + * @author Oliver Hummel, Selim Eser (2211482) */ public class Spreadsheet { Cell[][] cells; /** * Constructor that creates a Spreadsheet of size rows * cols. + * * @param rows number of rows * @param cols number of columns */ public Spreadsheet(int rows, int cols) { - // TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns + if(rows>99) + rows = 99; + else if(rows<1) + rows = 1; + if(cols < 1) + cols = 1; + else if(cols > 26) + cols = 26; cells = new Cell[rows][cols]; @@ -40,7 +52,11 @@ public class Spreadsheet { public String get(String cellName) { cellName = cellName.toUpperCase(); - return get(getRow(cellName), getCol(cellName)); + return get(getRow(cellName)-1, getCol(cellName)-1); + } + + public String getCellName(int row, int col) { + return ("" + ((char) ('A' + col))) + (row+1); } private void put(int row, int col, String value) { @@ -54,15 +70,17 @@ public class Spreadsheet { public void put(String cellName, String value) { cellName = cellName.toUpperCase(); - put(getRow(cellName), getCol(cellName), value); + put(getRow(cellName)-1, getCol(cellName)-1, value); } private int getCol(String cellName) { - return cellName.charAt(0) - 'A'; + return cellName.charAt(0) - 'A' + 1; } private int getRow(String cellName) { - return cellName.charAt(1) - '1'; + if(cellName.length()>2) + return Integer.parseInt(""+cellName.charAt(1) + cellName.charAt(2)); + return cellName.charAt(1) - '1' + 1; } // ----- @@ -70,21 +88,39 @@ public class Spreadsheet { /** * A method for reading in data from a CSV file. - * @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 path The file to read. + * @param separator The char used to split up the input, e.g. a comma or a semicolon. + * @param startCellName The upper left cell where data from the CSV file should be inserted. * @return Nothing. - * @exception IOException If path does not exist. + * @throws IOException If path does not exist. */ public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException { - // TODO: implement this + Scanner sc = new Scanner(new File(path)); + ArrayList memList = new ArrayList<>(); + + while (sc.hasNextLine()) { + memList.add(sc.nextLine()); + } + + String[][] memArr = new String[memList.size()][]; + for(int i = 0; i < memList.size(); i++) + memArr[i] = (memList.get(i)).split(String.valueOf(separator)); + + for (int r = getRow(startCellName)-1; r < memList.size(); r++) + for (int c = getCol(startCellName)-1; c < memList.get(0).length(); c++) + if(c9) { + substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4); + substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5); + substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5); + substringEndCell1 = formula.substring(formula.length() - 3, formula.length() - 1); + substringEndCell2 = formula.substring(formula.length() - 4, formula.length() - 1); + } + + if (formula.startsWith("SUMME("))// e.g. SUMME(A3:A8) + if (formula.length() < 13) + result = sum(substringStartCell1, substringEndCell1); + else if (formula.length() < 14) + result = sum(substringStartCell2, substringEndCell2); + else if (formula.length() < 15) + result = sum(substringStartCell3, substringEndCell2); + else + result = 0; + - 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) - result = "TODO"; // TODO + if (formula.length() < 15) + result = product(substringStartCell1, substringEndCell1); + else if (formula.length() < 16 && !substringEndCell2.contains(":")) + result = product(substringStartCell2, substringEndCell2); + else if (formula.length() < 17) + result = product(substringStartCell3, substringEndCell2); + else + result = 0; + + else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) - result = "TODO"; // TODO + if (formula.length() < 18) + result = average(substringStartCell1, substringEndCell1); + else if (formula.length() < 19) + result = average(substringStartCell2, substringEndCell2); + else if (formula.length() < 20) + result = average(substringStartCell3, substringEndCell2); + else + result = 0; + + 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 - result = "TODO"; // TODO - else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung - result = "TODO"; // TODO + if (formula.length() < 13) + result = standardDeviation(substringStartCell1, substringEndCell1); + else if (formula.length() < 14) + result = standardDeviation(substringStartCell2, substringEndCell2); + else if (formula.length() < 15) + result = standardDeviation(substringStartCell3, substringEndCell2); + else + result = 0; + + + else if (formula.startsWith("MIN(")) // e.g. MIN(C1:H13) -> kleinster Wert + if (formula.length() < 11) + result = min(substringStartCell1, substringEndCell1); + else if (formula.length() < 12) + result = min(substringStartCell2, substringEndCell2); + else if (formula.length() < 13) + 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, substringEndCell1); + else if (formula.length() < 12) + result = max(substringStartCell2, substringEndCell2); + else if (formula.length() < 13) + result = max(substringStartCell3, substringEndCell2); + else + result = 0; + + else if (!formula.isEmpty()) { try { - result = "" + calculate(formula); - } catch(ArithmeticException ae) { - result = "exc."; + result = calculate(formula); + } catch (ArithmeticException ae) { + result = 0; } } - cells[row][col].setValue("" + result); + if(!cells[row][col].getFormula().isEmpty()) + if(String.format("%.2f",result).substring(String.format("%.2f",result).length()-2).charAt(0)=='0'&& + String.format("%.2f",result).substring(String.format("%.2f",result).length()-2).charAt(1)=='0') + cells[row][col].setValue(String.format("%.0f",result)); + + else if(String.format("%.2f",result).substring(String.format("%.2f",result).length()-2).charAt(1)=='0') + cells[row][col].setValue(String.format("%.1f",result)); + + else + cells[row][col].setValue(String.format("%.2f",result)); } /** * Method for calculating the sum 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. + * @param endCellName The name of the cell in the lower right corner of the rectangle. * @return The sum calculated. */ - private long sum(String startCellName, String endCellName) { - // TODO implement + private double sum(String startCellName, String endCellName) { + double result = 0; - return 0; + for(int r = getRow(startCellName)-1; r0) + result *= Double.parseDouble(cells[r][c].getValue()); + else { + result = Double.parseDouble(cells[r][c].getValue()); + counter++; + } + + return result; + } + + /** + * Method for calculating the average 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 average calculated. + */ + private double average(String startCellName, String endCellName) { + double result = 0; + int counter = 0; + for(int r = getRow(startCellName)-1; r cellNames = new ArrayList<>(); + + double avg = 0; + int counter = 0; + for(int r = getRow(startCellName)-1; r copyCellNames = new ArrayList<>(cellNames); + double[] frequency = new double[cellNames.size()]; + Arrays.fill(frequency,1); + ArrayList relativeFrequency = new ArrayList<>(); + double mem = 0; + + for(int i = 0; i< cellNames.size(); i++) { + for(int t = 0; t cellNames = new ArrayList<>(); + + for(int r = getRow(startCellName)-1; r Double.parseDouble(get(cellNames.get(i)))) + result = Double.parseDouble(get(cellNames.get(i))); + } 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<>(); + + for(int r = getRow(startCellName)-1; r inputs = new ArrayList<>(); + for(int i =0; i19) + sb.append(" " + String.format("%19s", cells[r][c].getValue()).substring(0,19) + " |"); + else + 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(System.lineSeparator()); return sb.toString(); + + } -} \ No newline at end of file + + /** + * 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(" |"); + 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++) { + 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(System.lineSeparator()); + return sb.toString(); + + + } +} diff --git a/Axel/test/de/hs_mannheim/informatik/spreadsheet/SpreadsheetTest.java b/Axel/test/de/hs_mannheim/informatik/spreadsheet/SpreadsheetTest.java new file mode 100644 index 0000000..8d73a4e --- /dev/null +++ b/Axel/test/de/hs_mannheim/informatik/spreadsheet/SpreadsheetTest.java @@ -0,0 +1,198 @@ +package de.hs_mannheim.informatik.spreadsheet; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.jupiter.api.Assertions.*; + +class SpreadsheetTest { + + String[][] cells = {{"1","2"},{"2","4"},{"3","6"}}; + + @Test + void sumTest() { + + double result = 0; + + for(int r = 0; r0) + result *= Double.parseDouble(cells[r][c]); + else { + result = Double.parseDouble(cells[r][c]); + counter++; + } + assertEquals(288,result); + } + @Test + void averageTest() { + + double result = 0; + int counter = 0; + + for(int r = 0; r cellNames = new ArrayList<>(); + + double result = 0; + double avg = 0; + int counter = 0; + for(int r = 0; r copyCellNames = new ArrayList<>(cellNames); + double[] frequency = new double[cellNames.size()]; + Arrays.fill(frequency,1); + ArrayList relativeFrequency = new ArrayList<>(); + double mem = 0; + + for(int i = 0; i< cellNames.size(); i++) { + for(int t = 0; t cellNames = new ArrayList<>(); + + for(int r = 0; r< cells.length; r++) + for(int c = 0; c Double.parseDouble(cellNames.get(i))) + result = Double.parseDouble(cellNames.get(i)); + + assertEquals(1,result); + } + @Test + void maxTest() { + ArrayList cellNames = new ArrayList<>(); + + for(int r = 0; r< cells.length; r++) + for(int c = 0; c + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file