Browse Source

Adding CheckInput for a matrix with basic checks

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
5f8198e046
  1. 40
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java
  2. 49
      src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java

40
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;
}
}

49
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);
}
}
Loading…
Cancel
Save