package de.hs_mannheim.informatik.spreadsheet; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim. * * @author Oliver Hummel & Victor Waitz */ public class Axel { static Scanner keyboard = new Scanner(System.in); static Spreadsheet spr = new Spreadsheet(11, 11); static String saveFilePath = "Axel\\resources\\zahlen.csv"; public static void main(String[] args) throws FileNotFoundException { System.out.println("Welcome to Axel (Totally not Excel)"); System.out.println(); spr.loadSpecialTable(); System.out.println(spr); // spr.saveCsv("/tmp/test.csv"); while (true) { String userCommandPositionInput = userCommandPositionInput(); if (userCommandPositionInput.charAt(0) == '*') { executeCommand(userCommandPositionInput); System.out.println(spr); continue; } String userPositionInput = userCommandPositionInput; String userValueFormulaInput = userValueFormulaInput(userPositionInput); spr.put(userPositionInput, userValueFormulaInput); System.out.println(spr); } } //? User input for a program command or a position public static String userCommandPositionInput() { System.out.println(); System.out.println("Input a command for the program or a position"); System.out.printf("List of program commands: *clear, *load, *save, *exit or *help %nFormat for position: A1 or B14%n"); System.out.print("Input: "); String userCommandPositionInput = keyboard.nextLine(); return userCommandPositionComputation(userCommandPositionInput); } public static String userCommandPositionComputation(String userCommandPositionInput) { if (userCommandPositionInput.isEmpty()) { System.out.println("Input is empty!"); return userCommandPositionInput(); } //? Program command if (userCommandPositionInput.charAt(0) == '*') { if (!(userCommandErrorCheck(userCommandPositionInput))) { System.out.println("Invalid command!"); return userCommandPositionInput(); } return userCommandPositionInput; } //? Position if (!(userPositionErrorCheck(userCommandPositionInput, spr))) { System.out.println("Invalid position!"); return userCommandPositionInput(); } if (!(userPositionBoundsErrorCheck(userCommandPositionInput, spr))) { System.out.println("Position out of bounds!"); return userCommandPositionInput(); } return userCommandPositionInput; } public static boolean userCommandErrorCheck(String CommandToCheck) { //? true if valid //? valid inputs are: *clear, *exit, *save, *load, *help (and all upper case variants) CommandToCheck = CommandToCheck.toLowerCase(); if (CommandToCheck.equals("*clear") || CommandToCheck.equals("*exit") || CommandToCheck.equals("*save") || CommandToCheck.equals("*load") || CommandToCheck.equals("*help")) { return true; } return false; } public static boolean userPositionErrorCheck(String positionToCheck, Spreadsheet spr) { //? true if valid //? valid inputs are: A1, B14, C79, E99, F1, G99, J1, M98, ... (and all lower case variants) positionToCheck = positionToCheck.toUpperCase(); //? Check if input is the right length if (positionToCheck.length() < 2 || positionToCheck.length() > 3) { return false; } //? Check if input is in the right format if (!(Character.isLetter(positionToCheck.charAt(0)) && Character.isDigit(positionToCheck.charAt(1)))) { return false; } if (positionToCheck.length() == 3) { if (!(Character.isDigit(positionToCheck.charAt(2)))) { return false; } } return true; } public static boolean userPositionBoundsErrorCheck(String positionToCheck, Spreadsheet spr) { //? true if valid positionToCheck = positionToCheck.toUpperCase(); if ((spr.getRow(positionToCheck) >= spr.getRowsLCount()) || (spr.getCol(positionToCheck) >= spr.getColsCount())) { return false; } return true; } //? User input for a value or a formula public static String userValueFormulaInput(String currentPos) { System.out.println(); System.out.printf("Input a value of a formula for the selected position: %s.%n", currentPos); System.out.printf("Format for a value: 7 or 1337 %nFormat for a formula: =7*6 or =SUMME(A1:A3)%n"); System.out.print("Input: "); String userCommandInput = keyboard.nextLine(); return userValueFormulaComputation(userCommandInput, currentPos); } public static String userValueFormulaComputation(String userValueFormulaInput, String currentPos) { if (userValueFormulaInput.isEmpty()) { System.out.println("Input is empty!"); return userValueFormulaInput(currentPos); } //? Formula if (userValueFormulaInput.charAt(0) == '=') { if (!(userFormulaErrorCheck(userValueFormulaInput))) { System.out.println("Invalid formula!"); return userValueFormulaInput(currentPos); } return userValueFormulaInput; } //? Value if (!(userValueErrorCheck(userValueFormulaInput))) { System.out.println("Invalid value!"); return userValueFormulaInput(currentPos); } return userValueFormulaInput; } public static boolean userFormulaErrorCheck(String formulaToCheck) { //? true if valid //? valid inputs are: =7*6, =SUM(A1:A3), =A1, =A1+A2, =A1-A2, ... (and all lower case variants) //? remove '=' at the beginning and make everything upper case formulaToCheck = formulaToCheck.toUpperCase().substring(1); if (formulaToCheck.startsWith("SUMME(") || formulaToCheck.startsWith("PRODUKT(") || formulaToCheck.startsWith("MITTELWERT(") || formulaToCheck.startsWith("STABW(") || formulaToCheck.startsWith("MIN(") || formulaToCheck.startsWith("MAX(")) { return userFormulaFunctionErrorCheck(formulaToCheck); } return userFormulaExpressionErrorCheck(formulaToCheck); } public static boolean userFormulaFunctionErrorCheck(String functionToCheck) { String[] functionCorners = spr.isolateFunctionCorners(functionToCheck); if (functionCorners.length != 2) { return false; } for (String functionCorner : functionCorners) { if (!(spr.isValueCellName(functionCorner).equals("cellName"))) { return false; } if (!userPositionBoundsErrorCheck(functionCorner, spr)) { System.out.print("Out of bounds - "); return false; } } if (functionToCheck.startsWith("MITTELWERT(") || functionToCheck.startsWith("STABW(")) { String[] functionBlock = spr.wholeFunctionBlock(functionToCheck); boolean allEmpty = true; for (String cell : functionBlock) { if (!cell.isEmpty()) { allEmpty = false; } } if (allEmpty) { System.out.print("Division by zero - "); return false; } } if (functionToCheck.startsWith("STABW(")) { String[] functionBlock = spr.wholeFunctionBlock(functionToCheck); String[] notEmptyValues = spr.extractNotEmptyCells(functionBlock); if (notEmptyValues.length < 2) { System.out.print("Division by zero - "); return false; } } return true; } public static boolean userFormulaExpressionErrorCheck(String expressionToCheck) { //TODO ME: Maybe not worth the time return true; } public static boolean userValueErrorCheck(String valueToCheck) { //? true if valid //? valid inputs are: 7, 1337, 0, , -213,... //? For floating point numbers // String digitCheckRegex = "-?\\d+(\\.\\d+)?"; //? For integers String digitCheckRegex = "-?\\d+"; if (!(valueToCheck.matches(digitCheckRegex))) { return false; } return true; } public static void executeCommand(String command) throws FileNotFoundException { switch (command) { case "*clear": progClear(); break; case "*load": progLoad(); break; case "*save": progSave(); break; case "*exit": progExit(); break; } } private static void progClear(){ System.out.println("Are you sure you want to clear the table? (yes/no)"); System.out.print("Input: "); String userClearInput = keyboard.nextLine().toLowerCase(); if (userClearInput.equals("yes") || userClearInput.equals("y")){ spr.clearTable(); System.out.println("Table cleared!"); } } private static void progLoad() throws FileNotFoundException { spr.readCsv(saveFilePath, ",", "Amogus"); System.out.println("File loaded!"); } private static void progSave() throws FileNotFoundException { String savePath = saveFilePath; spr.saveCsv(savePath); System.out.println("File saved"); } private static void progExit() throws FileNotFoundException { System.out.println("Do you want to save befor you exit? (yes/no)"); System.out.print("Input: "); String userExitInput = keyboard.nextLine().toLowerCase(); if (userExitInput.equals("yes") || userExitInput.equals("y")) { progSave(); } System.out.println("Goodbye!"); } }