From e5d531a7a487b4bc2c09c85b73d5d6677a8f496e Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 7 Jan 2024 14:42:58 +0100 Subject: [PATCH] save --- Axel/resources/zahlen.csv | 14 ++++++++---- .../informatik/spreadsheet/Axel.java | 22 ++++++++++++++----- .../informatik/spreadsheet/Spreadsheet.java | 12 +++++----- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Axel/resources/zahlen.csv b/Axel/resources/zahlen.csv index 1ec60e0..eef5399 100644 --- a/Axel/resources/zahlen.csv +++ b/Axel/resources/zahlen.csv @@ -1,4 +1,10 @@ -1,2 -3,4 -5,6 -7,8 +,24,,,, +,28,,,, +123,24,,,, +,24,,,, +,5,,,, +,5,,,, +,200,,,, +,200,,,, +,=41+A2+10-2/3,,,, +,,,,, diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index c2d20a0..a3bf4ab 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -1,6 +1,9 @@ package de.hs_mannheim.informatik.spreadsheet; +import java.io.File; import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.PrintWriter; import java.util.Scanner; /** @@ -11,17 +14,17 @@ import java.util.Scanner; public class Axel { public static void main(String[] args) throws FileNotFoundException { - Spreadsheet spr = new Spreadsheet(10,10); + Spreadsheet spr = new Spreadsheet(10,6); @SuppressWarnings("resource") Scanner input = new Scanner(System.in); spr.put("A3", "123"); spr.put("B9", "=41+A2+10-2/3"); - spr.put("B8", "2"); - spr.put("B7", "20"); - spr.put("B6", "42"); - spr.put("B5", "23"); + spr.put("B8", "200"); + spr.put("B7", "200"); + spr.put("B6", "5"); + spr.put("B5", "5"); spr.put("B4", "24"); spr.put("B3", "24"); spr.put("B2", "28"); @@ -60,7 +63,14 @@ public class Axel { } }while(true); - spr.saveCsv("tmp/test.csv"); + String filePath = "Axel/resources/zahlen.csv"; + + try (PrintWriter writer = new PrintWriter(new FileOutputStream(filePath))) { + spr.saveCsv(writer); + System.out.println("CSV file saved successfully."); + } catch (FileNotFoundException e) { + System.err.println("Error: " + e.getMessage()); + } } } \ No newline at end of file diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index ec281eb..8c8dbac 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -25,12 +25,13 @@ public class Spreadsheet { */ public Spreadsheet(int rows, int cols) { - // TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns + int rowWithMax99= Math.min(rows, 99); + int colWithMax26= Math.min(cols, 26); - cells = new Cell[rows][cols]; + cells = new Cell[rowWithMax99][colWithMax26]; - for (int r = 0; r < rows; r++) - for (int c = 0; c < cols; c++) + for (int r = 0; r < rowWithMax99; r++) + for (int c = 0; c < colWithMax26; c++) cells[r][c] = new Cell(); } @@ -100,8 +101,7 @@ public class Spreadsheet { * @return Nothing. * @exception IOException If path does not exist. */ - public void saveCsv(String path) throws FileNotFoundException { - PrintWriter out = new PrintWriter(path); + public void saveCsv(PrintWriter out) throws FileNotFoundException { for (Cell[] row : cells) { for (Cell cell : row) {