Browse Source

REFACTORING, extracted more form the event Heandler to get even more Readabillity

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
2077778546
  1. 191
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java

191
src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java

@ -5,8 +5,20 @@ import javafx.scene.control.*;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment; import javafx.scene.text.TextAlignment;
import java.util.ArrayList;
public class MatrixCalcController { public class MatrixCalcController {
/**
*
*/
private static final String MULTIPLICATION_STRING = "multiplication";
private static final String ADDITION_STRING = "addition";
private static final String SUBSTRACTION_STRING = "substract";
private static final String TRANPOSE_STRING = "transpose";
private static final String CALCDETERMINAT_STRING = "calcDeterminate";
// Hier werden die fx:id Attribute verknuepft. // Hier werden die fx:id Attribute verknuepft.
@FXML @FXML
private Button multiplyButton, addButton, DetAButton, DetBButton, substractButton, transposeButton; private Button multiplyButton, addButton, DetAButton, DetBButton, substractButton, transposeButton;
@ -41,21 +53,11 @@ public class MatrixCalcController {
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2); checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
try {
double[][] result = math.matrixMultiplication(matrixA, matrixB);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
matricies.add(util.stringToMatrix(stringMatrixB));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
invokeOperation(matricies, MULTIPLICATION_STRING);
}); });
transposeButton.setOnMouseClicked((event) -> { transposeButton.setOnMouseClicked((event) -> {
@ -64,20 +66,10 @@ public class MatrixCalcController {
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1); checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
try {
double[][] result = math.matrixTransponation(matrixA);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
invokeOperation(matricies, TRANPOSE_STRING);
}); });
addButton.setOnMouseClicked((event) -> { addButton.setOnMouseClicked((event) -> {
@ -87,21 +79,11 @@ public class MatrixCalcController {
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2); checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
try {
double[][] result = math.matrixAddition(matrixA, matrixB);
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
matricies.add(util.stringToMatrix(stringMatrixB));
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
invokeOperation(matricies, ADDITION_STRING);
}); });
substractButton.setOnMouseClicked((event) -> { substractButton.setOnMouseClicked((event) -> {
@ -111,21 +93,11 @@ public class MatrixCalcController {
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2); checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
try {
double[][] result = math.matrixSubstraction(matrixA, matrixB);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
matricies.add(util.stringToMatrix(stringMatrixB));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
invokeOperation(matricies, SUBSTRACTION_STRING);
}); });
DetAButton.setOnMouseClicked((event) -> { DetAButton.setOnMouseClicked((event) -> {
@ -134,21 +106,10 @@ public class MatrixCalcController {
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1); checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
try {
double result = math.calcDeterminat(matrixA);
String DisplayableString = Double.toString(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
invokeOperation(matricies, CALCDETERMINAT_STRING);
}); });
DetBButton.setOnMouseClicked((event) -> { DetBButton.setOnMouseClicked((event) -> {
@ -157,22 +118,94 @@ public class MatrixCalcController {
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1); checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixB));
try {
double result = math.calcDeterminat(matrixB);
invokeOperation(matricies, CALCDETERMINAT_STRING);
});
}
String DisplayableString = Double.toString(result);
/**
* Invokes the right operations form the MatrixCalcMath class
* @param matricies contains both or onely one Matrix
* @param operation One of the Global Constats to select wich Operation is needed.
*/
private void invokeOperation(ArrayList<double[][]> matricies, String operation) {
if (matricies.size() == 2) {
if (operation.equals(MULTIPLICATION_STRING)) {
try {
double[][] result = math.matrixMultiplication(matricies.get(0), matricies.get(1));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (operation.equals(ADDITION_STRING)) {
try {
double[][] result = math.matrixAddition(matricies.get(0), matricies.get(1));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (operation.equals(SUBSTRACTION_STRING)) {
try {
double[][] result = math.matrixSubstraction(matricies.get(0), matricies.get(1));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
}else if (matricies.size() == 1) {
if (operation.equals(TRANPOSE_STRING)) {
try {
double[][] result = math.matrixTransponation(matricies.get(0));
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
String DisplayableString = util.outputMatrixToOutputText(result);
} catch (IllegalArgumentException e) {
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
} }
});
if (operation.equals(CALCDETERMINAT_STRING)) {
try {
double result = math.calcDeterminat(matricies.get(0));
String DisplayableString = Double.toString(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (Exception e){
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
}
} }
// TODO Wirte tests for the extracted Methode. // TODO Wirte tests for the extracted Methode.

Loading…
Cancel
Save