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.

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