118 lines
4.0 KiB
Java
118 lines
4.0 KiB
Java
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;
|
||
|
||
/**
|
||
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
||
*
|
||
* @author Oliver Hummel
|
||
*/
|
||
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/";
|
||
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("\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();
|
||
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]*)\\)))")) {
|
||
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!");
|
||
}
|
||
}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:");
|
||
}
|
||
}
|
||
}
|
||
|
||
} |