1
0
Fork 0

UI ausgebessert um verschiedene Funktionen aufzurufen: Funktion

implementiert um neue Dateien anzulegen, Funktion implementiert um
Dateien auszulesen
master
devra 2024-01-08 02:40:29 +01:00
parent f9983c112e
commit b7d8ceecc9
2 changed files with 210 additions and 60 deletions

View File

@ -1,71 +1,209 @@
package de.hs_mannheim.informatik.spreadsheet; package de.hs_mannheim.informatik.spreadsheet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.Scanner; import java.util.Scanner;
/** /**
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim. * Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
* *
* @author Oliver Hummel * @author Oliver Hummel
*/ */
public class Axel { public class Axel {
public static void main(String[] args) throws FileNotFoundException { Spreadsheet spr;
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
Spreadsheet spr = new Spreadsheet(10,10); String directoryPath = "C:\\Users\\devra\\git\\Axel\\Axel\\resources\\";
String filePath = "C:\\Users\\devra\\git\\Axel\\Axel\\resources\\example.csv";
System.out.println(); System.out.println("\nWilkommen zum coolsten Spreadsheet Programm der Welt!");
System.out.println("Was möchten sie machen?");
// spr.put("A1", "13"); System.out.println("\n 1. Neues Dokument erstellen");
// spr.put("A2", "1"); System.out.println(" 2. Dokument öffnen");
// spr.put("A3", "123"); System.out.println(" 3. Programm schließen");
// System.out.print("-> ");
// spr.put("B1", "=3"); String userInput = scanner.nextLine();
// spr.put("c1", "=A2-A1+30");
// spr.put("B9", "=41+A1");
// spr.put("J5", "=7*6/5");
// spr.put("J6", "=3/2");
switch(userInput) {
case "1":
int test=1; neuesDokument(directoryPath);
System.out.println("Wilkommen zum coolsten Spreadsheet Programm der Welt!"); break;
System.out.println("Beispiel-Formel: 'B2=15+5' ");
while(test==1) {
System.out.println("Geben sie ihre Formel ein: ");
System.out.print("-> ");
String userInput = scanner.nextLine();
if(userInput.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]*)\\)))"))
{
String[] parts = userInput.split("=");
spr.put(parts[0],"="+ parts[1]);
// System.out.println(parts[0]); case "2":
System.out.println(spr); dokumentAuswaehlen(directoryPath,filePath);
} break;
else {
System.out.println("Formel ist nicht valide! ");
} case "3":
break;
// System.out.println("Was möchten sie machen?");
//
// System.out.println("1. Dokument öffnen");
// System.out.println("2. neues Dokument erstellen");
} }
spr.saveCsv("C:\\Users\\devra\\git\\Axel\\Axel\\resources\\zahlen.csv");
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
} }
/**
* A method for creating a new .csv file
* @param directoryPath The path where the new File is being saved.
* @return Nothing.
* @exception IOException If path does not exist.
*/
public static void neuesDokument(String directoryPath) throws IOException {
//TODO: Zeilen und Spalten begrenzen
Scanner scanner = new Scanner(System.in);
System.out.println("Wie viele Spalten soll ihr Dokument haben?(1-26)");
System.out.print("->");
String spalten=scanner.nextLine();
System.out.println("Wie viele Zeilen soll ihr Dokument haben?(1-99)");
System.out.print("->");
String zeilen=scanner.nextLine();
String name="";
while (true) {
System.out.println("Wie soll ihr Dokument heißen?");
System.out.print("->");
name = scanner.nextLine();
File file = new File(directoryPath, name + ".csv");
if (file.exists()) {
System.out.println("\nDie Datei '" + name + ".csv" + "' existiert bereits, wähle einen anderen Namen");
} else {
break;
}
}
Spreadsheet spr = new Spreadsheet(Integer.parseInt(zeilen),Integer.parseInt(spalten));
String filePath=directoryPath+name+".csv";
spr.saveCsv(filePath);
System.out.println("Die Datei '"+name+".csv' wurde angelegt");
Spreadsheet spr1 = new Spreadsheet(Integer.parseInt(zeilen),Integer.parseInt(spalten));
spr1.readCsv(filePath,",");
System.out.println(spr1);
eingabeLoop(spr1,filePath);
}
/**
* A method for selecting a .csv file
* @param directoryPath The path where a File can be openend from.
* @return Nothing.
* @exception IOException if BufferedReader can't access a file.
*/
public static void dokumentAuswaehlen(String directoryPath,String filePath) throws IOException {
File folder = new File(directoryPath);
Scanner scanner = new Scanner(System.in);
String userInput="";
if (folder.exists() && folder.isDirectory()) {
File[] files = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".csv"));
if (files != null) {
while(true) {
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.print("->");
userInput = scanner.nextLine();
if(Integer.parseInt(userInput)>files.length||Integer.parseInt(userInput)<1) {
System.err.println("Datei nicht vorhanden!");
}
else {
break;
}
}
filePath = directoryPath+files[Integer.parseInt(userInput)-1].getName();
//feststellen wie groß die Datei ist(Zeilen/Spalten)
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
int zeilenAnzahl = 0;
int spaltenAnzahl = 20;
while ((line = reader.readLine()) != null) {
zeilenAnzahl++;
String[] spalten = line.split(",",-1); //-1 for empty cells
spaltenAnzahl = spalten.length;
}
reader.close();
//neues objekt mit zeilen/spalten-anzahl anlegen um .readCsv aufzurufen
Spreadsheet spr1 = new Spreadsheet(zeilenAnzahl,spaltenAnzahl);
spr1.readCsv(filePath,",");
System.out.println(spr1);
eingabeLoop(spr1,filePath);
} else {
System.out.println("Es wurde noch keine Datei angelegt.");
}
} else {
System.out.println("Ungültiges Verzeichnis oder Verzeichnis existiert nicht.");
System.out.println("Geben sie hier das Verzeichnis ein in dem sie Arbeiten möchten");
System.out.print("-> ");
directoryPath = scanner.nextLine();
}
}
/**
* A method for editing a .csv file
* @param filePath The path of the File that will be edited
* @param spr1 A Spreadsheet object used to access Spreadsheet-methods
* @return Nothing.
* @exception FileNotFoundException if file does not exist.
*/
private static void eingabeLoop(Spreadsheet spr1, String filePath) throws FileNotFoundException {
System.out.println("\n---------------------------------");
System.out.println("| Beispiel Formel: 'B2=B5*5' |");
System.out.println("| 'save' um zu Speichern |");
System.out.println("| 'exit' um Programm zu beenden |");
System.out.println("---------------------------------");
int flag = 1;
while (flag == 1) {
Scanner scanner = new Scanner(System.in);
System.out.println("\nGeben sie ihre Formel/Befehl ein: ");
System.out.print("-> ");
String userInput = scanner.nextLine();
if (userInput.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]*)\\)))")){
String[] parts = userInput.split("=");
spr1.put(parts[0], "=" + parts[1]);
System.out.println(spr1);
} else if (userInput.toLowerCase().contains("exit")) {
System.out.println("Programm wurde beendet!");
flag = 0;
} else if (userInput.toLowerCase().contains("save")) {
spr1.saveCsv(filePath);
File file = new File(filePath);
String filename = file.getName();
System.out.println("'"+filename+"' wurde erfolgreich gespeichert!");
} else {
System.err.println("Formel ist nicht valide!");
}
}
}
} }

