forked from hummel/PR1-Spreadsheet
UI ausgebessert um verschiedene Funktionen aufzurufen: Funktion
implementiert um neue Dateien anzulegen, Funktion implementiert um Dateien auszulesenmaster
parent
f9983c112e
commit
b7d8ceecc9
|
@ -1,71 +1,209 @@
|
|||
package de.hs_mannheim.informatik.spreadsheet;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
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;
|
||||
public static void main(String[] args) throws IOException {
|
||||
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");
|
||||
// spr.put("A2", "1");
|
||||
// spr.put("A3", "123");
|
||||
//
|
||||
// spr.put("B1", "=3");
|
||||
// spr.put("c1", "=A2-A1+30");
|
||||
// spr.put("B9", "=41+A1");
|
||||
// spr.put("J5", "=7*6/5");
|
||||
// spr.put("J6", "=3/2");
|
||||
System.out.println("\n 1. Neues Dokument erstellen");
|
||||
System.out.println(" 2. Dokument öffnen");
|
||||
System.out.println(" 3. Programm schließen");
|
||||
System.out.print("-> ");
|
||||
String userInput = scanner.nextLine();
|
||||
|
||||
|
||||
|
||||
int test=1;
|
||||
System.out.println("Wilkommen zum coolsten Spreadsheet Programm der Welt!");
|
||||
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]);
|
||||
switch(userInput) {
|
||||
case "1":
|
||||
neuesDokument(directoryPath);
|
||||
break;
|
||||
|
||||
// System.out.println(parts[0]);
|
||||
System.out.println(spr);
|
||||
}
|
||||
else {
|
||||
System.out.println("Formel ist nicht valide! ");
|
||||
case "2":
|
||||
dokumentAuswaehlen(directoryPath,filePath);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// System.out.println("Was möchten sie machen?");
|
||||
//
|
||||
// System.out.println("1. Dokument öffnen");
|
||||
// System.out.println("2. neues Dokument erstellen");
|
||||
|
||||
|
||||
case "3":
|
||||
break;
|
||||
}
|
||||
|
||||
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!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package de.hs_mannheim.informatik.spreadsheet;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
|
@ -14,17 +16,21 @@ import java.util.regex.Pattern;
|
|||
* @author Oliver Hummel
|
||||
*/
|
||||
public class Spreadsheet {
|
||||
int rows=0;
|
||||
int cols=0;
|
||||
Cell[][] cells;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor that creates a Spreadsheet of size rows * cols.
|
||||
* @param rows number of rows
|
||||
* @param cols number of columns
|
||||
*/
|
||||
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];
|
||||
|
||||
for (int r = 0; r < rows; r++)
|
||||
|
@ -87,12 +93,20 @@ public class Spreadsheet {
|
|||
* A method for reading in data from a CSV file.
|
||||
* @param path The file to read.
|
||||
* @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.
|
||||
* @exception IOException If path does not exist.
|
||||
*/
|
||||
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
||||
// TODO: implement this
|
||||
public void readCsv(String path, String separator) throws IOException {
|
||||
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);
|
||||
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
|
||||
|
||||
|
||||
ArrayList<String> zahlen = new ArrayList<>();
|
||||
|
||||
while(m.find()) {
|
||||
|
@ -187,7 +200,6 @@ public class Spreadsheet {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
int ersteZahl = Integer.parseInt(get(zahlen.get(0)));
|
||||
int zweiteZahl=0;
|
||||
|
||||
|
@ -213,8 +225,6 @@ public class Spreadsheet {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ersteZahl;
|
||||
}
|
||||
|
||||
|
@ -224,6 +234,8 @@ public class Spreadsheet {
|
|||
/**
|
||||
* This method checks if a given string contains one or more digits
|
||||
* 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) {
|
||||
return input.matches("[0-9]+");
|
||||
|
|
Loading…
Reference in New Issue