Last active
April 11, 2023 08:15
-
-
Save zhyunk/81656f1412cbb104d79b3881291ff3f6 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-06 | |
* */ | |
public class MiniAssignment04InFeedback { | |
public static void main(String[] args) { | |
int year = 0; | |
int month = 0; | |
int day = 0; | |
String gender = ""; | |
int genderNumber = 0; | |
MyScanner my; | |
System.out.println("[주민등록번호 계산]"); | |
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()) { | |
genderNumber = getGenderNumber("m", year); | |
} else { | |
genderNumber = getGenderNumber("y", year); | |
} | |
Random rand = new Random(); | |
System.out.println(String.format("%02d%02d%02d-%d%06d", year % 100, month, day, genderNumber, rand.nextInt(1000000))); | |
} | |
public static int getGenderNumber(String gender, int year) { | |
// 외국인 구분 x | |
if (year < 1900) { | |
if ("m".equals(gender)) { | |
return 9; | |
} else { | |
return 0; | |
} | |
} else if (year < 2000) { | |
if ("m".equals(gender)) { | |
return 1; | |
} else { | |
return 2; | |
} | |
} else { | |
if ("m".equals(gender)) { | |
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.next(); | |
if (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.next(); | |
if (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.next(); | |
if (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