2023-12-12 15:00:07 +01:00
|
|
|
package de.hs_mannheim.informatik.spreadsheet;
|
|
|
|
|
2023-12-14 08:31:20 +01:00
|
|
|
import java.io.FileNotFoundException;
|
2023-12-27 03:54:24 +01:00
|
|
|
import java.util.ArrayList;
|
2023-12-18 00:53:59 +01:00
|
|
|
import java.util.Scanner;
|
2023-12-27 03:54:24 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2023-12-12 15:00:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
|
|
|
*
|
2023-12-18 00:53:59 +01:00
|
|
|
* @author Oliver Hummel & Victor Waitz
|
2023-12-12 15:00:07 +01:00
|
|
|
*/
|
|
|
|
public class Axel {
|
2023-12-18 00:53:59 +01:00
|
|
|
static Scanner keyboard = new Scanner(System.in);
|
2023-12-27 03:54:24 +01:00
|
|
|
static Spreadsheet spr = new Spreadsheet(11,11);
|
2023-12-12 15:00:07 +01:00
|
|
|
|
2023-12-14 08:31:20 +01:00
|
|
|
public static void main(String[] args) throws FileNotFoundException {
|
2023-12-29 20:28:36 +01:00
|
|
|
System.out.println("Welcome to Axel (Totally not Excel)");
|
|
|
|
System.out.println();
|
|
|
|
/*
|
2023-12-27 03:54:24 +01:00
|
|
|
spr.put("A1", "1");
|
|
|
|
spr.put("A2", "1");
|
2023-12-12 15:00:07 +01:00
|
|
|
spr.put("A3", "123");
|
2023-12-27 03:54:24 +01:00
|
|
|
spr.put("B1", "2");
|
|
|
|
spr.put("B2", "4");
|
|
|
|
spr.put("B3", "1");
|
|
|
|
|
2023-12-18 00:53:59 +01:00
|
|
|
spr.put("B4", "=41+A2");
|
|
|
|
spr.put("C5", "=7*6");
|
|
|
|
spr.put("D1", "=3/2");
|
2023-12-29 20:28:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
progLoad();
|
2023-12-12 15:00:07 +01:00
|
|
|
System.out.println(spr);
|
|
|
|
|
2023-12-18 00:53:59 +01:00
|
|
|
// spr.saveCsv("/tmp/test.csv");
|
2023-12-14 08:31:20 +01:00
|
|
|
|
2023-12-18 00:53:59 +01:00
|
|
|
// DONE: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
|
|
|
|
// TODO ME: Implement program commands
|
2023-12-29 20:28:36 +01:00
|
|
|
|
2023-12-18 00:53:59 +01:00
|
|
|
|
|
|
|
while(true) {
|
|
|
|
String userCommandPositionInput = userCommandPositionInput();
|
|
|
|
if (userCommandPositionInput.charAt(0) == '*'){
|
|
|
|
executeCommand(userCommandPositionInput);
|
2023-12-27 03:54:24 +01:00
|
|
|
System.out.println(spr);
|
2023-12-18 00:53:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String userPositionInput = userCommandPositionInput;
|
|
|
|
|
2023-12-29 20:28:36 +01:00
|
|
|
String userValueFormulaInput = userValueFormulaInput(userPositionInput);
|
2023-12-18 00:53:59 +01:00
|
|
|
|
|
|
|
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: *exit, *save, *load 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
|
2023-12-29 20:28:36 +01:00
|
|
|
if (!(userPositionErrorCheck(userCommandPositionInput, spr))){
|
2023-12-18 00:53:59 +01:00
|
|
|
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: *exit, *save, *load, *help (and all upper case variants)
|
|
|
|
|
|
|
|
CommandToCheck = CommandToCheck.toLowerCase();
|
|
|
|
if (CommandToCheck.equals("*exit") || CommandToCheck.equals("*save") || CommandToCheck.equals("*load") || CommandToCheck.equals("*help")){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-29 20:28:36 +01:00
|
|
|
public static boolean userPositionErrorCheck(String positionToCheck, Spreadsheet spr){
|
2023-12-18 00:53:59 +01:00
|
|
|
//? 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;
|
2023-12-12 15:00:07 +01:00
|
|
|
}
|
|
|
|
|
2023-12-27 03:54:24 +01:00
|
|
|
|
2023-12-18 00:53:59 +01:00
|
|
|
//? User input for a value or a formula
|
2023-12-29 20:28:36 +01:00
|
|
|
public static String userValueFormulaInput(String currentPos){
|
2023-12-18 00:53:59 +01:00
|
|
|
System.out.println();
|
2023-12-29 20:28:36 +01:00
|
|
|
System.out.printf("Input a value of a formula for the selected position: %s.%n", currentPos);
|
2023-12-27 03:54:24 +01:00
|
|
|
System.out.printf("Format for a value: 7 or 1337 %nFormat for a formula: =7*6 or =SUMME(A1:A3)%n");
|
2023-12-18 00:53:59 +01:00
|
|
|
System.out.print("Input: ");
|
|
|
|
String userCommandInput = keyboard.nextLine();
|
|
|
|
|
2023-12-29 20:28:36 +01:00
|
|
|
return userValueFormulaComputation(userCommandInput, currentPos);
|
2023-12-18 00:53:59 +01:00
|
|
|
}
|
|
|
|
|
2023-12-29 20:28:36 +01:00
|
|
|
public static String userValueFormulaComputation(String userValueFormulaInput, String currentPos){
|
2023-12-18 00:53:59 +01:00
|
|
|
if (userValueFormulaInput.isEmpty()){
|
|
|
|
System.out.println("Input is empty!");
|
2023-12-29 20:28:36 +01:00
|
|
|
return userValueFormulaInput(currentPos);
|
2023-12-18 00:53:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//? Formula
|
|
|
|
if (userValueFormulaInput.charAt(0) == '='){
|
|
|
|
if (!(userFormulaErrorCheck(userValueFormulaInput))) {
|
|
|
|
System.out.println("Invalid formula!");
|
2023-12-29 20:28:36 +01:00
|
|
|
return userValueFormulaInput(currentPos);
|
2023-12-18 00:53:59 +01:00
|
|
|
}
|
|
|
|
return userValueFormulaInput;
|
|
|
|
}
|
|
|
|
|
|
|
|
//? Value
|
|
|
|
if (!(userValueErrorCheck(userValueFormulaInput))){
|
|
|
|
System.out.println("Invalid value!");
|
2023-12-29 20:28:36 +01:00
|
|
|
return userValueFormulaInput(currentPos);
|
2023-12-18 00:53:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
//TODO ME: Formula format check, maybe too much work
|
|
|
|
//TODO ME: Check if formula vars are in bounds
|
|
|
|
|
|
|
|
//? 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(")){
|
2023-12-27 03:54:24 +01:00
|
|
|
return userFormulaFunctionErrorCheck(formulaToCheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
return userFormulaExpressionErrorCheck(formulaToCheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean userFormulaFunctionErrorCheck(String functionToCheck){
|
|
|
|
String[] functionCorners = spr.isolateFunctionCorners(functionToCheck);
|
|
|
|
|
|
|
|
if (functionCorners.length != 2){
|
|
|
|
return false;
|
2023-12-18 00:53:59 +01:00
|
|
|
}
|
|
|
|
|
2023-12-27 03:54:24 +01:00
|
|
|
for (String functionCorner: functionCorners){
|
|
|
|
if (!(spr.isValueCellName(functionCorner).equals("cellName"))){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!userPositionBoundsErrorCheck(functionCorner, spr)){
|
2023-12-29 20:28:36 +01:00
|
|
|
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 - ");
|
2023-12-27 03:54:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean userFormulaExpressionErrorCheck(String expressionToCheck){
|
|
|
|
|
2023-12-18 00:53:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean userValueErrorCheck(String valueToCheck){
|
|
|
|
//? true if valid
|
2023-12-27 03:54:24 +01:00
|
|
|
//? valid inputs are: 7, 1337, 0, , -213,...
|
2023-12-18 00:53:59 +01:00
|
|
|
|
2023-12-27 03:54:24 +01:00
|
|
|
//? For floating point numbers
|
|
|
|
// String digitCheckRegex = "-?\\d+(\\.\\d+)?";
|
|
|
|
//? For integers
|
|
|
|
String digitCheckRegex = "-?\\d+";
|
2023-12-18 00:53:59 +01:00
|
|
|
if (!(valueToCheck.matches(digitCheckRegex))){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void executeCommand(String command) {
|
|
|
|
System.out.printf("Executing command: %s%n", command);
|
|
|
|
//TODO ME:
|
2023-12-27 03:54:24 +01:00
|
|
|
switch (command){
|
|
|
|
case "*load":
|
|
|
|
progLoad();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void progLoad(){
|
|
|
|
int rows = spr.getRowsLCount();
|
|
|
|
int cols = spr.getColsCount();
|
|
|
|
|
|
|
|
for (int r = 0; r < rows; r++){
|
|
|
|
for (int c = 0; c < cols; c++){
|
|
|
|
spr.cells[r][c].setValue("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spr.put("A1", "1");
|
|
|
|
spr.put("A2", "2");
|
|
|
|
spr.put("A3", "3");
|
|
|
|
spr.put("A4", "4");
|
|
|
|
spr.put("B1", "5");
|
|
|
|
spr.put("B2", "6");
|
|
|
|
spr.put("B3", "7");
|
|
|
|
spr.put("B4", "8");
|
|
|
|
spr.put("K1", "200");
|
|
|
|
spr.put("K11", "100");
|
|
|
|
spr.put("J4", "4");
|
|
|
|
spr.put("I10", "69");
|
2023-12-29 20:28:36 +01:00
|
|
|
spr.put("F5", "3");
|
|
|
|
spr.put("G4", "2");
|
|
|
|
spr.put("G5", "3");
|
2023-12-27 03:54:24 +01:00
|
|
|
|
2023-12-18 00:53:59 +01:00
|
|
|
}
|
2023-12-12 15:00:07 +01:00
|
|
|
}
|