2023-12-12 15:00:07 +01:00
|
|
|
package de.hs_mannheim.informatik.spreadsheet;
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
2023-12-14 08:31:20 +01:00
|
|
|
import java.io.PrintWriter;
|
2023-12-12 15:00:07 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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) {
|
2024-01-05 11:15:23 +01:00
|
|
|
|
2023-12-12 15:00:07 +01:00
|
|
|
// TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns
|
2024-01-05 11:15:23 +01:00
|
|
|
if (rows>99){
|
|
|
|
rows=99;
|
|
|
|
}
|
|
|
|
if (cols>26) {
|
|
|
|
cols=26;
|
|
|
|
}
|
|
|
|
|
2023-12-12 15:00:07 +01:00
|
|
|
cells = new Cell[rows][cols];
|
|
|
|
|
|
|
|
for (int r = 0; r < rows; r++)
|
|
|
|
for (int c = 0; c < cols; c++)
|
|
|
|
cells[r][c] = new Cell();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
// retrieve or change values of cells
|
|
|
|
|
|
|
|
private String get(int row, int col) {
|
|
|
|
return cells[row][col].getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String get(String cellName) {
|
|
|
|
cellName = cellName.toUpperCase();
|
|
|
|
return get(getRow(cellName), getCol(cellName));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void put(int row, int col, String value) {
|
|
|
|
if (!value.startsWith("="))
|
|
|
|
cells[row][col].setValue(value);
|
|
|
|
else {
|
|
|
|
cells[row][col].setFormula(value);
|
|
|
|
evaluateCell(row, col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void put(String cellName, String value) {
|
|
|
|
cellName = cellName.toUpperCase();
|
|
|
|
put(getRow(cellName), getCol(cellName), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getCol(String cellName) {
|
|
|
|
return cellName.charAt(0) - 'A';
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getRow(String cellName) {
|
|
|
|
return cellName.charAt(1) - '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
// business logic
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @return Nothing.
|
|
|
|
* @exception IOException If path does not exist.
|
|
|
|
*/
|
|
|
|
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
|
|
|
// TODO: implement this
|
|
|
|
}
|
|
|
|
|
2023-12-14 08:31:20 +01:00
|
|
|
/**
|
|
|
|
* A method for saving data to a CSV file.
|
|
|
|
* @param path The file to write.
|
|
|
|
* @return Nothing.
|
|
|
|
* @exception IOException If path does not exist.
|
|
|
|
*/
|
|
|
|
public void saveCsv(String path) throws FileNotFoundException {
|
|
|
|
PrintWriter out = new PrintWriter(path);
|
|
|
|
|
|
|
|
for (Cell[] row : cells) {
|
|
|
|
for (Cell cell : row) {
|
|
|
|
if (!cell.getFormula().isEmpty())
|
|
|
|
out.print("=" + cell.getFormula());
|
|
|
|
else
|
|
|
|
out.print(cell.getValue());
|
|
|
|
|
|
|
|
if (cell != row[cells.length-1])
|
|
|
|
out.print(",");
|
|
|
|
}
|
|
|
|
out.println();
|
|
|
|
}
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
2023-12-12 15:00:07 +01:00
|
|
|
/**
|
|
|
|
* This method does the actual evaluation/calcluation of a specific cell
|
|
|
|
* @param cellName the name of the cell to be evaluated
|
|
|
|
* @return Nothing.
|
|
|
|
*/
|
|
|
|
private void evaluateCell(int row, int col) {
|
|
|
|
String formula = cells[row][col].getFormula();
|
|
|
|
String result = "";
|
|
|
|
|
|
|
|
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
|
|
|
|
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
|
|
|
result = "TODO"; // TODO
|
|
|
|
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
|
2023-12-14 08:54:48 +01:00
|
|
|
else if (!formula.isEmpty()) {
|
|
|
|
try {
|
|
|
|
result = "" + calculate(formula);
|
|
|
|
} catch(ArithmeticException ae) {
|
|
|
|
result = "exc.";
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 15:00:07 +01:00
|
|
|
|
|
|
|
cells[row][col].setValue("" + 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.
|
|
|
|
* @return The sum calculated.
|
|
|
|
*/
|
|
|
|
private long sum(String startCellName, String endCellName) {
|
2024-01-05 11:58:29 +01:00
|
|
|
int sum=0;
|
|
|
|
for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) {
|
|
|
|
for(int j=startCellName.charAt(1)-'1'; j<=endCellName.charAt(1)-'1'; j++) {
|
|
|
|
if(cells[i][j].isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sum+=Integer.parseInt(cells[j][i].getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sum;
|
2023-12-12 15:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method calculates the result of a "normal" algebraic expression. It only needs to support
|
|
|
|
* expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus,
|
|
|
|
* minus, times, split only. An expression always starts with either a number or a cell name. If it
|
|
|
|
* continues, it is guaranteed that this is followed by an operator and either a number or a
|
|
|
|
* cell name again. It is NOT required to implement dot before dash or parentheses in formulas.
|
|
|
|
* @param formula The expression to be evaluated.
|
|
|
|
* @return The result calculated.
|
|
|
|
*/
|
2023-12-14 08:54:48 +01:00
|
|
|
private long calculate(String formula) throws ArithmeticException {
|
2023-12-12 15:00:07 +01:00
|
|
|
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
|
|
|
|
|
|
|
|
long res = 0;
|
2023-12-14 08:31:20 +01:00
|
|
|
|
2023-12-12 15:00:07 +01:00
|
|
|
// 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();
|
|
|
|
if (!s.isEmpty()) {
|
|
|
|
System.out.println(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
sb.append(" ");
|
2024-01-05 11:15:23 +01:00
|
|
|
for (int i = 0; i < cells[0].length; i++) {
|
2023-12-12 15:00:07 +01:00
|
|
|
sb.append(" " + (char)('A'+ i) + " | ");
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc = 1;
|
|
|
|
for (Cell[] r : cells) {
|
|
|
|
sb.append(System.lineSeparator());
|
|
|
|
sb.append(String.format("%2s", rc++) + ": ");
|
|
|
|
|
|
|
|
for (Cell c : r) {
|
|
|
|
sb.append(c + " | ");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|