Skip to content

Instantly share code, notes, and snippets.

@youngvctr
Last active June 2, 2023 19:31
Show Gist options
  • Save youngvctr/89108cb80126dfd5362a3d3654b1146c to your computer and use it in GitHub Desktop.
Save youngvctr/89108cb80126dfd5362a3d3654b1146c to your computer and use it in GitHub Desktop.
/*
김정훈
2023-06-02 | 주민등록번호 생성 프로그램
*/
import java.util.Random;
import java.util.Scanner;
public class JavaStudy04 {
public static void main(String[] args) {
System.out.println("[주민등록번호 계산]");
String nationalIdentifyNumber = "";
Random random = new Random();
StringBuilder sb = new StringBuilder();
System.out.print("출생년도를 입력해 주세요.(yyyy):");
Scanner sc = new Scanner(System.in);
String birthYear = sc.next();
if(!birthYear.matches("[0-9]+")) return;
if (Integer.parseInt(birthYear) < 2020 || birthYear.length() > 4) {
return;
}
sb.append(birthYear.substring(2,4));
System.out.print("출생월을 입력해 주세요.(mm):");
sc = new Scanner(System.in);
String birthMonth = sc.next();
if(!birthMonth.matches("[0-9]+")) return;
if(Integer.parseInt(birthMonth) > 12 || Integer.parseInt(birthMonth) < 1) {
return;
}
String strMonth = (birthMonth.length()==1) ? String.format("%02d", birthMonth) : birthMonth;
sb.append(strMonth);
System.out.print("출생일을 입력해 주세요.(dd):");
sc = new Scanner(System.in);
String birthDay = sc.next();
if(!birthDay.matches("[0-9]+")) {
return;
}
if(Integer.parseInt(birthDay) > 31 || Integer.parseInt(birthDay) < 1) {
return;
}
String strDay = (birthDay.length()==1) ? String.format("%02d", birthDay) : birthDay;
sb.append(strDay);
sb.append("-");
System.out.print("성별을 입력해 주세요.(m/f):");
sc = new Scanner(System.in);
String strSex = sc.next().toLowerCase();
if (strSex.length() != 1 || (!strSex.contains("m") && !strSex.contains("f"))) {
return;
}
int sex = (strSex.equals("f")) ? 4 : 3;
sb.append(sex);
sb.append(String.format("%06d", random.nextInt(1, 999999)));
nationalIdentifyNumber = new String(sb);
System.out.println(nationalIdentifyNumber); //nationalIdentifyNumber;
}
}
@youngvctr
Copy link
Author

4_2023-06-03 040913

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment