Compare commits

...

21 Commits
main ... main

Author SHA1 Message Date
Anastasia Kisner ff7f73a413 angepasster readCsv() Aufruf 2024-01-09 13:42:28 +01:00
Anastasia Kisner 4cb618729a Tests zu den Formeln ergänzt 2024-01-09 13:39:27 +01:00
Anastasia Kisner d7dbffeff8 readCSV() Methode ergänzt 2024-01-09 13:38:45 +01:00
Anastasia Kisner c980006f02 stabw() Methode ergänzt 2024-01-07 17:02:40 +01:00
Anastasia Kisner a0117ae961 counterOfCellsWithValue() Methode ergänzt. Angepasste mit() Methode 2024-01-07 16:38:23 +01:00
Anastasia Kisner 111b4a36eb min() und max() Methode ergänzt 2024-01-06 20:36:37 +01:00
Anastasia Kisner 10f0aef19a mit() Methode ergänzt 2024-01-06 20:25:15 +01:00
Anastasia Kisner bbe478ff81 prod() Methode ergänzt 2024-01-06 20:13:02 +01:00
Anastasia Kisner bbbaf7473a sum() Methode ergänzt 2024-01-06 19:02:13 +01:00
Anastasia Kisner 44e9e9bc9c Methoden zu den Formeln deklariert. In evaluateCell() Cell references für two-digits ermöglicht 2024-01-06 15:30:59 +01:00
Anastasia Kisner d3326a5c3b Angepasste UI 2024-01-06 13:10:12 +01:00
Anastasia Kisner b706e537c8 unnötige prints entfernt 2024-01-05 20:44:07 +01:00
Anastasia Kisner fa1dc30c27 jUnits zu Calculate() Methode hinzugefügt 2024-01-05 20:43:31 +01:00
Anastasia Kisner 0a2c6c0514 unnötiges import entfernt 2024-01-05 19:20:03 +01:00
Anastasia Kisner a3da7cf436 calculate() Methode ergänzt 2024-01-05 19:18:41 +01:00
Anastasia Kisner 7472ee85e8 getRow Methode um zweistellige Zahlen erweitert 2024-01-04 17:40:21 +01:00
Anastasia Kisner c66c40e340 Klammer an die richtige Stelle in e.g. in UI hinzugefügt 2023-12-28 01:00:35 +01:00
Anastasia Kisner 4988308c57 Klammer in e.g. in UI hinzugefügt 2023-12-28 00:59:38 +01:00
Anastasia Kisner 85948394c1 e.g. in UI hinzugefügt 2023-12-28 00:57:55 +01:00
Anastasia Kisner df3bb6b59b UI Loop hinzugefügt. Values können ins Spreadsheet hinzugefügt werden + Path erstellt 2023-12-28 00:35:11 +01:00
Anastasia Kisner c6ce44dac8 Geänderte Bugs 2023-12-23 23:14:16 +01:00
8 changed files with 483 additions and 45 deletions

8
.idea/.gitignore vendored 100644
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

5
.idea/misc.xml 100644
View File

@ -0,0 +1,5 @@
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/PR1-Spreadsheet.iml" filepath="$PROJECT_DIR$/PR1-Spreadsheet.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml 100644
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -1,6 +1,7 @@
package de.hs_mannheim.informatik.spreadsheet;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
@ -11,19 +12,43 @@ public class Axel {
public static void main(String[] args) throws FileNotFoundException {
Spreadsheet spr = new Spreadsheet(10,10);
spr.readCsv("C:/Users/kisne/IdeaProjects/PR1-Spreadsheet/tmp/test.csv", ",");
Scanner keyboard = new Scanner(System.in);
spr.put("A3", "123");
spr.put("A2", "1");
spr.put("A2", "3");
spr.put("A5", "5");
spr.put("B8", "6");
spr.put("A8", "=Summe(A5:D8)");
spr.put("J7", "=a2+2");
spr.put("J8", "=a2+J7*4");
spr.put("B9", "=41+A2");
spr.put("J5", "=7*6");
spr.put("J6", "=3/2");
System.out.println(spr);
spr.saveCsv("/tmp/test.csv");
while(true){
System.out.print("Please enter the cell name (e.g. D8, G5): ");
String cellName = keyboard.nextLine();
System.out.print("Please enter your value or your formula (starting with '='): ");
String cellValue = keyboard.nextLine();
spr.put(cellName, cellValue);
System.out.println(spr);
System.out.print("Do you want to end the program? Y/N: ");
if(keyboard.nextLine().equalsIgnoreCase("Y")){
break;
}
}
spr.saveCsv("C:/Users/kisne/IdeaProjects/PR1-Spreadsheet/tmp/test.csv");
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
}
}

