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.

262 lines
10 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. import java.util.ArrayList;
  7. public class MatrixCalcController {
  8. /**
  9. *
  10. */
  11. private static final String MULTIPLICATION_STRING = "multiplication";
  12. private static final String ADDITION_STRING = "addition";
  13. private static final String SUBSTRACTION_STRING = "substract";
  14. private static final String TRANPOSE_STRING = "transpose";
  15. private static final String CALCDETERMINAT_STRING = "calcDeterminate";
  16. // Hier werden die fx:id Attribute verknuepft.
  17. @FXML
  18. private Button multiplyButton, addButton, DetAButton, DetBButton, substractButton, transposeButton;
  19. @FXML
  20. private Text errorText, outputText;
  21. @FXML
  22. private TextArea matrixATextArea, matrixBTextArea;
  23. private MatrixCalcMath math = new MatrixCalcMath();
  24. private MatrixCalcIOUtils util = new MatrixCalcIOUtils();
  25. /**
  26. * Konstructor is called before initialize()
  27. */
  28. public MatrixCalcController() {
  29. // Setup of some Fields could be defined here.
  30. }
  31. /**
  32. * Initializes the controller class. This method is automatically called after
  33. * the fxml file has been loaded.
  34. */
  35. @FXML
  36. public void initialize() {
  37. /**
  38. * Convert Strings to matricies, multiply them and output the result.
  39. */
  40. multiplyButton.setOnMouseClicked((event) -> {
  41. String stringMatrixA = matrixATextArea.getText();
  42. String stringMatrixB = matrixBTextArea.getText();
  43. String[] stringMatrix = { stringMatrixA, stringMatrixB };
  44. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
  45. ArrayList<double[][]> matricies = new ArrayList<double[][]>();
  46. matricies.add(util.stringToMatrix(stringMatrixA));
  47. matricies.add(util.stringToMatrix(stringMatrixB));
  48. invokeOperation(matricies, MULTIPLICATION_STRING);
  49. });
  50. transposeButton.setOnMouseClicked((event) -> {
  51. String stringMatrixA = matrixATextArea.getText();
  52. String[] stringMatrix = { stringMatrixA, "" };
  53. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
  54. ArrayList<double[][]> matricies = new ArrayList<double[][]>();
  55. matricies.add(util.stringToMatrix(stringMatrixA));
  56. invokeOperation(matricies, TRANPOSE_STRING);
  57. });
  58. addButton.setOnMouseClicked((event) -> {
  59. String stringMatrixA = matrixATextArea.getText();
  60. String stringMatrixB = matrixBTextArea.getText();
  61. String[] stringMatrix = { stringMatrixA, stringMatrixB };
  62. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
  63. ArrayList<double[][]> matricies = new ArrayList<double[][]>();
  64. matricies.add(util.stringToMatrix(stringMatrixA));
  65. matricies.add(util.stringToMatrix(stringMatrixB));
  66. invokeOperation(matricies, ADDITION_STRING);
  67. });
  68. substractButton.setOnMouseClicked((event) -> {
  69. String stringMatrixA = matrixATextArea.getText();
  70. String stringMatrixB = matrixBTextArea.getText();
  71. String[] stringMatrix = { stringMatrixA, stringMatrixB };
  72. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
  73. ArrayList<double[][]> matricies = new ArrayList<double[][]>();
  74. matricies.add(util.stringToMatrix(stringMatrixA));
  75. matricies.add(util.stringToMatrix(stringMatrixB));
  76. invokeOperation(matricies, SUBSTRACTION_STRING);
  77. });
  78. DetAButton.setOnMouseClicked((event) -> {
  79. String stringMatrixA = matrixATextArea.getText();
  80. String[] stringMatrix = { stringMatrixA, "" };
  81. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
  82. ArrayList<double[][]> matricies = new ArrayList<double[][]>();
  83. matricies.add(util.stringToMatrix(stringMatrixA));
  84. invokeOperation(matricies, CALCDETERMINAT_STRING);
  85. });
  86. DetBButton.setOnMouseClicked((event) -> {
  87. String stringMatrixB = matrixBTextArea.getText();
  88. String[] stringMatrix = { "", stringMatrixB };
  89. checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
  90. ArrayList<double[][]> matricies = new ArrayList<double[][]>();
  91. matricies.add(util.stringToMatrix(stringMatrixB));
  92. invokeOperation(matricies, CALCDETERMINAT_STRING);
  93. });
  94. }
  95. /**
  96. * Invokes the right operations form the MatrixCalcMath class
  97. * @param matricies contains both or onely one Matrix
  98. * @param operation One of the Global Constats to select wich Operation is needed.
  99. */
  100. private void invokeOperation(ArrayList<double[][]> matricies, String operation) {
  101. if (matricies.size() == 2) {
  102. if (operation.equals(MULTIPLICATION_STRING)) {
  103. try {
  104. double[][] result = math.matrixMultiplication(matricies.get(0), matricies.get(1));
  105. String DisplayableString = util.outputMatrixToOutputText(result);
  106. outputText.setText(DisplayableString);
  107. outputText.setTextAlignment(TextAlignment.CENTER);
  108. } catch (IllegalArgumentException e) {
  109. outputText.setText(e.getMessage());
  110. outputText.setTextAlignment(TextAlignment.CENTER);
  111. }
  112. } else if (operation.equals(ADDITION_STRING)) {
  113. try {
  114. double[][] result = math.matrixAddition(matricies.get(0), matricies.get(1));
  115. String DisplayableString = util.outputMatrixToOutputText(result);
  116. outputText.setText(DisplayableString);
  117. outputText.setTextAlignment(TextAlignment.CENTER);
  118. } catch (IllegalArgumentException e) {
  119. outputText.setText(e.getMessage());
  120. outputText.setTextAlignment(TextAlignment.CENTER);
  121. }
  122. } else if (operation.equals(SUBSTRACTION_STRING)) {
  123. try {
  124. double[][] result = math.matrixSubstraction(matricies.get(0), matricies.get(1));
  125. String DisplayableString = util.outputMatrixToOutputText(result);
  126. outputText.setText(DisplayableString);
  127. outputText.setTextAlignment(TextAlignment.CENTER);
  128. } catch (IllegalArgumentException e) {
  129. outputText.setText(e.getMessage());
  130. outputText.setTextAlignment(TextAlignment.CENTER);
  131. }
  132. }
  133. }else if (matricies.size() == 1) {
  134. if (operation.equals(TRANPOSE_STRING)) {
  135. try {
  136. double[][] result = math.matrixTransponation(matricies.get(0));
  137. String DisplayableString = util.outputMatrixToOutputText(result);
  138. outputText.setText(DisplayableString);
  139. outputText.setTextAlignment(TextAlignment.CENTER);
  140. } catch (IllegalArgumentException e) {
  141. outputText.setText(e.getMessage());
  142. outputText.setTextAlignment(TextAlignment.CENTER);
  143. }
  144. }
  145. if (operation.equals(CALCDETERMINAT_STRING)) {
  146. try {
  147. double result = math.calcDeterminat(matricies.get(0));
  148. String DisplayableString = Double.toString(result);
  149. outputText.setText(DisplayableString);
  150. outputText.setTextAlignment(TextAlignment.CENTER);
  151. } catch (IllegalArgumentException e) {
  152. outputText.setText(e.getMessage());
  153. outputText.setTextAlignment(TextAlignment.CENTER);
  154. } catch (Exception e){
  155. outputText.setText(e.getMessage());
  156. outputText.setTextAlignment(TextAlignment.CENTER);
  157. }
  158. }
  159. }
  160. }
  161. // TODO Wirte tests for the extracted Methode.
  162. /**
  163. * Checks the Input and Displays it if the Input is Valid.
  164. *
  165. * @param stringMatrix Contains both input matrices if
  166. * numberOfMarriciesToMatch is 2. If the number
  167. * is 1 than one of them has to be a empty String
  168. * @param numberOfMatricesToMatch If the number is 1 onely one Marix will be
  169. * verifyed and the otherone needs to be an empty
  170. * String in the stringMatrix
  171. */
  172. private void checkInputAndDisplayIfInputIsNotValid(String[] stringMatrix, int numberOfMatricesToMatch) {
  173. if (numberOfMatricesToMatch == 1 && !stringMatrix[0].equals("")) {
  174. try {
  175. util.checkInput(stringMatrix[0]);
  176. } catch (Exception e) {
  177. outputText.setText(e.getMessage() + "A");
  178. outputText.setTextAlignment(TextAlignment.CENTER);
  179. }
  180. } else if (numberOfMatricesToMatch == 1 && !stringMatrix[1].equals("")) {
  181. try {
  182. util.checkInput(stringMatrix[1]);
  183. } catch (Exception e) {
  184. outputText.setText(e.getMessage() + "B");
  185. outputText.setTextAlignment(TextAlignment.CENTER);
  186. }
  187. } else if (numberOfMatricesToMatch == 2 && !stringMatrix[0].equals("") && !stringMatrix[1].equals("")) {
  188. try {
  189. util.checkInput(stringMatrix[0]);
  190. } catch (Exception e) {
  191. outputText.setText(e.getMessage() + "A");
  192. outputText.setTextAlignment(TextAlignment.CENTER);
  193. }
  194. try {
  195. util.checkInput(stringMatrix[1]);
  196. } catch (Exception e) {
  197. outputText.setText(e.getMessage() + "B");
  198. outputText.setTextAlignment(TextAlignment.CENTER);
  199. }
  200. } else if (stringMatrix[0].equals("")) {
  201. outputText.setText("Pease insert MatrixA");
  202. outputText.setTextAlignment(TextAlignment.CENTER);
  203. } else if (stringMatrix[1].equals("")) {
  204. outputText.setText("Pease insert MatrixB");
  205. outputText.setTextAlignment(TextAlignment.CENTER);
  206. }
  207. }
  208. }