1
0
Fork 0

UiLoop csv read und save

main
Jan 2024-01-07 18:58:12 +01:00
parent 883c36c929
commit d950b04be5
2 changed files with 73 additions and 11 deletions

View File

@ -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.FileOutputStream; import java.io.FileOutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -14,18 +15,64 @@ public class Axel {
public static void main(String[] args) throws FileNotFoundException { public static void main(String[] args) throws FileNotFoundException {
Spreadsheet spr = new Spreadsheet(10,6); Spreadsheet spr = new Spreadsheet(10,6);
boolean UiLoop = true;
@SuppressWarnings("resource") @SuppressWarnings("resource")
Scanner input = new Scanner(System.in); Scanner input = new Scanner(System.in);
String filePath = "Axel/resources/zahlen.csv"; String filePath = "Axel/resources/";
spr.readCsv(filePath,"A3"); 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."); System.out.println("\n 1. Dokument öffnen");
do{ 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 command = "";
String form = ""; String form = "";
String cell= ""; String cell= "";
boolean cellTRUE = true; boolean cellTRUE = true;
System.out.println("Schreibe 'stop' zum anhalten.");
System.out.println(spr); System.out.println(spr);
System.out.println("Befehl (z.B. D4_=7*6): "); System.out.println("Befehl (z.B. D4_=7*6): ");
command = input.nextLine(); command = input.nextLine();
@ -47,16 +94,19 @@ public class Axel {
spr.put(cell, form); spr.put(cell, form);
}else if(command.matches("stop")){ }else if(command.matches("stop")){
break; 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 { }else {
System.out.println("Die Eingabe war nicht korrekt. Die Tabelle bleibt auf folgendem stand:"); 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());
} }
} }

View File

@ -36,6 +36,18 @@ 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();
} }
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();
}
// ----- // -----
/* /*