Spreadsheet toString updated.
parent
ada8f538a2
commit
6f6cb59f08
|
@ -17,6 +17,7 @@ public class Spreadsheet {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor that creates a Spreadsheet of size rows * cols.
|
* Constructor that creates a Spreadsheet of size rows * cols.
|
||||||
|
*
|
||||||
* @param rows number of rows
|
* @param rows number of rows
|
||||||
* @param cols number of columns
|
* @param cols number of columns
|
||||||
*/
|
*/
|
||||||
|
@ -70,11 +71,12 @@ public class Spreadsheet {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A method for reading in data from a CSV file.
|
* 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 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 starCellName The upper left cell where data from the CSV file should be inserted.
|
||||||
* @return Nothing.
|
* @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 {
|
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
||||||
// TODO: implement this
|
// TODO: implement this
|
||||||
|
@ -82,9 +84,10 @@ public class Spreadsheet {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A method for saving data to a CSV file.
|
* A method for saving data to a CSV file.
|
||||||
|
*
|
||||||
* @param path The file to write.
|
* @param path The file to write.
|
||||||
* @return Nothing.
|
* @return Nothing.
|
||||||
* @exception IOException If path does not exist.
|
* @throws IOException If path does not exist.
|
||||||
*/
|
*/
|
||||||
public void saveCsv(String path) throws FileNotFoundException {
|
public void saveCsv(String path) throws FileNotFoundException {
|
||||||
PrintWriter out = new PrintWriter(path);
|
PrintWriter out = new PrintWriter(path);
|
||||||
|
@ -96,7 +99,7 @@ public class Spreadsheet {
|
||||||
else
|
else
|
||||||
out.print(cell.getValue());
|
out.print(cell.getValue());
|
||||||
|
|
||||||
if (cell != row[cells[0].length-1])
|
if (cell != row[cells[0].length - 1])
|
||||||
out.print(",");
|
out.print(",");
|
||||||
}
|
}
|
||||||
out.println();
|
out.println();
|
||||||
|
@ -107,6 +110,7 @@ public class Spreadsheet {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method does the actual evaluation/calcluation of a specific cell
|
* This method does the actual evaluation/calcluation of a specific cell
|
||||||
|
*
|
||||||
* @param cellName the name of the cell to be evaluated
|
* @param cellName the name of the cell to be evaluated
|
||||||
* @return Nothing.
|
* @return Nothing.
|
||||||
*/
|
*/
|
||||||
|
@ -129,7 +133,7 @@ public class Spreadsheet {
|
||||||
else if (!formula.isEmpty()) {
|
else if (!formula.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
result = "" + calculate(formula);
|
result = "" + calculate(formula);
|
||||||
} catch(ArithmeticException ae) {
|
} catch (ArithmeticException ae) {
|
||||||
result = "exc.";
|
result = "exc.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,8 +143,9 @@ public class Spreadsheet {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method for calculating the sum of a rectangular block of cells, such as from A1 to B3.
|
* 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 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.
|
* @return The sum calculated.
|
||||||
*/
|
*/
|
||||||
private long sum(String startCellName, String endCellName) {
|
private long sum(String startCellName, String endCellName) {
|
||||||
|
@ -155,6 +160,7 @@ public class Spreadsheet {
|
||||||
* minus, times, split only. An expression always starts with either a number or a cell name. If it
|
* 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
|
* 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.
|
* cell name again. It is NOT required to implement dot before dash or parentheses in formulas.
|
||||||
|
*
|
||||||
* @param formula The expression to be evaluated.
|
* @param formula The expression to be evaluated.
|
||||||
* @return The result calculated.
|
* @return The result calculated.
|
||||||
*/
|
*/
|
||||||
|
@ -181,22 +187,34 @@ public class Spreadsheet {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
sb.append(" ");
|
sb.append(System.lineSeparator());
|
||||||
|
sb.append(" |");
|
||||||
for (int i = 0; i < cells[0].length; i++) {
|
for (int i = 0; i < cells[0].length; i++) {
|
||||||
sb.append(" " + (char)('A'+ i) + " | ");
|
sb.append(" " + (char) ('A' + i) + " |");
|
||||||
}
|
}
|
||||||
|
|
||||||
int rc = 1;
|
int rc = 1;
|
||||||
for (Cell[] r : cells) {
|
for (Cell[] r : cells) {
|
||||||
sb.append(System.lineSeparator());
|
sb.append(System.lineSeparator());
|
||||||
sb.append(String.format("%2s", rc++) + ": ");
|
for (int i = 0; i < cells[0].length; i++) {
|
||||||
|
sb.append("----------------");
|
||||||
|
}
|
||||||
|
sb.append("------");
|
||||||
|
sb.append(System.lineSeparator());
|
||||||
|
sb.append(String.format(" " + "%2s", rc++) + " |");
|
||||||
|
|
||||||
for (Cell c : r) {
|
for (Cell c : r) {
|
||||||
sb.append(c + " | ");
|
sb.append(" " + String.format("%13s", c) + " |");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
sb.append(System.lineSeparator());
|
||||||
|
for (int i = 0; i < cells[0].length; i++) {
|
||||||
|
sb.append("----------------");
|
||||||
|
}
|
||||||
|
sb.append("------");
|
||||||
|
sb.append(System.lineSeparator());
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue