Browse Source

REFACTORING, mainly extracting multiple try catch blocks to get the code more Readable.

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

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

@ -15,6 +15,9 @@ public class MatrixCalcController {
@FXML @FXML
private TextArea matrixATextArea, matrixBTextArea; private TextArea matrixATextArea, matrixBTextArea;
private MatrixCalcMath math = new MatrixCalcMath();
private MatrixCalcIOUtils util = new MatrixCalcIOUtils();
/** /**
* Konstructor is called before initialize() * Konstructor is called before initialize()
*/ */
@ -32,16 +35,11 @@ public class MatrixCalcController {
* Convert Strings to matricies, multiply them and output the result. * Convert Strings to matricies, multiply them and output the result.
*/ */
multiplyButton.setOnMouseClicked((event) -> { multiplyButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText(); String stringMatrixB = matrixBTextArea.getText();
try {
String[] stringMatrix = { stringMatrixA, stringMatrixB };
} catch (Exception e) {
//TODO: handle exception
}
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB); double[][] matrixB = util.stringToMatrix(stringMatrixB);
@ -55,15 +53,10 @@ public class MatrixCalcController {
}); });
transposeButton.setOnMouseClicked((event) -> { transposeButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
try {
String[] stringMatrix = { stringMatrixA, "" };
} catch (Exception e) {
//TODO: handle exception
}
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] result = math.matrixTransponation(matrixA); double[][] result = math.matrixTransponation(matrixA);
@ -75,16 +68,11 @@ public class MatrixCalcController {
}); });
addButton.setOnMouseClicked((event) -> { addButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText(); String stringMatrixB = matrixBTextArea.getText();
try {
String[] stringMatrix = { stringMatrixA, stringMatrixB };
} catch (Exception e) {
//TODO: handle exception
}
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB); double[][] matrixB = util.stringToMatrix(stringMatrixB);
@ -97,25 +85,11 @@ public class MatrixCalcController {
}); });
substractButton.setOnMouseClicked((event) -> { substractButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText(); String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { stringMatrixA, stringMatrixB };
try {
util.checkInput(stringMatrixA);
} catch (Exception e) {
outputText.setText(e.getMessage() + "A");
outputText.setTextAlignment(TextAlignment.CENTER);
}
try {
util.checkInput(stringMatrixB);
} catch (Exception e) {
outputText.setText(e.getMessage() + "B");
outputText.setTextAlignment(TextAlignment.CENTER);
}
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB); double[][] matrixB = util.stringToMatrix(stringMatrixB);
@ -135,21 +109,13 @@ public class MatrixCalcController {
}); });
DetAButton.setOnMouseClicked((event) -> { DetAButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
String[] stringMatrix = { stringMatrixA, "" };
try {
util.checkInput(stringMatrixA);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixA = util.stringToMatrix(stringMatrixA);
try { try {
double result = math.calcDeterminat(matrixA); double result = math.calcDeterminat(matrixA);
@ -166,20 +132,13 @@ public class MatrixCalcController {
}); });
DetBButton.setOnMouseClicked((event) -> { DetBButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixB = matrixBTextArea.getText(); String stringMatrixB = matrixBTextArea.getText();
try {
util.checkInput(stringMatrixB);
String[] stringMatrix = { "", stringMatrixB };
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixB = util.stringToMatrix(stringMatrixB); double[][] matrixB = util.stringToMatrix(stringMatrixB);
try { try {
double result = math.calcDeterminat(matrixB); double result = math.calcDeterminat(matrixB);
@ -195,4 +154,57 @@ public class MatrixCalcController {
} }
}); });
} }
// TODO Wirte tests for the extracted Methode.
/**
* Checks the Input and Displays it if the Input is Valid.
*
* @param stringMatrix Contains both input matrices if
* numberOfMarriciesToMatch is 2. If the number
* is 1 than one of them has to be a empty String
* @param numberOfMatricesToMatch If the number is 1 onely one Marix will be
* verifyed and the otherone needs to be an empty
* String in the stringMatrix
*/
private void checkInputAndDisplayIfInputIsNotValid(String[] stringMatrix, int numberOfMatricesToMatch) {
if (numberOfMatricesToMatch == 1 && !stringMatrix[0].equals("")) {
try {
util.checkInput(stringMatrix[0]);
} catch (Exception e) {
outputText.setText(e.getMessage() + "A");
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (numberOfMatricesToMatch == 1 && !stringMatrix[1].equals("")) {
try {
util.checkInput(stringMatrix[1]);
} catch (Exception e) {
outputText.setText(e.getMessage() + "B");
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (numberOfMatricesToMatch == 2 && !stringMatrix[0].equals("") && !stringMatrix[1].equals("")) {
try {
util.checkInput(stringMatrix[0]);
} catch (Exception e) {
outputText.setText(e.getMessage() + "A");
outputText.setTextAlignment(TextAlignment.CENTER);
}
try {
util.checkInput(stringMatrix[1]);
} catch (Exception e) {
outputText.setText(e.getMessage() + "B");
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (stringMatrix[0].equals("")) {
outputText.setText("Pease insert MatrixA");
outputText.setTextAlignment(TextAlignment.CENTER);
} else if (stringMatrix[1].equals("")) {
outputText.setText("Pease insert MatrixB");
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
} }
Loading…
Cancel
Save