Klasa walidująca poprawność PESELu.

This commit is contained in:
Piotr Szefler 2021-10-27 18:39:03 +02:00
parent 136cb90a1f
commit b15b17be03

View File

@ -1,143 +1,142 @@
package wmiproject; package wmiproject;
public class PeselValid { public class PeselValid {
private byte PESEL[] = new byte[11]; private byte PESEL[] = new byte[11];
private boolean valid = false; private boolean valid = false;
public PeselValid(String PESELNumber) { public PeselValid(String PESELNumber) {
if (PESELNumber.length() != 11){ if (PESELNumber.length() != 11){
valid = false; valid = false;
} }
else { else {
for (int i = 0; i < 11; i++){ for (int i = 0; i < 11; i++){
PESEL[i] = Byte.parseByte(PESELNumber.substring(i, i+1)); PESEL[i] = Byte.parseByte(PESELNumber.substring(i, i+1));
} }
if (checkSum() && checkMonth() && checkDay()) { if (checkSum() && checkMonth() && checkDay()) {
valid = true; valid = true;
} }
else { else {
valid = false; valid = false;
} }
} }
} }
public int getBirthYear() { public int getBirthYear() {
int year; int year;
int month; int month;
year = 10 * PESEL[0]; year = 10 * PESEL[0];
year += PESEL[1]; year += PESEL[1];
month = 10 * PESEL[2]; month = 10 * PESEL[2];
month += PESEL[3]; month += PESEL[3];
if (month > 80 && month < 93) { if (month > 80 && month < 93) {
year += 1800; year += 1800;
} }
else if (month > 0 && month < 13) { else if (month > 0 && month < 13) {
year += 1900; year += 1900;
} }
else if (month > 20 && month < 33) { else if (month > 20 && month < 33) {
year += 2000; year += 2000;
} }
else if (month > 40 && month < 53) { else if (month > 40 && month < 53) {
year += 2100; year += 2100;
} }
else if (month > 60 && month < 73) { else if (month > 60 && month < 73) {
year += 2200; year += 2200;
} }
return year; return year;
} }
public int getBirthMonth() { public int getBirthMonth() {
int month; int month;
month = 10 * PESEL[2]; month = 10 * PESEL[2];
month += PESEL[3]; month += PESEL[3];
if (month > 80 && month < 93) { if (month > 80 && month < 93) {
month -= 80; month -= 80;
} }
else if (month > 20 && month < 33) { else if (month > 20 && month < 33) {
month -= 20; month -= 20;
} }
else if (month > 40 && month < 53) { else if (month > 40 && month < 53) {
month -= 40; month -= 40;
} }
else if (month > 60 && month < 73) { else if (month > 60 && month < 73) {
month -= 60; month -= 60;
} }
return month; return month;
} }
public int getBirthDay() {
public int getBirthDay() { int day;
int day; day = 10 * PESEL[4];
day = 10 * PESEL[4]; day += PESEL[5];
day += PESEL[5]; return day;
return day; }
}
private boolean checkSum() {
private boolean checkSum() { int sum = 1 * PESEL[0] +
int sum = 1 * PESEL[0] + 3 * PESEL[1] +
3 * PESEL[1] + 7 * PESEL[2] +
7 * PESEL[2] + 9 * PESEL[3] +
9 * PESEL[3] + 1 * PESEL[4] +
1 * PESEL[4] + 3 * PESEL[5] +
3 * PESEL[5] + 7 * PESEL[6] +
7 * PESEL[6] + 9 * PESEL[7] +
9 * PESEL[7] + 1 * PESEL[8] +
1 * PESEL[8] + 3 * PESEL[9];
3 * PESEL[9]; sum %= 10;
sum %= 10; sum = 10 - sum;
sum = 10 - sum; sum %= 10;
sum %= 10;
if (sum == PESEL[10]) {
if (sum == PESEL[10]) { return true;
return true; }
} else {
else { return false;
return false; }
} }
}
private boolean checkMonth() {
private boolean checkMonth() { int month = getBirthMonth();
int month = getBirthMonth(); if (month > 0 && month < 13) {
if (month > 0 && month < 13) { return true;
return true; }
} else {
else { return false;
return false; }
} }
}
private boolean checkDay() {
private boolean checkDay() { int year = getBirthYear();
int year = getBirthYear(); int month = getBirthMonth();
int month = getBirthMonth(); int day = getBirthDay();
int day = getBirthDay(); if ((day >0 && day < 32) &&
if ((day >0 && day < 32) && (month == 1 || month == 3 || month == 5 ||
(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||
month == 7 || month == 8 || month == 10 || month == 12)) {
month == 12)) { return true;
return true; }
} else if ((day >0 && day < 31) &&
else if ((day >0 && day < 31) && (month == 4 || month == 6 || month == 9 ||
(month == 4 || month == 6 || month == 9 || month == 11)) {
month == 11)) { return true;
return true; }
} else if ((day >0 && day < 30 && leapYear(year)) ||
else if ((day >0 && day < 30 && leapYear(year)) || (day >0 && day < 29 && !leapYear(year))) {
(day >0 && day < 29 && !leapYear(year))) { return true;
return true; }
} else {
else { return false;
return false; }
} }
}
private boolean leapYear(int year) {
private boolean leapYear(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true;
return true; else
else return false;
return false; }
} public boolean isValid() {
public boolean isValid() { return valid;
return valid; }
} }
}