diff --git a/src/main/java/cokr/xit/foundation/data/DataFormat.java b/src/main/java/cokr/xit/foundation/data/DataFormat.java index 08eb01a..05f48e1 100644 --- a/src/main/java/cokr/xit/foundation/data/DataFormat.java +++ b/src/main/java/cokr/xit/foundation/data/DataFormat.java @@ -7,22 +7,6 @@ import java.util.Date; import cokr.xit.foundation.AbstractComponent; public class DataFormat extends AbstractComponent { - /**'-'로 구분된 법인등록번호를 반환한다. - * @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) : ""; - } - /**yyyyMMdd, 또는 yyMMdd 포맷의 문자열을 yyyy-MM-dd 포맷으로 변환하여 반환한다. * @param obj yyyyMMdd, 또는 yyMMdd 포맷의 문자열 * @return yyyy-MM-dd 포맷 문자열 diff --git a/src/main/java/cokr/xit/foundation/data/RegistrationNumber.java b/src/main/java/cokr/xit/foundation/data/RegistrationNumber.java new file mode 100644 index 0000000..cce7fa7 --- /dev/null +++ b/src/main/java/cokr/xit/foundation/data/RegistrationNumber.java @@ -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)); + } +*/ +} \ No newline at end of file diff --git a/src/main/java/cokr/xit/foundation/util/DateFormats.java b/src/main/java/cokr/xit/foundation/util/DateFormats.java index f084795..48e8e35 100644 --- a/src/main/java/cokr/xit/foundation/util/DateFormats.java +++ b/src/main/java/cokr/xit/foundation/util/DateFormats.java @@ -27,4 +27,15 @@ public class DateFormats { throw Assert.runtimeException(e); } } + + public boolean isValid(String pattern, String str) { + if (Assert.isEmpty(str)) return false; + + try { + dateFormat(pattern).parse(str); + return true; + } catch (Exception e) { + return false; + } + } } \ No newline at end of file