PR1-Spreadsheet/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java

118 lines
4.0 KiB
Java
Raw Normal View History

2023-12-12 15:00:07 +01:00
package de.hs_mannheim.informatik.spreadsheet;
2024-01-07 18:58:12 +01:00
import java.io.File;
2023-12-14 08:31:20 +01:00
import java.io.FileNotFoundException;
2024-01-07 14:42:58 +01:00
import java.io.FileOutputStream;
import java.io.PrintWriter;
2024-01-06 15:24:05 +01:00
import java.util.Scanner;
2023-12-12 15:00:07 +01:00
/**
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
2024-01-06 22:18:47 +01:00
*
2023-12-12 15:00:07 +01:00
* @author Oliver Hummel
*/
public class Axel {
2023-12-14 08:31:20 +01:00
public static void main(String[] args) throws FileNotFoundException {
2024-01-07 14:42:58 +01:00
Spreadsheet spr = new Spreadsheet(10,6);
2024-01-07 18:58:12 +01:00
boolean UiLoop = true;
2024-01-06 15:24:05 +01:00
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
2024-01-07 18:58:12 +01:00
String filePath = "Axel/resources/";
System.out.println("Wilkommen zum coolsten Spreadsheet Programm der Welt!(Cooler als Devran Cakici<63>s 3009694)");
2024-01-07 11:36:28 +01:00
2024-01-07 18:58:12 +01:00
System.out.println("Was m<>chten sie machen?");
2023-12-12 15:00:07 +01:00
2024-01-07 18:58:12 +01:00
System.out.println("\n 1. Dokument <20>ffnen");
System.out.println(" 2. Neues Dokument erstellen");
System.out.println(" 3. Programm schlie<69>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<6E>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<6E>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){
2024-01-06 15:24:05 +01:00
String command = "";
String form = "";
String cell= "";
boolean cellTRUE = true;
2024-01-07 18:58:12 +01:00
System.out.println("Schreibe 'stop' zum anhalten.");
2024-01-06 15:24:05 +01:00
System.out.println(spr);
System.out.println("Befehl (z.B. D4_=7*6): ");
command = input.nextLine();
2024-01-07 11:36:28 +01:00
if(command.matches("[A-Za-z][1-9][0-9]?_="
+ "((((([A-Za-z][1-9][0-9]?)|([0-9]+))(\\*|\\+|\\-|\\/))*"
+ "(([A-Za-z][1-9][0-9]?)|([0-9]+)))|"
+ "((SUMME|PRODUKT|MID|STABW|MIN|MAX)\\(([A-Za-z][1-9][0-9]*\\:[A-Za-z][1-9][0-9]*)\\)))")) {
2024-01-07 10:36:27 +01:00
for(int i = 0; i<command.length();i++) {
if(command.charAt(i)=='_') {
cellTRUE = false;
continue;
}
if(cellTRUE) {
cell+=command.charAt(i);
}else {
form+=command.charAt(i);
}
}
if((spr.getRow(cell)+1)<=spr.cells.length&&(spr.getCol(cell)+1)<=spr.cells[0].length) {
spr.put(cell, form);
}
else {
System.err.println("Zelle '"+ cell+ "' existiert nicht!");
}
2024-01-07 10:36:27 +01:00
}else if(command.matches("stop")){
break;
2024-01-07 18:58:12 +01:00
}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;
2024-01-07 10:36:27 +01:00
}else {
System.out.println("Die Eingabe war nicht korrekt. Die Tabelle bleibt auf folgendem stand:");
}
2024-01-07 14:42:58 +01:00
}
2023-12-12 15:00:07 +01:00
}
}