From d950b04be526621348dde53e195efdaa6d9b874d Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 7 Jan 2024 18:58:12 +0100 Subject: [PATCH] UiLoop csv read und save --- .../informatik/spreadsheet/Axel.java | 72 ++++++++++++++++--- .../informatik/spreadsheet/Spreadsheet.java | 12 ++++ 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index d48fa56..d575548 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -1,5 +1,6 @@ package de.hs_mannheim.informatik.spreadsheet; +import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; @@ -14,18 +15,64 @@ public class Axel { public static void main(String[] args) throws FileNotFoundException { Spreadsheet spr = new Spreadsheet(10,6); + boolean UiLoop = true; @SuppressWarnings("resource") Scanner input = new Scanner(System.in); - String filePath = "Axel/resources/zahlen.csv"; - spr.readCsv(filePath,"A3"); + String filePath = "Axel/resources/"; + System.out.println("Wilkommen zum coolsten Spreadsheet Programm der Welt!(Cooler als Devran Cakici´s 3009694)"); + System.out.println("Was möchten sie machen?"); - System.out.println("Schreibe 'stop' zum anhalten."); - do{ + System.out.println("\n 1. Dokument öffnen"); + System.out.println(" 2. Neues Dokument erstellen"); + System.out.println(" 3. Programm schließen"); + System.out.print("-> "); + String docSlct = input.nextLine(); + + switch(docSlct) { + case "1": + File folder = new File(filePath); + + if (folder.exists() && folder.isDirectory()) { + File[] files = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".csv")); + + if (files != null) { + System.out.println("\nWähle eine Datei aus: "); + System.out.println(""); + for (int i = 0; i < files.length; i++) { + System.out.println(i+1 + ": " + files[i].getName()); + } + System.out.println(); + spr.readCsv(filePath+=files[Integer.parseInt(input.nextLine())-1].getName(),""); + } else { + System.out.println("Es wurden noch keine Datei angelegt."); + } + }else { + System.out.println("Ungültiges Verzeichnis oder Verzeichnis existiert nicht."); + System.out.println("Geben sie hier ihr verzeichnis ein in dem sie Arbeiten möchten: "); + filePath = input.nextLine(); + + } + + break; + case "2": + System.out.println("Reihen: "); + int rows = Integer.parseInt(input.nextLine()); + System.out.println("Spalten: "); + int cols = Integer.parseInt(input.nextLine()); + spr.changeSize(rows, cols); + break; + case "3": + UiLoop = false; + break; + } + while(UiLoop){ String command = ""; String form = ""; String cell= ""; boolean cellTRUE = true; + + System.out.println("Schreibe 'stop' zum anhalten."); System.out.println(spr); System.out.println("Befehl (z.B. D4_=7*6): "); command = input.nextLine(); @@ -47,16 +94,19 @@ public class Axel { spr.put(cell, form); }else if(command.matches("stop")){ break; + }else if(command.startsWith("save")){ + String csvName=command.substring(5)+".csv"; + String savePath = "Axel/resources/"; + try (PrintWriter writer = new PrintWriter(new FileOutputStream(savePath+csvName))) { + spr.saveCsv(writer); + System.out.println("CSV file saved successfully."); + } catch (FileNotFoundException e) { + System.err.println("Error: " + e.getMessage()); + } + break; }else { System.out.println("Die Eingabe war nicht korrekt. Die Tabelle bleibt auf folgendem stand:"); } - }while(true); - - 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()); } } diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index bb09fb5..1ff2a95 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -36,6 +36,18 @@ public class Spreadsheet { for (int c = 0; c < colWithMax26; c++) cells[r][c] = new Cell(); } + 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(); + } // ----- /*