From 4d92178abe5b8367c4e816c8d0e69994f8a38717 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Mon, 15 Jul 2019 08:06:31 +0200 Subject: [PATCH] Refactoring, mostly extracting code --- .../matrixcalc/MatrixCalcController.java | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java index bd7fbc1..f307792 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java @@ -39,25 +39,43 @@ public class MatrixCalcController { String stringMatrixB = matrixBTextArea.getText(); if (checkInput(stringMatrixA) && checkInput(stringMatrixB)) { MatrixCalcMath math = new MatrixCalcMath(); + double[][] matrixA = math.stringToMatrix(stringMatrixA); double[][] matrixB = math.stringToMatrix(stringMatrixB); double[][] result = math.matrixMultiplication(matrixA, matrixB); - String resultString = ""; - for (int i = 0; i < result.length; i++) { - for (int j = 0; j < result[0].length; j++) { - resultString += result[i][j] + " "; - //System.out.println(result[i][j]); - } - resultString += "\n\n"; - } - //Display output - outputText.setText(resultString); - outputText.setTextAlignment(TextAlignment.CENTER); + outputMatrixToOutputText(result); } // System.out.println(matrixATextArea.getText()); }); } + /** + * Prints a given 2D-Array to the output text Field. + * @param output2DArray The Array that gets Displayed + */ + private void outputMatrixToOutputText(double[][] output2DArray) { + //convert array to String + String DisplayableString = convertsArrayToStringInOrderToDisplayIt(output2DArray); + // Display output + outputText.setText(DisplayableString); + outputText.setTextAlignment(TextAlignment.CENTER); + } + /** + * Converts Array to String in oder to Display it. + * @param array2D the array wich will be converted to an Displayable String + * @return The Displayable String + */ + private String convertsArrayToStringInOrderToDisplayIt(double[][] array2D) { + String displayableString = ""; + for (int i = 0; i < array2D.length; i++) { + for (int j = 0; j < array2D[0].length; j++) { + displayableString += array2D[i][j] + " "; + // System.out.println(result[i][j]); + } + displayableString += "\n\n"; + } + return displayableString; + } /** * Chcks if the Input is Valid, with Regex. Returns true if the Matrix can be