View File

@ -1,11 +1,17 @@
package de.hs_mannheim.informatik.spreadsheet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;
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.
@ -20,16 +26,18 @@ public class Spreadsheet {
* @param rows number of rows
* @param cols number of columns
*/
public Spreadsheet(int rows, int cols) {
// TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns
public Spreadsheet(int rows, int cols) throws IllegalArgumentException {
if(rows < 1 || rows > 99 || cols < 1 || cols > 26){
throw new IllegalArgumentException("Range in rows 1-99; Range in cols 1-26");
}else {
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
@ -62,6 +70,8 @@ public class Spreadsheet {
}
private int getRow(String cellName) {
if(cellName.length()==3){
return Integer.parseInt(cellName.substring(1))-1;}
return cellName.charAt(1) - '1';
}
@ -72,18 +82,44 @@ public class Spreadsheet {
* 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
public void readCsv(String path, String separator) throws FileNotFoundException {
ArrayList<String> lines = new ArrayList<>();
Scanner scan = new Scanner(new File(path));
while (scan.hasNextLine()) {
lines.add(scan.nextLine());
}
String[] linesArray = lines.toArray(new String[lines.size()]);
ArrayList<int[]> values = new ArrayList<>();
for(int i = 0; i < linesArray.length; i++) {
//System.out.println(linesArray[i]);
String[] zwischenSchritt = linesArray[i].split(separator);
for (int j = 0; j < zwischenSchritt.length; j++) {
System.out.println(zwischenSchritt[j]);
if(zwischenSchritt[j].startsWith("=")){
values.add(new int[]{i, j});
}else{
this.put(i, j, zwischenSchritt[j]);
}
}
for(int[] cell : values){
String[] value = linesArray[cell[0]].split(",");
String formula = value[cell[1]];
this.put(cell[0], cell[1], formula);
}
scan.close();
}
}
/**
* 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 {
@ -96,7 +132,7 @@ public class Spreadsheet {
else
out.print(cell.getValue());
if (cell != row[cells.length-1])
if (cell != row[cells[0].length-1])
out.print(",");
}
out.println();
@ -107,25 +143,27 @@ public class Spreadsheet {
/**
* This method does the actual evaluation/calcluation of a specific cell
* @param cellName the name of the cell to be evaluated
* @return Nothing.
* @param row the row of the cell to be evaluated
* @param col the col of the cell to be evaluated
*/
private void evaluateCell(int row, int col) {
String formula = cells[row][col].getFormula();
String result = "";
int colon = formula.indexOf(':');
String endSubstring = formula.substring((colon + 1), (formula.length()-1));
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
result = "" + sum(formula.substring(6, colon), endSubstring);
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
result = "TODO"; // TODO
result = "" + prod(formula.substring(8, colon), endSubstring);
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
result = "TODO"; // TODO
result = "" + mit(formula.substring(11, colon), endSubstring);
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
result = "" + stabw(formula.substring(6, colon), endSubstring);
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> kleinster Wert
result = "" + min(formula.substring(4, colon), endSubstring);
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert
result = "" + max(formula.substring(4, colon), endSubstring);
else if (!formula.isEmpty()) {
try {
result = "" + calculate(formula);
@ -137,6 +175,27 @@ public class Spreadsheet {
cells[row][col].setValue("" + result);
}
/**
* Method for calculating the amount of cells with values inside 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 counted cells with values calculated.
*/
private long counterOfCellsWithValue(String startCellName, String endCellName){
long counter = 0;
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
String value = get((row - 1), (col - 'A'));
if(!value.isEmpty()){
counter++;
}
}
}
return counter;
}
/**
* 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.
@ -144,10 +203,141 @@ public class Spreadsheet {
* @return The sum calculated.
*/
private long sum(String startCellName, String endCellName) {
// TODO implement
long result = 0;
return 0;
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
String value = get((row - 1), (col - 'A')); //"-1" because we start with 1 instead of 0. "-A" because A in ascii is 65, but we start with 0
if(!value.isEmpty()){
result += Long.parseLong(value);
}
}
}
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 long prod(String startCellName, String endCellName) {
long result = 0;
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
String value = get((row - 1), (col - 'A'));
System.out.println(value);
if(result == 0){
result = Long.parseLong(value);
}
else if(!value.isEmpty() && !(value.equals("0"))){
result *= Long.parseLong(value);
}
}
}
return result;
}
/**
* Method for calculating the arithmetic 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 arithmetic average calculated.
*/
private long mit(String startCellName, String endCellName) {
return sum(startCellName, endCellName)/ counterOfCellsWithValue(startCellName, endCellName);
}
/**
* Method for calculating the standard deviation 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 standard deviation calculated.
*/
private long stabw(String startCellName, String endCellName) {
long mean = mit(startCellName, endCellName);
long counter = counterOfCellsWithValue(startCellName, endCellName);
long result = 0;
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++) {
for (int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
String value = get((row - 1), (col - 'A'));
if(!value.isEmpty()){
result += (long) Math.pow(Long.parseLong(value) - mean, 2);
}
}
}
return (long) Math.sqrt(result / counter);
}
/**
* Method for finding the maximum value 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 minimum value of the block.
*/
private long min(String startCellName, String endCellName) {
long result = 0;
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
String value = get((row - 1), (col - 'A')); //"-1" because we start with 1 instead of 0. "-A" because A in ascii is 65, but we start with 0
if((col == startCellName.charAt(0)) && (row == Integer.parseInt(startCellName.substring(1)))) {
result = Long.parseLong(value);
}
else if(!value.isEmpty() && (Long.parseLong(value) < result)){
result = Long.parseLong(value);
}
}
}
return result;
}
/**
* Method for finding the maximum value 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 maximum value of the block.
*/
private long max(String startCellName, String endCellName) {
long result = 0;
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
String value = get((row - 1), (col - 'A')); //"-1" because we start with 1 instead of 0. "-A" because A in ascii is 65, but we start with 0
if((col == startCellName.charAt(0)) && (row == Integer.parseInt(startCellName.substring(1)))) {
result = Long.parseLong(value);
}
else if(!value.isEmpty() && (Long.parseLong(value) > result)){
result = Long.parseLong(value);
}
}
}
return result;
}
/**
* This method calculates the result of a "normal" algebraic expression. It only needs to support
@ -162,17 +352,47 @@ public class Spreadsheet {
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
long res = 0;
char operator = '+';
// 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()) {
String s = m.group();
if (!s.isEmpty()) {
System.out.println(s);
}
long value;
if(s.isEmpty()){
continue;
}
if (!(s.matches("[-\\+\\*/]"))) {
if(s.matches("[A-Z][0-9]*")) {
int col = this.getCol(s);
int row = this.getRow(s);
value = Long.parseLong(cells[row][col].getValue());
}else{
value = Long.parseLong(s);
}
switch(operator) {
case '+':
res += value;
break;
case '-':
res -= value;
break;
case '*':
res *= value;
break;
case '/':
if (res != 0) {
res /= value;
break;
}
}
}else{
operator = s.charAt(0);
}
}
return res;
}
@ -182,7 +402,7 @@ public class Spreadsheet {
StringBuilder sb = new StringBuilder();
sb.append(" ");
for (int i = 0; i < cells.length; i++) {
for (int i = 0; i < cells[0].length; i++) {
sb.append(" " + (char)('A'+ i) + " | ");
}

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Axel/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/Test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@ -0,0 +1,138 @@
package de.hs_mannheim.informatik.spreadsheet;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SpreadsheetTest {
@Test
void calculateTheSumOfNumbers(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "=13+6");
assertEquals("19", eingabe.get("T5"));
eingabe.put("T6", "=0+0");
assertEquals("0", eingabe.get("T6"));
eingabe.put("T7", "=22+34");
assertEquals("56", eingabe.get("T7"));
eingabe.put("T8", "= 9 + 3");
assertEquals("12", eingabe.get("T8"));
}
@Test
void calculateTheDifOfNumbers(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "=13-6");
assertEquals("7", eingabe.get("T5"));
eingabe.put("T6", "=0-0");
assertEquals("0", eingabe.get("T6"));
eingabe.put("T7", "=22-34");
assertEquals("-12", eingabe.get("T7"));
eingabe.put("T8", "= 9 - 3");
assertEquals("6", eingabe.get("T8"));
}
@Test
void calculateTheProdOfNumbers(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "=13*6");
assertEquals("78", eingabe.get("T5"));
eingabe.put("T6", "=0*0");
assertEquals("0", eingabe.get("T6"));
eingabe.put("T7", "=-2*34");
assertEquals("-68", eingabe.get("T7"));
eingabe.put("T8", "= 9 * 3");
assertEquals("27", eingabe.get("T8"));
}
@Test
void calculateTheValueOfQuotOfNumbers(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "=12/3");
assertEquals("4", eingabe.get("T5"));
eingabe.put("T6", "=5/0");
assertEquals("exc.", eingabe.get("T6"));
eingabe.put("T7", "= 22 / 2");
assertEquals("11", eingabe.get("T7"));
eingabe.put("T8", "= 0 / 5");
assertEquals("0", eingabe.get("T8"));
}
@Test
void calculateTheSumOfRectangular(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "8");
eingabe.put("T6", "2");
eingabe.put("T7", "30");
eingabe.put("T8", "10");
eingabe.put("T9", "=Summe(T5:T8)");
assertEquals("50", eingabe.get("T9"));
}
void calculateTheSProdOfRectangular(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "2");
eingabe.put("T6", "4");
eingabe.put("T7", "6");
eingabe.put("T8", "10");
eingabe.put("T9", "=Produkt(T5:T8)");
assertEquals("480", eingabe.get("T9"));
}
void calculateTheMitOfRectangular(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "8");
eingabe.put("T6", "2");
eingabe.put("T7", "30");
eingabe.put("T8", "10");
eingabe.put("T9", "=Mittelwert(T5:T8)");
assertEquals("12", eingabe.get("T9"));
}
void calculateTheStabwOfRectangular(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "8");
eingabe.put("T6", "2");
eingabe.put("T7", "30");
eingabe.put("T8", "10");
eingabe.put("T9", "=Stabw(T5:T8)");
assertEquals("12", eingabe.get("T9"));
}
void calculateTheMinOfRectangular(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "8");
eingabe.put("T6", "2");
eingabe.put("T7", "30");
eingabe.put("T8", "10");
eingabe.put("T9", "=min(T5:T8)");
assertEquals("2", eingabe.get("T9"));
}
void calculateTheMaxOfRectangular(){
Spreadsheet eingabe = new Spreadsheet(99, 26);
eingabe.put("T5", "8");
eingabe.put("T6", "2");
eingabe.put("T7", "30");
eingabe.put("T8", "10");
eingabe.put("T9", "=Summe(T5:T8)");
assertEquals("30", eingabe.get("T9"));
}
}