Skip to content

Commit fd9eccb

Browse files
committed
Merge branch 'feature/add_lib' into develop
2 parents 7de54e5 + bf9c203 commit fd9eccb

File tree

6 files changed

+287
-1
lines changed

6 files changed

+287
-1
lines changed

polishIdentifiersUtils/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build
2+
/out

polishIdentifiersUtils/build.gradle

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apply plugin: 'java-library'
2+
apply plugin: 'kotlin'
3+
4+
dependencies {
5+
implementation fileTree(dir: 'libs', include: ['*.jar'])
6+
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
7+
compile 'junit:junit:4.12'
8+
}
9+
10+
sourceCompatibility = "1.7"
11+
targetCompatibility = "1.7"
12+
buildscript {
13+
ext.kotlin_version = '1.2.10'
14+
repositories {
15+
mavenCentral()
16+
}
17+
dependencies {
18+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
19+
}
20+
}
21+
repositories {
22+
mavenCentral()
23+
}
24+
compileKotlin {
25+
kotlinOptions {
26+
jvmTarget = "1.8"
27+
}
28+
}
29+
compileTestKotlin {
30+
kotlinOptions {
31+
jvmTarget = "1.8"
32+
}
33+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (C) 2017 Mateusz Utkała
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.utkala.polishidentifiersutils
18+
19+
class PeselUtil(private val pesel: String) {
20+
enum class Gender { MALE, FEMALE }
21+
22+
private val WEIGHTS = listOf(1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1)
23+
var error: ValidatorError? = null
24+
private var valid = false
25+
26+
fun isValid(): Boolean {
27+
return if (valid || checkInput())
28+
validate()
29+
else
30+
false
31+
}
32+
33+
fun getGender(): Gender? {
34+
return if (isValid()) {
35+
if (pesel[9].toInt() % 2 != 0) Gender.MALE else Gender.FEMALE
36+
} else
37+
null
38+
}
39+
40+
fun getBirthDate(): String? {
41+
if (isValid()) {
42+
var year = Integer.parseInt(pesel.substring(0, 2), 10)
43+
var month = Integer.parseInt(pesel.substring(2, 4), 10)
44+
val day = Integer.parseInt(pesel.substring(4, 6), 10)
45+
46+
when {
47+
month > 80 -> {
48+
year += 1800
49+
month -= 80
50+
}
51+
month > 60 -> {
52+
year += 2200
53+
month -= 60
54+
}
55+
month > 40 -> {
56+
year += 2100
57+
month -= 40
58+
}
59+
month > 20 -> {
60+
year += 2000
61+
month -= 20
62+
}
63+
else -> year += 1900
64+
}
65+
66+
return year.toString() + "-" + String.format("%02d", month) + "-" + String.format("%02d", day)
67+
} else {
68+
return null
69+
}
70+
}
71+
72+
private fun checkInput(): Boolean {
73+
if (pesel.isBlank() || pesel.length != 11) {
74+
error = ValidatorError.WRONG_LENGTH
75+
return false
76+
}
77+
78+
if (!pesel.matches(Regex("^\\d{11}$"))) {
79+
error = ValidatorError.INVALID_INPUT
80+
return false
81+
}
82+
83+
return true
84+
}
85+
86+
private fun validate(): Boolean {
87+
var sum = 0
88+
pesel.forEachIndexed { index, value -> sum += (value.toInt() * WEIGHTS[index]) }
89+
return if (sum % 10 == 0) {
90+
valid = true
91+
true
92+
} else {
93+
error = ValidatorError.INVALID
94+
false
95+
}
96+
}
97+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (C) 2017 Mateusz Utkała
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.utkala.polishidentifiersutils
18+
19+
enum class ValidatorError {
20+
INVALID_INPUT, WRONG_LENGTH, INVALID
21+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (C) 2017 Mateusz Utkała
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.utkala.polishidentifiersutils
18+
19+
import org.junit.Assert.assertEquals
20+
import org.junit.Test
21+
22+
23+
class PeselUtilTest {
24+
@Test
25+
fun validPeselTest() {
26+
var pesel = PeselUtil("67881319915")
27+
assertEquals(pesel.isValid(), true)
28+
assertEquals(pesel.error, null)
29+
30+
pesel = PeselUtil("78060533750")
31+
assertEquals(pesel.isValid(), true)
32+
assertEquals(pesel.error, null)
33+
34+
pesel = PeselUtil("61021854465")
35+
assertEquals(pesel.isValid(), true)
36+
assertEquals(pesel.error, null)
37+
38+
pesel = PeselUtil("09272910390")
39+
assertEquals(pesel.isValid(), true)
40+
assertEquals(pesel.error, null)
41+
42+
pesel = PeselUtil("35111831513")
43+
assertEquals(pesel.isValid(), true)
44+
assertEquals(pesel.error, null)
45+
}
46+
47+
@Test
48+
fun invalidPeselTest() {
49+
val pesel = PeselUtil("67881319910")
50+
assertEquals(pesel.isValid(), false)
51+
assertEquals(pesel.error, ValidatorError.INVALID)
52+
}
53+
54+
@Test
55+
fun emptyPeselTest() {
56+
val pesel = PeselUtil("")
57+
assertEquals(pesel.isValid(), false)
58+
assertEquals(pesel.error, ValidatorError.WRONG_LENGTH)
59+
}
60+
61+
@Test
62+
fun shortPeselTest() {
63+
val pesel = PeselUtil("123")
64+
assertEquals(pesel.isValid(), false)
65+
assertEquals(pesel.error, ValidatorError.WRONG_LENGTH)
66+
}
67+
68+
@Test
69+
fun longPeselTest() {
70+
val pesel = PeselUtil("12345678901234")
71+
assertEquals(pesel.isValid(), false)
72+
assertEquals(pesel.error, ValidatorError.WRONG_LENGTH)
73+
}
74+
75+
@Test
76+
fun notNumberPeselTest() {
77+
val pesel = PeselUtil("abc")
78+
assertEquals(pesel.isValid(), false)
79+
assertEquals(pesel.error, ValidatorError.WRONG_LENGTH)
80+
}
81+
82+
@Test
83+
fun mixedLetterNumberPeselTest() {
84+
val pesel = PeselUtil("123abc34247")
85+
assertEquals(pesel.isValid(), false)
86+
assertEquals(pesel.error, ValidatorError.INVALID_INPUT)
87+
}
88+
89+
@Test
90+
fun checkGenderForValidPeselTest() {
91+
var pesel = PeselUtil("67881319915")
92+
assertEquals(pesel.isValid(), true)
93+
assertEquals(pesel.getGender(), PeselUtil.Gender.MALE)
94+
95+
pesel = PeselUtil("61021854465")
96+
assertEquals(pesel.isValid(), true)
97+
assertEquals(pesel.getGender(), PeselUtil.Gender.FEMALE)
98+
}
99+
100+
@Test
101+
fun checkGenderForInvalidPeselTest() {
102+
val pesel = PeselUtil("67881319911")
103+
assertEquals(pesel.isValid(), false)
104+
assertEquals(pesel.getGender(), null)
105+
}
106+
107+
@Test
108+
fun checkBirthDateForValidPeselTest() {
109+
var pesel = PeselUtil("67881319915")
110+
assertEquals(pesel.isValid(), true)
111+
assertEquals(pesel.getBirthDate(), "1867-08-13")
112+
113+
pesel = PeselUtil("61021854465")
114+
assertEquals(pesel.isValid(), true)
115+
assertEquals(pesel.getBirthDate(), "1961-02-18")
116+
117+
pesel = PeselUtil("01301055623")
118+
assertEquals(pesel.isValid(), true)
119+
assertEquals(pesel.getBirthDate(), "2001-10-10")
120+
121+
pesel = PeselUtil("33450593635")
122+
assertEquals(pesel.isValid(), true)
123+
assertEquals(pesel.getBirthDate(), "2133-05-05")
124+
}
125+
126+
@Test
127+
fun checkBirthDateForInvalidPeselTest() {
128+
val pesel = PeselUtil("67881319911")
129+
assertEquals(pesel.isValid(), false)
130+
assertEquals(pesel.getBirthDate(), null)
131+
}
132+
133+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':app'
1+
include ':app', ':polishIdentifiersUtils'

0 commit comments

Comments
 (0)