From 4419f6e8721e31b7037bbaed9bb4962151e52621 Mon Sep 17 00:00:00 2001 From: <3010678@stud.hs-mannheim.de> Date: Thu, 11 Jan 2024 02:01:26 +0100 Subject: [PATCH] //JUNIT Tests & some Error corrections --- .../informatik/spreadsheet/Axel.java | 16 ++- .../informatik/spreadsheet/AxelTesting.java | 134 ++++++++++++++++++ .../informatik/spreadsheet/Spreadsheet.java | 28 +++- 3 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 Axel/src/de/hs_mannheim/informatik/spreadsheet/AxelTesting.java diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index b1390d0..619b46c 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -34,6 +34,8 @@ public class Axel { spr.put("B2", "5"); spr.put("B3", "8"); + spr.put("C1", "8"); + spr.put("C2", "2"); // @@ -64,8 +66,20 @@ public class Axel { System.out.println("cell name:"); readInput= sc.nextLine(); + currentCell=readInput; + readInput= readInput.toUpperCase(); + if(readInput.charAt(0)>(char)('A'+spr.cells[0].length-1)){ + System.out.println("This cell doesn't exist :("); + continue; + } + if(Integer.parseInt(readInput.substring(1))>spr.cells.length) { + System.out.println("This cell doesn't exist :("); + continue; + } + + System.out.println("Set formula or value:"); readInput= sc.nextLine(); @@ -81,7 +95,7 @@ public class Axel { System.out.println("Do you want to save the sheet?(y/n)"); readInput=sc.nextLine(); if(readInput.equals("y")||readInput.equals("Y")) { - spr.saveCsv("test"); + spr.saveCsv("/home/emelie/Dokumente/Files/leer.csv"); } diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/AxelTesting.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/AxelTesting.java new file mode 100644 index 0000000..8f2acae --- /dev/null +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/AxelTesting.java @@ -0,0 +1,134 @@ +package de.hs_mannheim.informatik.spreadsheet; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class AxelTesting { + + @Test + void testSum() { + Spreadsheet testSheet= new Spreadsheet(99,26); + testSheet.put("A1", "=4"); + testSheet.put("A2", "=3"); + testSheet.put("B1", "=5"); + testSheet.put("B2", "=2"); + testSheet.put("B3", "=1"); + testSheet.put("C1", "=SUMME(A1:B3)"); + + assertEquals("15", testSheet.get("C1")); + + + } + + @Test + void testProd() { + + Spreadsheet testSheet= new Spreadsheet(99,26); + testSheet.put("A1", "=4"); + testSheet.put("A2", "=3"); + testSheet.put("B1", "=5"); + testSheet.put("B2", "=2"); + testSheet.put("B3", "=1"); + testSheet.put("C1", "=PRODUKT(A1:B3)"); + + assertEquals("120", testSheet.get("C1")); + + } + + @Test + void testAvg() { + + Spreadsheet testSheet= new Spreadsheet(99,26); + testSheet.put("A1", "=4"); + testSheet.put("A2", "=3"); + testSheet.put("B1", "=5"); + testSheet.put("B2", "=2"); + testSheet.put("B3", "=1"); + testSheet.put("C1", "=10"); + testSheet.put("C3", "=15"); + + testSheet.put("C4", "=MITTELWERT(A1:C3)"); + + assertEquals("5", testSheet.get("C4")); + + } + + @Test + void testMin() { + + Spreadsheet testSheet= new Spreadsheet(99,26); + testSheet.put("A1", "=123"); + testSheet.put("A2", "=12"); + testSheet.put("B1", "=5"); + testSheet.put("B2", "=23"); + testSheet.put("B3", "=4"); + testSheet.put("C1", "=10"); + testSheet.put("C3", "=15"); + testSheet.put("D5", "=3"); + + testSheet.put("D6", "=MIN(A1:D5)"); + assertEquals(3, Integer.parseInt(testSheet.get("D6"))); + + + } + + @Test + + void testMax() { + + Spreadsheet testSheet= new Spreadsheet(99,26); + testSheet.put("A1", "=23"); + testSheet.put("A2", "=12"); + testSheet.put("B1", "=5"); + testSheet.put("B2", "=123"); + testSheet.put("B3", "=4"); + testSheet.put("C1", "=10"); + testSheet.put("C3", "=15"); + testSheet.put("D5", "=3"); + + testSheet.put("D6", "=MAX(A1:D5)"); + assertEquals("123", testSheet.get("D6")); + + } + + @Test + void testStDev() { + Spreadsheet testSheet= new Spreadsheet(99,26); + testSheet.put("A1", "=23"); + testSheet.put("A2", "=12"); + testSheet.put("B1", "=5"); + testSheet.put("B2", "=123"); + testSheet.put("B3", "=4"); + testSheet.put("C1", "=10"); + testSheet.put("C3", "=15"); + testSheet.put("D5", "=3"); + + testSheet.put("D6", "=STABW(A1:D5)"); + assertEquals("37", testSheet.get("D6")); + } + + @Test + void testCalc() { + + Spreadsheet testSheet= new Spreadsheet(99,26); + testSheet.put("A1", "=23"); + testSheet.put("A2", "=12"); + testSheet.put("B1", "=5"); + testSheet.put("B2", "=123"); + testSheet.put("B3", "=4"); + testSheet.put("C1", "=10"); + testSheet.put("C3", "=15"); + testSheet.put("D5", "=3"); + + testSheet.put("D6", "=10+3+C3-B1"); + testSheet.put("D7", "=666/B3"); + testSheet.put("D8", "=30*5/2+20"); + + assertEquals("23", testSheet.get("D6")); + assertEquals("166", testSheet.get("D7")); + assertEquals("95", testSheet.get("D8")); + } + + +} diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index e303885..8df082b 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -77,9 +77,12 @@ public class Spreadsheet { } private int getRow(String cellName) { - return cellName.charAt(1) - '1'; + String row = ""; + for (int i = 1; i < cellName.length(); i++){ + row += cellName.charAt(i); + } + return Integer.parseInt(row) - 1; } - // ----- // business logic @@ -221,9 +224,12 @@ public class Spreadsheet { for(int i= startRow; i<=endRow; i++) { for(int j=startCol; j<=endCol; j++) { + if(cells[i][j].getValue().equals("")){ + temp=0; + }else { temp= Integer.parseInt(cells[i][j].getValue()); sum=sum+temp; - + } } } @@ -255,11 +261,14 @@ public class Spreadsheet { for(int i= startRow; i<=endRow; i++) { for(int j=startCol; j<=endCol; j++) { - + if(cells[i][j].getValue().equals("")){ + temp=1; + }else { temp= Integer.parseInt(cells[i][j].getValue()); prod=prod*temp; } } + } return prod; } @@ -292,9 +301,11 @@ public class Spreadsheet { for(int i= startRow; i<=endRow; i++) { for(int j=startCol; j<=endCol; j++) { + if(!cells[i][j].getValue().equals("")) { temp= Integer.parseInt(cells[i][j].getValue()); sum=sum+temp; part++; + } } } avg=sum/part; @@ -328,12 +339,13 @@ public class Spreadsheet { for(int i= startRow; i<=endRow; i++) { for(int j=startCol; j<=endCol; j++) { - + if(!cells[i][j].getValue().equals("")) { temp= Integer.parseInt(cells[i][j].getValue()); if(min>temp) { min=temp; } } + } } @@ -367,11 +379,12 @@ public class Spreadsheet { for(int i= startRow; i<=endRow; i++) { for(int j=startCol; j<=endCol; j++) { - + if(!cells[i][j].getValue().equals("")) { temp= Integer.parseInt(cells[i][j].getValue()); if(max