Ultra Geile Studenten Benutzer Oberfläche (UGSBO)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
8.5 KiB

  1. package com.ugsbo.matrixcalc;
  2. import javafx.fxml.FXML;
  3. import javafx.scene.control.*;
  4. import javafx.scene.text.Text;
  5. import javafx.scene.text.TextAlignment;
  6. public class MatrixCalcController {
  7. // Hier werden die fx:id Attribute verknuepft.
  8. @FXML
  9. private Button multiplyButton, addButton, DetAButton, DetBButton, substractButton, transposeButton;
  10. @FXML
  11. private Text errorText, outputText;
  12. @FXML
  13. private TextArea matrixATextArea, matrixBTextArea;
  14. private MatrixCalcMath math = new MatrixCalcMath();
  15. private MatrixCalcIOUtils util = new MatrixCalcIOUtils();
  16. /**
  17. * Konstructor is called before initialize()
  18. */
  19. public MatrixCalcController() {
  20. // Setup of some Fields could be defined here.
  21. }
  22. /**
  23. * Initializes the controller class. This method is automatically called after
  24. * the fxml file has been loaded.
  25. */
  26. @FXML
  27. public void initialize() {
  28. /**
  29. * Convert Strings to matricies, multiply them and output the result.
  30. */
  31. multiplyButton.setOnMouseClicked((event) -> {
  32. String stringMatrixA = matrixATextArea.getText();
  33. String stringMatrixB = matrixBTextArea.getText();
  34. String[] stringMatrix = { stringMatrixA, stringMatrixB };
  35. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
  36. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  37. double[][] matrixB = util.stringToMatrix(stringMatrixB);
  38. try {
  39. double[][] result = math.matrixMultiplication(matrixA, matrixB);
  40. String DisplayableString = util.outputMatrixToOutputText(result);
  41. outputText.setText(DisplayableString);
  42. outputText.setTextAlignment(TextAlignment.CENTER);
  43. } catch (IllegalArgumentException e) {
  44. outputText.setText(e.getMessage());
  45. outputText.setTextAlignment(TextAlignment.CENTER);
  46. }
  47. });
  48. transposeButton.setOnMouseClicked((event) -> {
  49. String stringMatrixA = matrixATextArea.getText();
  50. String[] stringMatrix = { stringMatrixA, "" };
  51. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
  52. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  53. try {
  54. double[][] result = math.matrixTransponation(matrixA);
  55. String DisplayableString = util.outputMatrixToOutputText(result);
  56. outputText.setText(DisplayableString);
  57. outputText.setTextAlignment(TextAlignment.CENTER);
  58. } catch (IllegalArgumentException e) {
  59. outputText.setText(e.getMessage());
  60. outputText.setTextAlignment(TextAlignment.CENTER);
  61. }
  62. });
  63. addButton.setOnMouseClicked((event) -> {
  64. String stringMatrixA = matrixATextArea.getText();
  65. String stringMatrixB = matrixBTextArea.getText();
  66. String[] stringMatrix = { stringMatrixA, stringMatrixB };
  67. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
  68. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  69. double[][] matrixB = util.stringToMatrix(stringMatrixB);
  70. try {
  71. double[][] result = math.matrixAddition(matrixA, matrixB);
  72. String DisplayableString = util.outputMatrixToOutputText(result);
  73. outputText.setText(DisplayableString);
  74. outputText.setTextAlignment(TextAlignment.CENTER);
  75. } catch (IllegalArgumentException e) {
  76. outputText.setText(e.getMessage());
  77. outputText.setTextAlignment(TextAlignment.CENTER);
  78. }
  79. });
  80. substractButton.setOnMouseClicked((event) -> {
  81. String stringMatrixA = matrixATextArea.getText();
  82. String stringMatrixB = matrixBTextArea.getText();
  83. String[] stringMatrix = { stringMatrixA, stringMatrixB };
  84. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
  85. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  86. double[][] matrixB = util.stringToMatrix(stringMatrixB);
  87. try {
  88. double[][] result = math.matrixSubstraction(matrixA, matrixB);
  89. String DisplayableString = util.outputMatrixToOutputText(result);
  90. outputText.setText(DisplayableString);
  91. outputText.setTextAlignment(TextAlignment.CENTER);
  92. } catch (IllegalArgumentException e) {
  93. outputText.setText(e.getMessage());
  94. outputText.setTextAlignment(TextAlignment.CENTER);
  95. }
  96. });
  97. DetAButton.setOnMouseClicked((event) -> {
  98. String stringMatrixA = matrixATextArea.getText();
  99. String[] stringMatrix = { stringMatrixA, "" };
  100. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
  101. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  102. try {
  103. double result = math.calcDeterminat(matrixA);
  104. String DisplayableString = Double.toString(result);
  105. outputText.setText(DisplayableString);
  106. outputText.setTextAlignment(TextAlignment.CENTER);
  107. } catch (IllegalArgumentException e) {
  108. outputText.setText(e.getMessage());
  109. outputText.setTextAlignment(TextAlignment.CENTER);
  110. }
  111. });
  112. DetBButton.setOnMouseClicked((event) -> {
  113. String stringMatrixB = matrixBTextArea.getText();
  114. String[] stringMatrix = { "", stringMatrixB };
  115. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
  116. double[][] matrixB = util.stringToMatrix(stringMatrixB);
  117. try {
  118. double result = math.calcDeterminat(matrixB);
  119. String DisplayableString = Double.toString(result);
  120. outputText.setText(DisplayableString);
  121. outputText.setTextAlignment(TextAlignment.CENTER);
  122. } catch (IllegalArgumentException e) {
  123. outputText.setText(e.getMessage());
  124. outputText.setTextAlignment(TextAlignment.CENTER);
  125. }
  126. });
  127. }
  128. // TODO Wirte tests for the extracted Methode.
  129. /**
  130. * Checks the Input and Displays it if the Input is Valid.
  131. *
  132. * @param stringMatrix Contains both input matrices if
  133. * numberOfMarriciesToMatch is 2. If the number
  134. * is 1 than one of them has to be a empty String
  135. * @param numberOfMatricesToMatch If the number is 1 onely one Marix will be
  136. * verifyed and the otherone needs to be an empty
  137. * String in the stringMatrix
  138. */
  139. private void checkInputAndDisplayIfInputIsNotValid(String[] stringMatrix, int numberOfMatricesToMatch) {
  140. if (numberOfMatricesToMatch == 1 && !stringMatrix[0].equals("")) {
  141. try {
  142. util.checkInput(stringMatrix[0]);
  143. } catch (Exception e) {
  144. outputText.setText(e.getMessage() + "A");
  145. outputText.setTextAlignment(TextAlignment.CENTER);
  146. }
  147. } else if (numberOfMatricesToMatch == 1 && !stringMatrix[1].equals("")) {
  148. try {
  149. util.checkInput(stringMatrix[1]);
  150. } catch (Exception e) {
  151. outputText.setText(e.getMessage() + "B");
  152. outputText.setTextAlignment(TextAlignment.CENTER);
  153. }
  154. } else if (numberOfMatricesToMatch == 2 && !stringMatrix[0].equals("") && !stringMatrix[1].equals("")) {
  155. try {
  156. util.checkInput(stringMatrix[0]);
  157. } catch (Exception e) {
  158. outputText.setText(e.getMessage() + "A");
  159. outputText.setTextAlignment(TextAlignment.CENTER);
  160. }
  161. try {
  162. util.checkInput(stringMatrix[1]);
  163. } catch (Exception e) {
  164. outputText.setText(e.getMessage() + "B");
  165. outputText.setTextAlignment(TextAlignment.CENTER);
  166. }
  167. } else if (stringMatrix[0].equals("")) {
  168. outputText.setText("Pease insert MatrixA");
  169. outputText.setTextAlignment(TextAlignment.CENTER);
  170. } else if (stringMatrix[1].equals("")) {
  171. outputText.setText("Pease insert MatrixB");
  172. outputText.setTextAlignment(TextAlignment.CENTER);
  173. }
  174. }
  175. }