//added javadoc comments
parent
75dec01910
commit
1d841fd7be
|
@ -12,7 +12,7 @@ import java.util.Scanner;
|
||||||
public class Axel {
|
public class Axel {
|
||||||
|
|
||||||
public static void main(String[] args) throws FileNotFoundException {
|
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);
|
Scanner sc= new Scanner(System.in);
|
||||||
|
|
||||||
|
|
||||||
|
@ -62,10 +62,11 @@ public class Axel {
|
||||||
System.out.println(spr);
|
System.out.println(spr);
|
||||||
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
// System.out.println("Exit? j/n");
|
System.out.println("Exit? y/n");
|
||||||
// readInput= sc.nextLine();
|
|
||||||
|
readInput= sc.nextLine();
|
||||||
|
|
||||||
if(readInput.equals("j"))
|
if(readInput.equals("y")||readInput.equals("Y"))
|
||||||
exit=true;
|
exit=true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim.
|
* A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim.
|
||||||
|
@ -89,8 +91,49 @@ public class Spreadsheet {
|
||||||
* @return Nothing.
|
* @return Nothing.
|
||||||
* @exception IOException If path does not exist.
|
* @exception IOException If path does not exist.
|
||||||
*/
|
*/
|
||||||
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
public void readCsv(String filePath, String separator, String startCellName) throws FileNotFoundException {
|
||||||
// TODO: implement this
|
ArrayList<String> fileRows = new ArrayList<>();
|
||||||
|
Scanner sc = new Scanner(new File(filePath));
|
||||||
|
|
||||||
|
/* while (sc.hasNextLine()) {
|
||||||
|
fileRows.add(sc.nextLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ArrayList <int[]> 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
|
else
|
||||||
out.print(cell.getValue());
|
out.print(cell.getValue());
|
||||||
|
|
||||||
if (cell != row[cells.length-1])
|
if (cell != row[cells[0].length-1])
|
||||||
out.print(",");
|
out.print(",");
|
||||||
}
|
}
|
||||||
out.println();
|
out.println();
|
||||||
|
@ -188,6 +231,13 @@ public class Spreadsheet {
|
||||||
return sum;
|
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) {
|
private long prod(String startCellName, String endCellName) {
|
||||||
|
|
||||||
int startRow=0;
|
int startRow=0;
|
||||||
|
@ -214,17 +264,25 @@ public class Spreadsheet {
|
||||||
return prod;
|
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 startRow=0;
|
||||||
int startCol=0;
|
int startCol=0;
|
||||||
|
|
||||||
int endRow=0;
|
int endRow=0;
|
||||||
int endCol;
|
int endCol;
|
||||||
int sum=0;
|
long sum=0;
|
||||||
int temp;
|
long temp;
|
||||||
int part=0;
|
int part=0;
|
||||||
int avg=0;;
|
long avg=0;;
|
||||||
|
|
||||||
startRow= getRow(startCellName);
|
startRow= getRow(startCellName);
|
||||||
startCol= getCol(startCellName);
|
startCol= getCol(startCellName);
|
||||||
|
@ -245,7 +303,12 @@ public class Spreadsheet {
|
||||||
return avg;
|
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) {
|
private int min(String startCellName, String endCellName) {
|
||||||
int startRow=0;
|
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) {
|
private int max(String startCellName, String endCellName) {
|
||||||
int startRow=0;
|
int startRow=0;
|
||||||
|
@ -311,18 +379,24 @@ public class Spreadsheet {
|
||||||
return max;
|
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 startRow=0;
|
||||||
int startCol=0;
|
int startCol=0;
|
||||||
|
|
||||||
int endRow=0;
|
int endRow=0;
|
||||||
int endCol;
|
int endCol;
|
||||||
int temp;
|
long temp;
|
||||||
int part=0;
|
int part=0;
|
||||||
int avg=0;;
|
long avg=0;;
|
||||||
int dev=0;
|
long dev=0;
|
||||||
|
|
||||||
|
|
||||||
startRow= getRow(startCellName);
|
startRow= getRow(startCellName);
|
||||||
|
@ -330,7 +404,7 @@ public class Spreadsheet {
|
||||||
|
|
||||||
endRow= getRow(endCellName);
|
endRow= getRow(endCellName);
|
||||||
endCol= getCol(endCellName);
|
endCol= getCol(endCellName);
|
||||||
ArrayList<Integer> elements = new ArrayList<>();
|
ArrayList<Long> elements = new ArrayList<>();
|
||||||
|
|
||||||
avg= average(startCellName, endCellName);
|
avg= average(startCellName, endCellName);
|
||||||
|
|
||||||
|
@ -382,7 +456,7 @@ public class Spreadsheet {
|
||||||
int cnt=0;
|
int cnt=0;
|
||||||
// TODO implement
|
// 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()
|
while (m.find()) { // m.find() must always be used before m.group()
|
||||||
String s = m.group();
|
String s = m.group();
|
||||||
|
|
||||||
|
@ -449,7 +523,7 @@ public class Spreadsheet {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
sb.append(" ");
|
sb.append(" ");
|
||||||
for (int i = 0; i < cells.length; i++) {
|
for (int i = 0; i < cells[0].length; i++) {
|
||||||
sb.append(" " + (char)('A'+ 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) {
|
public String getStartCellName(String formula) {
|
||||||
|
|
||||||
|
@ -488,18 +566,19 @@ public class Spreadsheet {
|
||||||
startCell=formula.substring(a+1, a+4);
|
startCell=formula.substring(a+1, a+4);
|
||||||
}else startCell=formula.substring(a+1, a+3);
|
}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);
|
System.out.println(startCell);
|
||||||
|
|
||||||
return 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) {
|
public String getEndCellName(String formula) {
|
||||||
|
|
||||||
String endCell="";
|
String endCell="";
|
||||||
|
@ -526,16 +605,6 @@ public class Spreadsheet {
|
||||||
System.out.println(endCell);
|
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;
|
return endCell;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue