diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index dd9633f..3bf30a0 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -12,7 +12,7 @@ import java.util.Scanner; public class Axel { public static void main(String[] args) throws FileNotFoundException { - Spreadsheet spr = new Spreadsheet(10,10); + Spreadsheet spr = new Spreadsheet(10,20); Scanner sc= new Scanner(System.in); @@ -62,10 +62,11 @@ public class Axel { System.out.println(spr); System.out.println(); - // System.out.println("Exit? j/n"); - // readInput= sc.nextLine(); + System.out.println("Exit? y/n"); + + readInput= sc.nextLine(); - if(readInput.equals("j")) + if(readInput.equals("y")||readInput.equals("Y")) exit=true; } diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index daa772e..d76c893 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -1,12 +1,14 @@ package de.hs_mannheim.informatik.spreadsheet; import java.io.FileNotFoundException; +import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.ArrayList; +import java.util.Scanner; /** * A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim. @@ -89,8 +91,49 @@ public class Spreadsheet { * @return Nothing. * @exception IOException If path does not exist. */ - public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException { - // TODO: implement this + public void readCsv(String filePath, String separator, String startCellName) throws FileNotFoundException { + ArrayList fileRows = new ArrayList<>(); + Scanner sc = new Scanner(new File(filePath)); + +/* while (sc.hasNextLine()) { + fileRows.add(sc.nextLine()); + } + + + ArrayList formulas = new ArrayList<>(); + + for (int rowI = 0; rowI < fileRows.size(); rowI++) { + String row = fileRows.get(rowI); + String[] cells = row.split(separator); + + for (int colI = 0; colI < cells.length; colI++) { + String cellContent = cells[colI].toUpperCase(); + + if (cellContent.startsWith("=")){ + formulas.add(new int[]{rowI, colI}); + } + else { + put(rowI, colI, cellContent); + } + + } + } + + for (int[] formulaPos : formulas){ + int rowI = formulaPos[0]; + int colI = formulaPos[1]; + System.out.printf("Formula at %d-%d %n", rowI, colI); + + String row = fileRows.get(rowI); + String formulaToFill = row.split(separator)[colI]; + + put(rowI, colI, formulaToFill); + } + + sc.close(); + + */ + } /** @@ -109,7 +152,7 @@ public class Spreadsheet { else out.print(cell.getValue()); - if (cell != row[cells.length-1]) + if (cell != row[cells[0].length-1]) out.print(","); } out.println(); @@ -188,6 +231,13 @@ public class Spreadsheet { return sum; } + /** + * Method for calculating the product 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 ofODO the rectangle. + * @return The product calculated. + */ + private long prod(String startCellName, String endCellName) { int startRow=0; @@ -214,17 +264,25 @@ public class Spreadsheet { return prod; } - private int average(String startCellName, String endCellName) { + + /** + * 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 ofODO the rectangle. + * @return The average calculated. + */ + + private long average(String startCellName, String endCellName) { int startRow=0; int startCol=0; int endRow=0; int endCol; - int sum=0; - int temp; + long sum=0; + long temp; int part=0; - int avg=0;; + long avg=0;; startRow= getRow(startCellName); startCol= getCol(startCellName); @@ -245,7 +303,12 @@ public class Spreadsheet { return avg; } - //Method to calculate the minimum + /** + * Method for calculating the minimum of a rectangular block of cells + * @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 ofODO the rectangle. + * @return The minimum. + */ private int min(String startCellName, String endCellName) { int startRow=0; @@ -279,7 +342,12 @@ public class Spreadsheet { } - //calculate maximum + /** + * Method for calculating the maximum of a rectangular block of cells + * @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 ofODO the rectangle. + * @return The maximum. + */ private int max(String startCellName, String endCellName) { int startRow=0; @@ -311,18 +379,24 @@ public class Spreadsheet { return max; } + /** + * Method for calculating the standard deviation of a rectangular block of cells + * @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 ofODO the rectangle. + * @return The standard deviation calculated. + */ - private int stDev(String startCellName, String endCellName) { + private long stDev(String startCellName, String endCellName) { int startRow=0; int startCol=0; int endRow=0; int endCol; - int temp; + long temp; int part=0; - int avg=0;; - int dev=0; + long avg=0;; + long dev=0; startRow= getRow(startCellName); @@ -330,7 +404,7 @@ public class Spreadsheet { endRow= getRow(endCellName); endCol= getCol(endCellName); - ArrayList elements = new ArrayList<>(); + ArrayList elements = new ArrayList<>(); avg= average(startCellName, endCellName); @@ -382,7 +456,7 @@ public class Spreadsheet { int cnt=0; // TODO implement - // uncomment the following to see an example how the elements of a formula can be accessed + while (m.find()) { // m.find() must always be used before m.group() String s = m.group(); @@ -449,7 +523,7 @@ public class Spreadsheet { StringBuilder sb = new StringBuilder(); sb.append(" "); - for (int i = 0; i < cells.length; i++) { + for (int i = 0; i < cells[0].length; i++) { sb.append(" " + (char)('A'+ i) + " | "); } @@ -467,8 +541,12 @@ public class Spreadsheet { } + /** + * Method to get the correct start cell name if it has more then 1 digit + * @param formula The formula in the cell + * @return name of the startCell + */ - //methods to get correct cell names if they have more then 1 digit public String getStartCellName(String formula) { @@ -488,18 +566,19 @@ public class Spreadsheet { startCell=formula.substring(a+1, a+4); }else startCell=formula.substring(a+1, a+3); - //try to get the cell name for each formula not only SUMS - -/* if(Character.isDigit(formula.charAt(8))) { - startCell= formula.substring(6, 9); - }else startCell= formula.substring(6, 8); - */ + System.out.println(startCell); return startCell; } + /** + * Method to get the correct end cell name if it has more then 1 digit + * @param formula The formula in the cell + * @return name of the endCell + */ + public String getEndCellName(String formula) { String endCell=""; @@ -526,16 +605,6 @@ public class Spreadsheet { System.out.println(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; }