summaryrefslogtreecommitdiffstats
path: root/app/src/main
diff options
context:
space:
mode:
authormaks <maks@jopek.eu>2022-11-09 08:10:30 +0100
committermaks <maks@jopek.eu>2022-11-09 08:10:30 +0100
commitd9c3799fbd368ea672d41f5b98df168b8191148e (patch)
tree848d5da4c52941b83b0c0bd72321a4291d21cb2c /app/src/main
parent81ff51148575b9ad4bc16ebf18afa5819174c7e6 (diff)
downloadapplicationdevrification-d9c3799fbd368ea672d41f5b98df168b8191148e.tar.gz
applicationdevrification-d9c3799fbd368ea672d41f5b98df168b8191148e.tar.zst
applicationdevrification-d9c3799fbd368ea672d41f5b98df168b8191148e.zip
Add testsHEADmaster
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/com/jopek/applicationdevrification/CodeOption.java13
-rw-r--r--app/src/main/java/com/jopek/applicationdevrification/MainActivity.java105
-rw-r--r--app/src/main/java/com/jopek/applicationdevrification/RegexTester.java78
3 files changed, 120 insertions, 76 deletions
diff --git a/app/src/main/java/com/jopek/applicationdevrification/CodeOption.java b/app/src/main/java/com/jopek/applicationdevrification/CodeOption.java
new file mode 100644
index 0000000..d4b96f2
--- /dev/null
+++ b/app/src/main/java/com/jopek/applicationdevrification/CodeOption.java
@@ -0,0 +1,13 @@
+package com.jopek.applicationdevrification;
+
+public enum CodeOption {
+ zipCode,
+ address,
+ phone,
+ email,
+ pesel,
+ idcard,
+ nameNSurname,
+ password,
+ censorship,
+}
diff --git a/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java b/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java
index e9c9603..1b46be2 100644
--- a/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java
+++ b/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java
@@ -7,7 +7,6 @@ import android.graphics.Color;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
-import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
@@ -21,19 +20,8 @@ import androidx.appcompat.app.AppCompatActivity;
import java.util.regex.Pattern;
-enum CodeOption {
- zipCode,
- address,
- phone,
- email,
- pesel,
- idcard,
- nameNSurname,
- password,
- censorship,
-}
-
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
+ public boolean testing = false;
ArrayAdapter<CharSequence> adapter;
EditText input;
TextView output;
@@ -68,72 +56,37 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
} else if (selOption == CodeOption.zipCode) {
Requests requests = new Requests();
requests.get("https://jopek.eu/maks/get-city?code=" + in, txt -> {
- validateResult(txt.length() != 0, "");
+ validateResult(txt.length() != 0, txt);
});
return;
- } else if (selOption == CodeOption.address) {
- Pattern p = Pattern.compile("^([a-ząśćżźńłęó]+ )+\\d+[a-z]?$", Pattern.CASE_INSENSITIVE);
- result = p.matcher(in).matches();
- } else if (selOption == CodeOption.phone) {
- Pattern p;
- if (in.charAt(0) == '+') {
- p = Pattern.compile("\\+(\\d\\d?\\d?)\\d{9}", Pattern.CASE_INSENSITIVE);
- } else {
- p = Pattern.compile("\\d{7}(\\d{2})?", Pattern.CASE_INSENSITIVE);
- }
- result = p.matcher(in).matches();
- } else if (selOption == CodeOption.email) {
- Pattern p = Pattern.compile("^[a-zA-Z][\\w.]+[\\w]@[a-z]{2,}\\.[a-z]{2,}$");
- result = p.matcher(in).matches();
} else if (selOption == CodeOption.pesel) {
- int lastDigit = Integer.parseInt(in.substring(10, 11));
- int a = Integer.parseInt(in.substring(0, 1));
- int b = Integer.parseInt(in.substring(1, 2));
- int c = Integer.parseInt(in.substring(2, 3));
- int d = Integer.parseInt(in.substring(3, 4));
- int e = Integer.parseInt(in.substring(4, 5));
- int f = Integer.parseInt(in.substring(5, 6));
- int g = Integer.parseInt(in.substring(6, 7));
- int h = Integer.parseInt(in.substring(7, 8));
- int i = Integer.parseInt(in.substring(8, 9));
- int j = Integer.parseInt(in.substring(9, 10));
- int sum = 9 * a + 7 * b + 3 * c + 1 * d + 9 * e + 7 * f + 3 * g + 1 * h + 9 * i + 7 * j;
- result = sum % 10 == lastDigit;
- if (result) {
- int plec = Integer.parseInt(in.substring(9, 10));
- if (plec % 2 == 0 || plec == 0) {
- gender = "K";
- } else {
- gender = "M";
+ if (in.length() != 11) {
+ result = false;
+ } else {
+ int lastDigit = Integer.parseInt(in.substring(10, 11));
+ int a = Integer.parseInt(in.substring(0, 1));
+ int b = Integer.parseInt(in.substring(1, 2));
+ int c = Integer.parseInt(in.substring(2, 3));
+ int d = Integer.parseInt(in.substring(3, 4));
+ int e = Integer.parseInt(in.substring(4, 5));
+ int f = Integer.parseInt(in.substring(5, 6));
+ int g = Integer.parseInt(in.substring(6, 7));
+ int h = Integer.parseInt(in.substring(7, 8));
+ int i = Integer.parseInt(in.substring(8, 9));
+ int j = Integer.parseInt(in.substring(9, 10));
+ int sum = 9 * a + 7 * b + 3 * c + 1 * d + 9 * e + 7 * f + 3 * g + 1 * h + 9 * i + 7 * j;
+ result = sum % 10 == lastDigit;
+ if (result) {
+ int plec = Integer.parseInt(in.substring(9, 10));
+ if (plec % 2 == 0 || plec == 0) {
+ gender = "K";
+ } else {
+ gender = "M";
+ }
}
}
- } else if (selOption == CodeOption.idcard) {
- Pattern p = Pattern.compile(".*\\d.*");
- result = p.matcher(in).matches();
- if (result) {
- p = Pattern.compile("^[a-z][a-z\\d]+$", Pattern.CASE_INSENSITIVE);
- result = p.matcher(in).matches();
- }
- } else if (selOption == CodeOption.nameNSurname) {
- Pattern p = Pattern.compile("^[a-z]+ [a-z]+(-[a-z]+)?$", Pattern.CASE_INSENSITIVE);
- result = p.matcher(in).matches();
- } else if (selOption == CodeOption.password) {
- result = in.matches(".*[A-Z].*");
- Log.d("maks", "validate: " + result);
- result = result && in.matches(".*[a-z].*");
- Log.d("maks", "validate: " + result);
- result = result && in.matches(".*([^\\w\\s]|_).*");
- Log.d("maks", "validate: " + result);
- result = result && in.matches(".*\\d.*");
- Log.d("maks", "validate: " + result);
- result = result && in.matches(".{7,}");
- Log.d("maks", "validate: " + result);
- } else if (selOption == CodeOption.censorship) {
- String[] illegal = new String[]{"wtf", "rabarbar", "kobieta", "bezalkoholowe", "kamienskiego"};
- result = true;
- for (String ill : illegal) {
- result = result && !in.matches(".*" + ill + ".*");
- }
+ } else {
+ result = RegexTester.validate(in, selOption);
}
validateResult(result, gender);
@@ -167,7 +120,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
selOption = CodeOption.address// input.setMinLines(1);
// input.setLines(2);
// input.setMaxLines(10);
-;
+ ;
} else if ("phone".contentEquals(selectedOption)) {
selOption = CodeOption.phone;
} else if ("email".contentEquals(selectedOption)) {
@@ -195,7 +148,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
} else if (selOption == CodeOption.pesel) {
input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
input.setInputType(InputType.TYPE_CLASS_NUMBER);
- } else if (selOption == CodeOption.password || selOption == CodeOption.censorship) {
+ } else if (/* selOption == CodeOption.password || */ selOption == CodeOption.censorship) {
input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
input.setSingleLine(false);
}
diff --git a/app/src/main/java/com/jopek/applicationdevrification/RegexTester.java b/app/src/main/java/com/jopek/applicationdevrification/RegexTester.java
new file mode 100644
index 0000000..da491fc
--- /dev/null
+++ b/app/src/main/java/com/jopek/applicationdevrification/RegexTester.java
@@ -0,0 +1,78 @@
+package com.jopek.applicationdevrification;
+
+import java.util.regex.Pattern;
+
+public class RegexTester {
+ static boolean validate(String in, CodeOption selOption) {
+ String gender = "";
+ boolean result = false;
+ if (in.length() == 0) {
+ result = false;
+ } else if (selOption == CodeOption.zipCode) {
+ return true;
+ } else if (selOption == CodeOption.address) {
+ Pattern p = Pattern.compile("^([a-ząśćżźńłęó]+ )+\\d+[a-z]?$", Pattern.CASE_INSENSITIVE);
+ result = p.matcher(in).matches();
+ } else if (selOption == CodeOption.phone) {
+ Pattern p;
+ if (in.charAt(0) == '+') {
+ p = Pattern.compile("\\+(\\d\\d?\\d?)\\d{9}", Pattern.CASE_INSENSITIVE);
+ } else {
+ p = Pattern.compile("\\d{7}(\\d{2})?", Pattern.CASE_INSENSITIVE);
+ }
+ result = p.matcher(in).matches();
+ } else if (selOption == CodeOption.email) {
+ Pattern p = Pattern.compile("^[a-zA-Z][\\w.]+[\\w]@[a-z]{2,}\\.[a-z]{2,}$");
+ result = p.matcher(in).matches();
+ } else if (selOption == CodeOption.pesel) {
+ if (in.length() != 11) {
+ result = false;
+ } else {
+ int lastDigit = Integer.parseInt(in.substring(10, 11));
+ int a = Integer.parseInt(in.substring(0, 1));
+ int b = Integer.parseInt(in.substring(1, 2));
+ int c = Integer.parseInt(in.substring(2, 3));
+ int d = Integer.parseInt(in.substring(3, 4));
+ int e = Integer.parseInt(in.substring(4, 5));
+ int f = Integer.parseInt(in.substring(5, 6));
+ int g = Integer.parseInt(in.substring(6, 7));
+ int h = Integer.parseInt(in.substring(7, 8));
+ int i = Integer.parseInt(in.substring(8, 9));
+ int j = Integer.parseInt(in.substring(9, 10));
+ int sum = 9 * a + 7 * b + 3 * c + 1 * d + 9 * e + 7 * f + 3 * g + 1 * h + 9 * i + 7 * j;
+ result = sum % 10 == lastDigit;
+ if (result) {
+ int plec = Integer.parseInt(in.substring(9, 10));
+ if (plec % 2 == 0 || plec == 0) {
+ gender = "K";
+ } else {
+ gender = "M";
+ }
+ }
+ }
+ } else if (selOption == CodeOption.idcard) {
+ Pattern p = Pattern.compile(".*\\d.*");
+ result = p.matcher(in).matches();
+ if (result) {
+ p = Pattern.compile("^[a-z][a-z\\d]+$", Pattern.CASE_INSENSITIVE);
+ result = p.matcher(in).matches();
+ }
+ } else if (selOption == CodeOption.nameNSurname) {
+ Pattern p = Pattern.compile("^[a-z]+ [a-z]+(-[a-z]+)?$", Pattern.CASE_INSENSITIVE);
+ result = p.matcher(in).matches();
+ } else if (selOption == CodeOption.password) {
+ result = in.matches(".*[A-Z].*");
+ result = result && in.matches(".*[a-z].*");
+ result = result && in.matches(".*([^\\w\\s]|_).*");
+ result = result && in.matches(".*\\d.*");
+ result = result && in.matches(".{7,}");
+ } else if (selOption == CodeOption.censorship) {
+ String[] illegal = new String[]{"wtf", "rabarbar", "kobieta", "bezalkoholowe", "kamienskiego"};
+ result = true;
+ for (String ill : illegal) {
+ result = result && !in.matches(".*" + ill + ".*");
+ }
+ }
+ return result;
+ }
+}