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. 216
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java

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

@ -15,6 +15,9 @@ public class MatrixCalcController {
@FXML
private TextArea matrixATextArea, matrixBTextArea;
private MatrixCalcMath math = new MatrixCalcMath();
private MatrixCalcIOUtils util = new MatrixCalcIOUtils();
/**
* Konstructor is called before initialize()
*/
@ -32,167 +35,176 @@ public class MatrixCalcController {
* Convert Strings to matricies, multiply them and output the result.
*/
multiplyButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText();
try {
} catch (Exception e) {
//TODO: handle exception
}
String[] stringMatrix = { stringMatrixA, stringMatrixB };
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
double[][] result = math.matrixMultiplication(matrixA, matrixB);
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
String DisplayableString = util.outputMatrixToOutputText(result);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
double[][] result = math.matrixMultiplication(matrixA, matrixB);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
// System.out.println(matrixATextArea.getText());
});
transposeButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText();
try {
} catch (Exception e) {
//TODO: handle exception
}
String[] stringMatrix = { stringMatrixA, "" };
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] result = math.matrixTransponation(matrixA);
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
String DisplayableString = util.outputMatrixToOutputText(result);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] result = math.matrixTransponation(matrixA);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
});
addButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText();
try {
} catch (Exception e) {
//TODO: handle exception
}
String[] stringMatrix = { stringMatrixA, stringMatrixB };
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
double[][] result = math.matrixAddition(matrixA, matrixB);
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
String DisplayableString = util.outputMatrixToOutputText(result);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
double[][] result = math.matrixAddition(matrixA, matrixB);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
});
substractButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { stringMatrixA, stringMatrixB };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
try {
util.checkInput(stringMatrixA);
} catch (Exception e) {
outputText.setText(e.getMessage() + "A");
outputText.setTextAlignment(TextAlignment.CENTER);
}
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
try {
util.checkInput(stringMatrixB);
} catch (Exception e) {
outputText.setText(e.getMessage() + "B");
outputText.setTextAlignment(TextAlignment.CENTER);
}
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
try {
double[][] result = math.matrixSubstraction(matrixA, matrixB);
double[][] result = math.matrixSubstraction(matrixA, matrixB);
String DisplayableString = util.outputMatrixToOutputText(result);
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (Exception e) {
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (Exception e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
});
DetAButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText();
String[] stringMatrix = { stringMatrixA, "" };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
try {
double result = math.calcDeterminat(matrixA);
String DisplayableString = Double.toString(result);
util.checkInput(stringMatrixA);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
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) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
});
DetBButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { "", stringMatrixB };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
try {
double result = math.calcDeterminat(matrixB);
util.checkInput(stringMatrixB);
String DisplayableString = Double.toString(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
});
}
double[][] matrixB = util.stringToMatrix(stringMatrixB);
try {
double result = math.calcDeterminat(matrixB);
// 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);
}
String DisplayableString = Double.toString(result);
try {
util.checkInput(stringMatrix[1]);
} catch (Exception e) {
outputText.setText(e.getMessage() + "B");
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (stringMatrix[0].equals("")) {
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
outputText.setText("Pease insert MatrixA");
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
} else if (stringMatrix[1].equals("")) {
outputText.setText("Pease insert MatrixB");
outputText.setTextAlignment(TextAlignment.CENTER);
}
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
});
}
}
Loading…
Cancel
Save