1
0
Fork 0

Some method descriptions, toString/seeFormula updated, readCsv fix.

pull/1/head
selim 2023-12-27 19:14:30 +01:00
parent 2b95af08d1
commit 347d36a669
2 changed files with 114 additions and 35 deletions

View File

@ -1,10 +1,9 @@
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
=2,,,=SUMME(A1:A6),,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,

1 =2 =SUMME(A1:A6)
2
3
4
5
6
7
8
9

View File

@ -110,7 +110,7 @@ public class Spreadsheet {
for (int r = getRow(startCellName)-1; r < memList.size(); r++)
for (int c = getCol(startCellName)-1; c < memList.get(0).length(); c++)
if(c<memArr[r].length)
put(r, c, memArr[r][c]);
cells[r][c].setFormula(memArr[r][c]);
sc.close();
}
@ -239,7 +239,15 @@ public class Spreadsheet {
}
if(!cells[row][col].getFormula().isEmpty())
cells[row][col].setValue(String.format("%.1f",result));
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));
}
/**
@ -259,6 +267,14 @@ public class Spreadsheet {
return result;
}
/**
* 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 of the rectangle.
* @return The product calculated.
*/
private double product(String startCellName, String endCellName) {
double result = 0;
int counter = 0;
@ -275,6 +291,14 @@ public class Spreadsheet {
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;
@ -288,6 +312,14 @@ public class Spreadsheet {
result /= counter;
return result;
}
/**
* Method for calculating the standardDeviation 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 standardDeviation calculated.
*/
private double standardDeviation(String startCellName, String endCellName){
ArrayList<String> cellNames = new ArrayList<>();
@ -344,6 +376,14 @@ public class Spreadsheet {
return Math.sqrt(mem);
//standardDeviation formula
}
/**
* Method to find out the lowest 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 lowest number.
*/
private double min(String startCellName, String endCellName){
ArrayList<String> cellNames = new ArrayList<>();
@ -363,6 +403,14 @@ public class Spreadsheet {
} 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<String> cellNames = new ArrayList<>();
@ -429,27 +477,49 @@ public class Spreadsheet {
return result;
}
private double evaluateOperator(double result, double currentOperand, String currentOperator) {
/**
* This method does the actual calculation with the current operator,
* the left and right operands provided by the calculate method.
*
* @param leftOperand The first operand used to calculate.
* @param rightOperand The second operand used to calculate.
* @param currentOperator The current operator used to calculate.
* @return The partial result calculated.
*/
private double evaluateOperator(double leftOperand, double rightOperand, String currentOperator) {
switch (currentOperator) {
case "+":
return (result + currentOperand);
return (leftOperand + rightOperand);
case "-":
return (result - currentOperand);
return (leftOperand - rightOperand);
case "*":
return (result * currentOperand);
return (leftOperand * rightOperand);
case "/":
return (result / currentOperand);
return (leftOperand / rightOperand);
default:
throw new IllegalArgumentException("Invalid operator: " + currentOperator);
}
}
///--- ui methods
/**
* This method updates all cells value by provided formula.
*
* @return nothing.
*/
public void updateSpreadsheet() {
for (int r = 0; r < cells.length; r++)
for (int c = 0; c < cells[r].length; c++)
evaluateCell(r, c);
}
/**
* This method scans and evaluates the user input.
*
* @return The boolean true, when the programm is finished by the user input.
*/
public boolean cellInput() throws FileNotFoundException {
Scanner cs = new Scanner(System.in);
@ -491,36 +561,42 @@ public class Spreadsheet {
inputs.clear();
return false;
}
/**
* This method outputs the cell spreadsheet with all the values.
*
* @return nothing.
*/
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(System.lineSeparator());
sb.append(System.lineSeparator());
sb.append(" |");
sb.append(" |");
for (int i = 0; i < cells[0].length; i++) {
sb.append(" " + (char) ('A' + 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("------");
sb.append(System.lineSeparator());
sb.append(String.format(" " + "%2s", rc++) + " |");
sb.append(String.format(" " + "%2s", rc++) + " |");
for (int c = 0; c < cells[r].length; c++) {
if((cells[r][c].getValue()).length()>13)
sb.append(" " + String.format("%13s", cells[r][c].getValue()).substring(0,13) + " |");
if((cells[r][c].getValue()).length()>19)
sb.append(" " + String.format("%19s", cells[r][c].getValue()).substring(0,19) + " |");
else
sb.append(" " + String.format("%13s", cells[r][c].getValue()) + " |");
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("------");
sb.append(System.lineSeparator());
@ -528,36 +604,40 @@ public class Spreadsheet {
}
/**
* 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(" |");
sb.append(" |");
for (int i = 0; i < cells[0].length; i++) {
sb.append(" " + (char) ('A' + 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("------");
sb.append(System.lineSeparator());
sb.append(String.format(" " + "%2s", rc++) + " |");
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()) + " |");
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("------");
sb.append(System.lineSeparator());
@ -565,4 +645,4 @@ public class Spreadsheet {
}
}
}