-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidation.java
More file actions
91 lines (80 loc) · 3.6 KB
/
Validation.java
File metadata and controls
91 lines (80 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package CryptarithmeticSolver;
import java.util.regex.Pattern;
import javafx.scene.control.Alert;
public final class Validation {
boolean isValid = false;
final Pattern pattern = Pattern.compile("^[A-Za-z, ]++$");
/*
constructor checks if there are any invalid characters
in the input strings and changes the flag isValid accordingly
*/
public Validation(String input1, String input2) {
boolean temp1 = pattern.matcher(input1).matches();
boolean temp2 = pattern.matcher(input2).matches();
if (temp1 && temp2) {
isValid = true;
}
}
/*
funtion to make alert box
*/
public void makeErrorAlert(String alertMessage) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Error!");
alert.setHeaderText("Invalid Input");
alert.setContentText(alertMessage);
alert.showAndWait();
}
//check if the input is valid after preparing the input into words
public void postPreperation(Preperation problem) {
//check if any of the words are too long
switch (problem.operator) {
//validation for addition operation
case '+':
int longestPositiveWordIndex = 0;
//for every word from the addents,
for (int i = 0; i < problem.wordsArray.length; i++) {
//check if it is longer than the current longest word and update if it is not
if (problem.wordsArray[longestPositiveWordIndex].length() < problem.wordsArray[i].length()) {
longestPositiveWordIndex = i;
}
//check if it is longer than the summation word
if (problem.wordsArray[i].length() > problem.summationLetters.length) {
makeErrorAlert("The summation word needs to be the longest word.");
isValid = false;
}
}
//check if the summation word is too long#
if (problem.summationLetters.length > problem.wordsArray[longestPositiveWordIndex].length() + 1) {
makeErrorAlert("The summation word is too long");
isValid = false;
}
break;
//validation for substraction operation
case '-':
int longestNegativeWordIndex = 0;
//for every word from the addents,
for (int i = 0; i < problem.wordsArray.length; i++) {
//check if it is longer than the current longest word and update if it is not
if (problem.wordsArray[longestNegativeWordIndex].length() < problem.wordsArray[i].length()) {
longestNegativeWordIndex = i;
}
}
//check if the summation word is too long#
if (problem.summationLetters.length > problem.wordsArray[longestNegativeWordIndex].length()) {
makeErrorAlert("The summation word is too long");
isValid = false;
}
break;
}
/*
check if there are too many characters or not
*/
if (problem.uniqueLettersList.size() > Integer.parseInt(problem.base)) {
makeErrorAlert("Too many unique characters. No more than the base is allowed");
isValid = false;
}
System.out.println("unique Letters: " + problem.uniqueLettersList);
System.out.println("Integer.parseInt(problem.base): " + Integer.parseInt(problem.base));
}
}