Skip to content

Instantly share code, notes, and snippets.

@FeelingXD
Last active January 5, 2023 14:09
Show Gist options
  • Save FeelingXD/1b3f0dcaf2f6cf34b312f6d220a3030e to your computer and use it in GitHub Desktop.
Save FeelingXD/1b3f0dcaf2f6cf34b312f6d220a3030e to your computer and use it in GitHub Desktop.
주민번호 생성기
/*
작성자: 고지민
*/
//주민번호 생성기
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year =input_int("출생년도를 입력해 주세요.(yyyy):",input,1975,2100);
int month=input_int("출생월을 입력해 주세요.(mm):",input,1,12);
int day=input_day("출생일을 입력해 주세요.(dd):",input,1,31, year,month);
char gender=input_char("성별을 입력해주세요.(m/f):",input);
output_id(year,month,day,gender);
}
public static int input_int(String message,Scanner input,int min, int max){
try{
System.out.print(message);
int value = input.nextInt();
if (value<min||value>max){
throw new Exception();
}
return value;
}catch (Exception e){
System.out.println("다시입력해주세요");
return input_int(message,input,min,max);
}
}
public static int input_day(String message,Scanner input,int min,int max,int year,int month){ // day 인경우 month에 따라 최대값 제한
int max_day;
int[] even_month={4,6,9,11};
if(month==2){
max_day= isLeap(year) ? 29:28;
} else{
max_day=Arrays.asList(even_month).contains(month) ? 30:31;
}
try{
System.out.print(message);
int value = input.nextInt();
if (value<min||value>max_day){
throw new Exception();
}
return value;
}catch (Exception e){
System.out.println("다시입력해주세요");
return input_int(message,input,min,max_day);
}
}
public static char input_char(String message,Scanner input){
try{
System.out.print(message);
char value = input.next().charAt(0);
if(value!='m'||value!='f'){
return value;
}
throw new Exception();
}catch (Exception e){
System.out.println("다시입력해주세요");
return input_char(message,input);
}
}
public static boolean isLeap(int year){ // 윤년 체크
return (year%4==0&&year%100!=0)||year%400==0;
}
public static void output_id(int year,int month,int day, char gender){ //주민번호 생성및 출력
StringBuilder id= new StringBuilder();
id.append(year % 100);
id.append(String.format("%02d", month));
id.append(String.format("%02d", day));
id.append("-");
id.append(rightNumber(year,gender));
System.out.println(id);
}
public static StringBuilder rightNumber(int year, char gender){ //주민번호 오른쪽 자리생성
int firstNum;
Random rnd =new Random();
if (year<2000){
firstNum = gender=='m' ? 1:2;
}else{
firstNum = gender=='m'? 3:4;
}
StringBuilder number = new StringBuilder();
number.append(firstNum);
while(number.length()<7){
number.append(rnd.nextInt(10));
}
return number;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment