From 5f8198e0467f230d997de28e7af4e9cfd95c8c61 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Sat, 6 Jul 2019 16:32:00 +0200 Subject: [PATCH] Adding CheckInput for a matrix with basic checks --- .../matrixcalc/MatrixCalcController.java | 40 +++++++++++++-- .../matrixcalc/MatrixInputcheckTest.java | 49 +++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java index f8e1cdb..3dd8596 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java @@ -3,6 +3,8 @@ package com.ugsbo.matrixcalc; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.text.Text; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class MatrixCalcController { @@ -28,12 +30,40 @@ public class MatrixCalcController { @FXML public void initialize() { multiplyButton.setOnMouseClicked((event) -> { - // TODO matrixATextArea and matrixBTextArea need to be parsed to double[][] do - // this in an extern Methode maybe an extern class. - // MatrixCalcMath math = new MatrixCalcMath(); - // math.matrixMultiplication(matrixATextArea, matrixATextArea); - System.out.println(event); + String matrixA = matrixATextArea.getText(); + String matrixB = matrixBTextArea.getText(); + if (checkInput(matrixA)) { + // TODO matrixATextArea and matrixBTextArea need to be parsed to double[][] do + // this in an extern Methode maybe an extern class. + // MatrixCalcMath math = new MatrixCalcMath(); + // math.matrixMultiplication(matrixATextArea, matrixATextArea); + } + // System.out.println(matrixATextArea.getText()); }); } + public boolean checkInput(String matrix) { + boolean isMatched = false; + if (matrix.length() == 0) { + throw new IllegalArgumentException("Please insert a Matrix"); + } + + // Matches digits witch following spaces 1 to 3 times + String row1 = "(\\d*\\u0020*){1,3}"; + // Matches newlineCurrierReturn followed by digits witch following spaces 1 to + // 3times + String row2 = "(\\n){0,3}(\\d*\\u0020*){0,3}"; + String row3 = "(\\n){0,3}(\\d*\\u0020*){0,3}"; + + // TODO get the input check more stricktly missing matrix slots are allowed. + + Pattern p = Pattern.compile(row1 + row2 + row3); + Matcher m = p.matcher(matrix); + + isMatched = m.matches(); + + // System.out.println(isMatched); + return isMatched; + } + } \ No newline at end of file diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java new file mode 100644 index 0000000..483e000 --- /dev/null +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java @@ -0,0 +1,49 @@ +package com.ugsbo.matrixcalc; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Tests how well worng input can be determent. + */ +public class MatrixInputcheckTest { + + @Test(expected = IllegalArgumentException.class) + public void inputEmptySouldThrowAIllegalArgumentException() { + MatrixCalcController contr = new MatrixCalcController(); + String input = ""; + + contr.checkInput(input); + } + + @Test + public void checkIfA1by3MatrixIsMatched(){ + MatrixCalcController contr = new MatrixCalcController(); + String input = "1 2 3"; + + boolean result = contr.checkInput(input); + + assertTrue("The 1 by 3 Matrix was not Matched but it should be.", result); + } + + @Test + public void checkIfA2by3MatrixIsMatched(){ + MatrixCalcController contr = new MatrixCalcController(); + String input = "1 2 3\n1 2 3"; + + boolean result = contr.checkInput(input); + + assertTrue("The 2 by 3 Matrix was not Matched but it should be.", result); + } + + @Test + public void checkIfA3by3MatrixIsMatched(){ + MatrixCalcController contr = new MatrixCalcController(); + String input = "1 2 3\n1 2 3\n1 2 3"; + + boolean result = contr.checkInput(input); + + assertTrue("The 3 by 3 Matrix was not Matched but it should be.", result); + } +} \ No newline at end of file