View File

@ -1,6 +1,8 @@
package de.hs_mannheim.informatik.spreadsheet; package de.hs_mannheim.informatik.spreadsheet;
import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,17 +16,21 @@ import java.util.regex.Pattern;
* @author Oliver Hummel * @author Oliver Hummel
*/ */
public class Spreadsheet { public class Spreadsheet {
int rows=0;
int cols=0;
Cell[][] cells; Cell[][] cells;
/** /**
* Constructor that creates a Spreadsheet of size rows * cols. * Constructor that creates a Spreadsheet of size rows * cols.
* @param rows number of rows * @param rows number of rows
* @param cols number of columns * @param cols number of columns
*/ */
public Spreadsheet(int rows, int cols) { public Spreadsheet(int rows, int cols) {
// TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns // TODO limit the maximum size to 99 (1..99) rows and 26 (A..Z) columns
this.rows=rows;
this.cols=cols;
cells = new Cell[rows][cols]; cells = new Cell[rows][cols];
for (int r = 0; r < rows; r++) for (int r = 0; r < rows; r++)
@ -87,12 +93,20 @@ public class Spreadsheet {
* A method for reading in data from a CSV file. * A method for reading in data from a CSV file.
* @param path The file to read. * @param path The file to read.
* @param separator The char used to split up the input, e.g. a comma or a semicolon. * @param separator The char used to split up the input, e.g. a comma or a semicolon.
* @param starCellName The upper left cell where data from the CSV file should be inserted.
* @return Nothing. * @return Nothing.
* @exception IOException If path does not exist. * @exception IOException If path does not exist.
*/ */
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException { public void readCsv(String path, String separator) throws IOException {
// TODO: implement this BufferedReader reader = new BufferedReader(new FileReader(path));
for(int row=0;row<rows;row++) {
String line = reader.readLine();
String[] columns = line.split(",");
for (int col = 0; col < columns.length; col++) {
put(row, col, columns[col]);
}
}
reader.close();
} }
/** /**
@ -177,7 +191,6 @@ public class Spreadsheet {
//System.out.println(formula); //System.out.println(formula);
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
ArrayList<String> zahlen = new ArrayList<>(); ArrayList<String> zahlen = new ArrayList<>();
while(m.find()) { while(m.find()) {
@ -187,7 +200,6 @@ public class Spreadsheet {
} }
} }
int ersteZahl = Integer.parseInt(get(zahlen.get(0))); int ersteZahl = Integer.parseInt(get(zahlen.get(0)));
int zweiteZahl=0; int zweiteZahl=0;
@ -213,8 +225,6 @@ public class Spreadsheet {
break; break;
} }
} }
return ersteZahl; return ersteZahl;
} }
@ -224,6 +234,8 @@ public class Spreadsheet {
/** /**
* This method checks if a given string contains one or more digits * This method checks if a given string contains one or more digits
* to make sure that the string consists of only numbers, * to make sure that the string consists of only numbers,
* @param input The input that is getting checked for only numbers
* @return returns true when the string contains only numbers
*/ */
public boolean containsOnlyNumbers(String input) { public boolean containsOnlyNumbers(String input) {
return input.matches("[0-9]+"); return input.matches("[0-9]+");