RegistrationNumber 추가
parent
fd59548218
commit
93a21ecea0
@ -0,0 +1,109 @@
|
||||
package cokr.xit.foundation.data;
|
||||
|
||||
import cokr.xit.foundation.AbstractComponent;
|
||||
import cokr.xit.foundation.util.DateFormats;
|
||||
|
||||
public class RegistrationNumber extends AbstractComponent {
|
||||
public static enum Type {
|
||||
RESIDENT,
|
||||
FOREIGNER,
|
||||
CORPORATE,
|
||||
BUSINESS,
|
||||
UNKNOWN
|
||||
}
|
||||
|
||||
private static final DateFormats dateFormats = new DateFormats();
|
||||
|
||||
public static final Type getType(String str) {
|
||||
if (isEmpty(str))
|
||||
return Type.UNKNOWN;
|
||||
|
||||
String digits = str.replaceAll("[^0-9]", "");
|
||||
int length = digits.length();
|
||||
if (length == 10)
|
||||
return Type.BUSINESS;
|
||||
if (length != 13)
|
||||
return Type.UNKNOWN;
|
||||
|
||||
String yyMMdd = digits.substring(0, 6),
|
||||
gender = Character.toString(digits.charAt(6)),
|
||||
century = switch (gender) {
|
||||
case "1", "2", "5", "6" -> "19";
|
||||
case "3", "4", "7", "8" -> "20";
|
||||
default -> "";
|
||||
};
|
||||
boolean date = dateFormats.isValid("yyyyMMdd", !isEmpty(century) ? century + yyMMdd : ""),
|
||||
person = ofPerson(digits);
|
||||
|
||||
if ("1,2,3,4".contains(gender) && date && person)
|
||||
return Type.RESIDENT;
|
||||
if ("5,6,7,8".contains(gender))
|
||||
return Type.FOREIGNER;
|
||||
if (ofCorporate(digits))
|
||||
return Type.CORPORATE;
|
||||
|
||||
return Type.UNKNOWN;
|
||||
}
|
||||
|
||||
private static final boolean ofPerson(String digits) {
|
||||
int[] weights = {2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5};
|
||||
int sum = 0;
|
||||
for (int i = 0; i < weights.length; i++) {
|
||||
sum += Character.getNumericValue(digits.charAt(i)) * weights[i];
|
||||
}
|
||||
int last = Character.getNumericValue(digits.charAt(12)),
|
||||
checkDigit = (11 - (sum % 11)) % 10;
|
||||
return checkDigit == last;
|
||||
}
|
||||
|
||||
private static final boolean ofCorporate(String digits) {
|
||||
int[] weights = new int[] {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2};
|
||||
int sum = 0;
|
||||
for (int i = 0; i < weights.length; i++) {
|
||||
int weightedValue = Character.getNumericValue(digits.charAt(i)) * weights[i];
|
||||
sum += weightedValue;
|
||||
/*
|
||||
// 곱한 결과가 두 자리면 각 자릿수를 더함
|
||||
if (weightedValue >= 10) {
|
||||
sum += (weightedValue / 10) + (weightedValue % 10);
|
||||
} else {
|
||||
sum += weightedValue;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
int last = Character.getNumericValue(digits.charAt(12)),
|
||||
checkDigit = (10 - (sum % 10)) % 10;
|
||||
return checkDigit == last;
|
||||
}
|
||||
|
||||
/**'-'로 구분된 법인등록번호를 반환한다.
|
||||
* @param str '-'이 없는 법인등록번호
|
||||
* @return '-'로 구분된 법인등록번호
|
||||
*/
|
||||
public static final String corporateNo(String str) {
|
||||
return !isEmpty(str) ? str.substring(0, 6) + "-" + str.substring(6) : "";
|
||||
}
|
||||
|
||||
/**'-'로 구분된 사업자등록번호를 반환한다.
|
||||
* @param str '-'이 없는 사업자등록번호
|
||||
* @return '-'로 구분된 사업자등록번호
|
||||
*/
|
||||
public static final String businessNo(String str) {
|
||||
return !isEmpty(str) ? str.substring(0, 3) + "-" + str.substring(3, 5) + "-" + str.substring(5) : "";
|
||||
}
|
||||
/*
|
||||
public static void main(String[] args) {
|
||||
String str = "주민 750416-1067011: ";
|
||||
System.out.println(str + getType(str));
|
||||
str = "법인 110111-2429672: ";
|
||||
System.out.println(str + getType(str));
|
||||
str = "외국인 901225-5234567: ";
|
||||
System.out.println(str + getType(str));
|
||||
str = "외국인 000101-7012345: ";
|
||||
System.out.println(str + getType(str));
|
||||
str = "사업자 214-87-03698: ";
|
||||
System.out.println(str + getType(str));
|
||||
}
|
||||
*/
|
||||
}
|
||||
Loading…
Reference in New Issue