Last active
March 3, 2023 15:58
-
-
Save zhyunk/2e561178cdbabef200d0a6227bcb2ae5 to your computer and use it in GitHub Desktop.
과제 4. 주민등록번호 생성 프로그램
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.Random; | |
| import java.util.Scanner; | |
| /* | |
| * 과제 4. 주민등록번호 생성 프로그램 | |
| * | |
| * 작성자 : 김지현 | |
| * 작성일 : 2023-03-02 | |
| * */ | |
| public class MiniAssignment04 { | |
| public static void main(String[] args) { | |
| int year = 0; | |
| int month = 0; | |
| int day = 0; | |
| String gender = ""; | |
| String imuiNumber = ""; | |
| System.out.println("[주민등록번호 계산]"); | |
| MyScanner my = new MyScanner(); | |
| year = my.getScannerReturnIntLength("출생년도를 입력해 주세요.(yyyy):", 4); | |
| month = my.getScannerReturnIntRange("출생월을 입력해 주세요.(mm):", 1, 12); | |
| day = my.getScannerReturnIntRange("출생일을 입력해 주세요.(dd):", 1, 31); | |
| gender = my.getScannerReturnString("성별을 입력해 주세요.(m/f):", "[m,M,f,F]"); | |
| my.close(); | |
| if (gender.replaceAll("[m,M]", "").isEmpty()) { | |
| imuiNumber += getGenderNumber("m", year); | |
| } else { | |
| imuiNumber += getGenderNumber("y", year); | |
| } | |
| Random rand = new Random(); | |
| for (int i = 0; i < 6; i++) { | |
| imuiNumber += rand.nextInt(10); | |
| } | |
| System.out.println(String.format("%02d%02d%02d-%s", year % 100, month, day, imuiNumber)); | |
| } | |
| public static int getGenderNumber(String gender, int year) { | |
| // 외국인 구분 x | |
| if (year < 1900) { | |
| if (gender.equals("m")) { | |
| return 9; | |
| } else { | |
| return 0; | |
| } | |
| } else if (year < 2000) { | |
| if (gender.equals("m")) { | |
| return 1; | |
| } else { | |
| return 2; | |
| } | |
| } else { | |
| if (gender.equals("m")) { | |
| return 3; | |
| } else { | |
| return 4; | |
| } | |
| } | |
| } | |
| static class MyScanner { | |
| String str; | |
| Scanner scan; | |
| MyScanner() { | |
| scan = new Scanner(System.in); | |
| } | |
| public int getScannerReturnIntRange(String text, int min, int max) { | |
| // min = 최소(포함) 범위 , max = 최대(포함) 범위 | |
| str = ""; | |
| while (true) { | |
| System.out.print(text); | |
| str = scan.nextLine(); | |
| if (!str.replaceAll(" ", "").isEmpty() && str.replaceAll("[0-9]", "").isEmpty()) { | |
| if (Integer.parseInt(str) < max + 1 && Integer.parseInt(str) > min - 1) | |
| break; | |
| } | |
| } | |
| return Integer.parseInt(str); | |
| } | |
| public int getScannerReturnIntLength(String text, int numLength) { | |
| // numLength = 숫자 길이 | |
| str = ""; | |
| while (true) { | |
| System.out.print(text); | |
| str = scan.nextLine(); | |
| if (!str.replaceAll(" ", "").isEmpty() && str.replaceAll("[0-9]", "").isEmpty() && str.length() == numLength) { | |
| break; | |
| } | |
| } | |
| return Integer.parseInt(str); | |
| } | |
| public String getScannerReturnString(String text, String regex) { | |
| // regex = 해당되는 문자를 제거할 정규식, 문자, 문자열 | |
| str = ""; | |
| while (true) { | |
| System.out.print(text); | |
| str = scan.nextLine(); | |
| if (!str.replaceAll(" ", "").isEmpty() && str.replaceAll(regex, "").isEmpty()) { | |
| break; | |
| } | |
| } | |
| return str; | |
| } | |
| public void close() { | |
| scan.close(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment