forked from hummel/PR1-Spreadsheet
parent
fe45bcf50c
commit
ba4469b358
|
@ -1,5 +1,6 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
@ -7,8 +8,7 @@ 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.List;
|
import java.util.List;
|
||||||
|
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.
|
||||||
* One aspect worth mentioning is that it only supports long numbers, not doubles.
|
* One aspect worth mentioning is that it only supports long numbers, not doubles.
|
||||||
|
@ -17,8 +17,7 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public class Spreadsheet {
|
public class Spreadsheet {
|
||||||
Cell[][] cells;
|
Cell[][] cells;
|
||||||
|
/*
|
||||||
/**
|
|
||||||
* 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
|
||||||
|
@ -34,6 +33,23 @@ public class Spreadsheet {
|
||||||
for (int c = 0; c < colWithMax26; c++)
|
for (int c = 0; c < colWithMax26; c++)
|
||||||
cells[r][c] = new Cell();
|
cells[r][c] = new Cell();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Sets a Spreadsheet of size rows * cols.
|
||||||
|
* @param rows number of rows
|
||||||
|
* @param cols number of columns
|
||||||
|
*/
|
||||||
|
public void changeSize(int rows, int cols) {
|
||||||
|
cells = new Cell[0][0];
|
||||||
|
|
||||||
|
int rowWithMax99= Math.min(rows, 99);
|
||||||
|
int colWithMax26= Math.min(cols, 26);
|
||||||
|
|
||||||
|
cells = new Cell[rowWithMax99][colWithMax26];
|
||||||
|
|
||||||
|
for (int r = 0; r < rowWithMax99; r++)
|
||||||
|
for (int c = 0; c < colWithMax26; c++)
|
||||||
|
cells[r][c] = new Cell();
|
||||||
|
}
|
||||||
|
|
||||||
// -----
|
// -----
|
||||||
/*
|
/*
|
||||||
|
@ -55,7 +71,7 @@ public class Spreadsheet {
|
||||||
cellName = cellName.toUpperCase();
|
cellName = cellName.toUpperCase();
|
||||||
return get(getRow(cellName), getCol(cellName));
|
return get(getRow(cellName), getCol(cellName));
|
||||||
}
|
}
|
||||||
private void put(int row, int col, String value) {
|
void put(int row, int col, String value) {
|
||||||
if (!value.startsWith("="))
|
if (!value.startsWith("="))
|
||||||
cells[row][col].setValue(value);
|
cells[row][col].setValue(value);
|
||||||
else {
|
else {
|
||||||
|
@ -67,10 +83,10 @@ public class Spreadsheet {
|
||||||
cellName = cellName.toUpperCase();
|
cellName = cellName.toUpperCase();
|
||||||
put(getRow(cellName), getCol(cellName), value);
|
put(getRow(cellName), getCol(cellName), value);
|
||||||
}
|
}
|
||||||
private int getCol(String cellName) {
|
public int getCol(String cellName) {
|
||||||
return cellName.charAt(0) - 'A';
|
return cellName.charAt(0) - 'A';
|
||||||
}
|
}
|
||||||
private int getRow(String cellName) {
|
public int getRow(String cellName) {
|
||||||
if (cellName.length()==3) {
|
if (cellName.length()==3) {
|
||||||
int Row = 0;
|
int Row = 0;
|
||||||
Row +=((cellName.charAt(1)-'0')*10);
|
Row +=((cellName.charAt(1)-'0')*10);
|
||||||
|
@ -86,13 +102,34 @@ 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 path The file to read.
|
||||||
* @param separator The char used to split up the input, e.g. a comma or a semicolon.
|
* @param startCellName 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.
|
* @exception IOException If path does not exist.
|
||||||
*/
|
*/
|
||||||
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
public void readCsv(String path, String startCellName) throws FileNotFoundException {
|
||||||
// TODO: implement this
|
Scanner csvIn = new Scanner(new File(path));
|
||||||
|
int index= 0;
|
||||||
|
|
||||||
|
while(csvIn.hasNextLine()){
|
||||||
|
String line =csvIn.nextLine();
|
||||||
|
String all="";
|
||||||
|
int filled=0;
|
||||||
|
for(int i = 0;i<line.length();i++) {
|
||||||
|
if(line.charAt(i)==',') {
|
||||||
|
if(!all.isBlank()) {
|
||||||
|
put(index,i-filled,all);
|
||||||
|
all="";
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
filled++;
|
||||||
|
all+=line.charAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
csvIn.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -169,7 +206,7 @@ public class Spreadsheet {
|
||||||
* @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) {
|
public long sum(String startCellName, String endCellName) {
|
||||||
long res = 0;
|
long res = 0;
|
||||||
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
|
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
|
||||||
|
@ -186,7 +223,7 @@ public class Spreadsheet {
|
||||||
* @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 product calculated.
|
* @return The product calculated.
|
||||||
*/
|
*/
|
||||||
private long prod(String startCellName, String endCellName) {
|
public long prod(String startCellName, String endCellName) {
|
||||||
long res = 1;
|
long res = 1;
|
||||||
boolean anyNumbers= false;
|
boolean anyNumbers= false;
|
||||||
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
@ -208,7 +245,7 @@ public class Spreadsheet {
|
||||||
* @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 Mid Value calculated.
|
* @return The Mid Value calculated.
|
||||||
*/
|
*/
|
||||||
private long mid(String startCellName, String endCellName) {
|
public long mid(String startCellName, String endCellName) {
|
||||||
long res = 0;
|
long res = 0;
|
||||||
int Numbers= 0;
|
int Numbers= 0;
|
||||||
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
@ -228,7 +265,7 @@ public class Spreadsheet {
|
||||||
* @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 Standard deviation calculated.
|
* @return The Standard deviation calculated.
|
||||||
*/
|
*/
|
||||||
private long stabw(String startCellName, String endCellName) {
|
public long stabw(String startCellName, String endCellName) {
|
||||||
long res=0;
|
long res=0;
|
||||||
long midSum=0;
|
long midSum=0;
|
||||||
//mid(startCellName,endCellName)
|
//mid(startCellName,endCellName)
|
||||||
|
@ -262,7 +299,7 @@ public class Spreadsheet {
|
||||||
* @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 minimum value calculated.
|
* @return The minimum value calculated.
|
||||||
*/
|
*/
|
||||||
private long min(String startCellName, String endCellName) {
|
public long min(String startCellName, String endCellName) {
|
||||||
long res = 0;
|
long res = 0;
|
||||||
List<Long> args = new ArrayList<>();
|
List<Long> args = new ArrayList<>();
|
||||||
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
@ -288,7 +325,7 @@ public class Spreadsheet {
|
||||||
* @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 maximum value calculated.
|
* @return The maximum value calculated.
|
||||||
*/
|
*/
|
||||||
private long max(String startCellName, String endCellName) {
|
public long max(String startCellName, String endCellName) {
|
||||||
long res = 0;
|
long res = 0;
|
||||||
List<Long> args = new ArrayList<>();
|
List<Long> args = new ArrayList<>();
|
||||||
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
|
Loading…
Reference in New Issue