Browse Source

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

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

159
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);
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, MULTIPLICATION_STRING);
}); });
transposeButton.setOnMouseClicked((event) -> { transposeButton.setOnMouseClicked((event) -> {
@ -64,58 +66,75 @@ public class MatrixCalcController {
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1); checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
try {
double[][] result = math.matrixTransponation(matrixA);
invokeOperation(matricies, TRANPOSE_STRING);
});
String DisplayableString = util.outputMatrixToOutputText(result);
addButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { stringMatrixA, stringMatrixB };
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
matricies.add(util.stringToMatrix(stringMatrixB));
invokeOperation(matricies, ADDITION_STRING);
}); });
addButton.setOnMouseClicked((event) -> {
substractButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText(); String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { stringMatrixA, stringMatrixB }; String[] stringMatrix = { stringMatrixA, stringMatrixB };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2); checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
matricies.add(util.stringToMatrix(stringMatrixB));
try {
double[][] result = math.matrixAddition(matrixA, matrixB);
invokeOperation(matricies, SUBSTRACTION_STRING);
});
String DisplayableString = util.outputMatrixToOutputText(result);
DetAButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText();
String[] stringMatrix = { stringMatrixA, "" };
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
invokeOperation(matricies, CALCDETERMINAT_STRING);
}); });
substractButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText();
DetBButton.setOnMouseClicked((event) -> {
String stringMatrixB = matrixBTextArea.getText(); String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { stringMatrixA, stringMatrixB };
String[] stringMatrix = { "", stringMatrixB };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixB));
invokeOperation(matricies, CALCDETERMINAT_STRING);
});
}
/**
* 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 { try {
double[][] result = math.matrixSubstraction(matrixA, matrixB);
double[][] result = math.matrixMultiplication(matricies.get(0), matricies.get(1));
String DisplayableString = util.outputMatrixToOutputText(result); String DisplayableString = util.outputMatrixToOutputText(result);
@ -126,53 +145,67 @@ public class MatrixCalcController {
outputText.setText(e.getMessage()); outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
});
DetAButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText();
String[] stringMatrix = { stringMatrixA, "" };
} else if (operation.equals(ADDITION_STRING)) {
try {
double[][] result = math.matrixAddition(matricies.get(0), matricies.get(1));
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
String DisplayableString = util.outputMatrixToOutputText(result);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
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 { try {
double result = math.calcDeterminat(matrixA);
double[][] result = math.matrixSubstraction(matricies.get(0), matricies.get(1));
String DisplayableString = Double.toString(result);
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString); outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
outputText.setText(e.getMessage()); outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
});
DetBButton.setOnMouseClicked((event) -> {
String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { "", stringMatrixB };
}
}else if (matricies.size() == 1) {
if (operation.equals(TRANPOSE_STRING)) {
try {
double[][] result = math.matrixTransponation(matricies.get(0));
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
String DisplayableString = util.outputMatrixToOutputText(result);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
if (operation.equals(CALCDETERMINAT_STRING)) {
try { try {
double result = math.calcDeterminat(matrixB);
double result = math.calcDeterminat(matricies.get(0));
String DisplayableString = Double.toString(result); String DisplayableString = Double.toString(result);
outputText.setText(DisplayableString); outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (Exception e){
outputText.setText(e.getMessage()); outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
});
}
}
} }
// TODO Wirte tests for the extracted Methode. // TODO Wirte tests for the extracted Methode.

Loading…
Cancel
Save