Created
January 19, 2025 07:16
-
-
Save Exom9434/a453653d68b4ac039716389bb2d33403 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//노재경 | |
import java.util.Scanner; | |
import java.util.Random; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("[주민등록번호 생성]"); | |
String birthDate = getValidatedDate(scanner); | |
String gender = getValidatedGender(scanner); | |
String socialNum = generateSocialNumber(birthDate, gender); | |
scanner.close(); | |
System.out.println("생성된 주민등록번호: " + socialNum); | |
} | |
private static String getValidatedDate(Scanner scanner) { | |
while (true) { | |
System.out.print("출생년월일을 입력해주세요 (yyyy.mm.dd): "); | |
String input = scanner.nextLine(); | |
if (input.matches("\\d{4}\\.\\d{2}\\.\\d{2}")) { | |
return input; | |
} | |
System.out.println("유효하지 않은 형식입니다. yyyy.mm.dd 형식으로 입력해주세요."); | |
} | |
} | |
private static String getValidatedGender(Scanner scanner) { | |
while (true) { | |
System.out.print("성별을 입력해주세요 (m/f): "); | |
String input = scanner.nextLine().trim().toLowerCase(); | |
if (input.equals("m") || input.equals("f")) { | |
return input; | |
} | |
System.out.println("유효하지 않은 입력입니다. m 또는 f를 입력해주세요."); | |
} | |
} | |
private static String generateSocialNumber(String birthDate, String gender) { | |
// 년월일 분리 | |
String[] dateParts = birthDate.split("\\."); | |
int year = Integer.parseInt(dateParts[0]); | |
int month = Integer.parseInt(dateParts[1]); | |
int day = Integer.parseInt(dateParts[2]); | |
// 앞자리 생성 | |
String firstPart = String.format("%02d%02d%02d", year % 100, month, day); | |
// 성별 코드 생성 | |
String genderDigit = getGenderDigit(year, gender); | |
// 뒤쪽 6자리 랜덤 생성 | |
String randomNumString = String.format("%06d", new Random().nextInt(999999) + 1); | |
// 최종 주민등록번호 결합 | |
return firstPart + "-" + genderDigit + randomNumString; | |
} | |
private static String getGenderDigit(int year, String gender) { | |
boolean isMale = gender.equals("m"); | |
if (year < 2000) { | |
return isMale ? "1" : "2"; | |
} else { | |
return isMale ? "3" : "4"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment