Skip to content

Instantly share code, notes, and snippets.

@soeunkk
Last active September 17, 2022 11:14
Show Gist options
  • Save soeunkk/e88542b898dca60b577660c30e6849f3 to your computer and use it in GitHub Desktop.
Save soeunkk/e88542b898dca60b577660c30e6849f3 to your computer and use it in GitHub Desktop.
/*
* 김소은
* 과제4. 주민등록번호 생성 프로그램
*
* 주민등록과 관련된 변수, 함수를 묶어 클래스를 만들었습니다.
* 주민등록번호 생성 가능한 날짜는 1900년 1월 1일부터 프로그램을 실행하는 당일까지 가능하도록 구현하였습니다.
* 입력값이 잘못됐으면 재입력할 수 있도록 구현하였습니다.
*/
import java.time.LocalDate;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
class ResidentRegistration {
private int birthYear;
private int birthMonth;
private int birthDay;
private char gender;
public static void checkValidRegistrationYear(int year) {
int thisYear = LocalDate.now().getYear();
if (year < 1900 || year > thisYear) {
throw new InputMismatchException(String.format("1900 ~ %d 사이의 값만 등록 가능합니다.", thisYear));
}
}
public static void checkValidRegistrationMonth(int month) {
if (month < 1 || month > 12) {
throw new InputMismatchException("1 ~ 12 사이의 값만 가능합니다.");
}
}
public static void checkValidRegistrationMonth(int month, int monthLimit) {
if (month < 1 || month > monthLimit) {
throw new InputMismatchException(String.format("해당 년도에 등록 가능한 달은 1 ~ %d 입니다.", monthLimit));
}
}
public static void checkValidRegistrationDay(int day, int dayLimit) {
if (day < 1 || day > dayLimit) {
throw new InputMismatchException(String.format("해당 년도와 월에 등록 가능한 날은 1 ~ %d 입니다.", dayLimit));
}
}
public static void checkValidGender(char gender) {
if (gender != 'm' && gender != 'f') {
throw new InputMismatchException("m 또는 f만 입력 가능합니다.");
}
}
public ResidentRegistration(int birthYear, int birthMonth, int birthDay, char gender) {
this.birthYear = birthYear;
this.birthMonth = birthMonth;
this.birthDay = birthDay;
this.gender = gender;
}
public String makeNumber() {
StringBuilder residentRegistrationNumber = new StringBuilder();
int twoDigitBirthYear = birthYear % 100;
residentRegistrationNumber.append(String.format("%02d%02d%02d", twoDigitBirthYear, birthMonth, birthDay));
residentRegistrationNumber.append('-');
residentRegistrationNumber.append(getGenderNumber(birthYear, gender));
residentRegistrationNumber.append(getRandomNumber(6));
return residentRegistrationNumber.toString();
}
private int getGenderNumber(int birthYear, char gender) {
if (gender == 'm') {
if (birthYear < 2000) {
return 1;
} else {
return 3;
}
} else if (gender == 'f') {
if (birthYear < 2000) {
return 2;
} else {
return 4;
}
} else {
throw new IllegalArgumentException("성별의 입력값이 올바르지 않습니다.");
}
}
private String getRandomNumber(int digit) {
Random random = new Random();
StringBuilder randomNumber = new StringBuilder();
for (int i = 0; i < digit; i++) {
randomNumber.append(random.nextInt(10));
}
return randomNumber.toString();
}
}
public class Test4 {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("[주민등록번호 계산]");
ResidentRegistration residentRegistration = inputResidentRegistration();
String residentRegistrationNumber = residentRegistration.makeNumber();
System.out.println(residentRegistrationNumber);
closeResource();
}
private static ResidentRegistration inputResidentRegistration() {
int birthYear = inputBirthYear();
int birthMonth;
if (birthYear == LocalDate.now().getYear()) { // 생일이 올해라면 현재 달까지만 등록 가능
int monthLimit = LocalDate.now().getMonthValue();
birthMonth = inputBirthMonth(monthLimit);
} else {
birthMonth = inputBirthMonth();
}
int dayLimit;
if (birthYear == LocalDate.now().getYear() && birthMonth == LocalDate.now().getMonthValue()) { // 생일이 올해 이번 달이라면 현재 날짜까지만 등록 가능
dayLimit = LocalDate.now().getDayOfMonth();
} else {
dayLimit = LocalDate.of(birthYear, birthMonth, 1).lengthOfMonth();
}
int birthDay = inputBirthDay(dayLimit);
char gender = inputGender();
return new ResidentRegistration(birthYear, birthMonth, birthDay, gender);
}
private static int inputBirthYear() {
int birthYear = 0;
boolean isValid = false;
while (! isValid) {
System.out.print("출생년도를 입력해 주세요.(yyyy):");
try {
checkIntType();
birthYear = scanner.nextInt();
ResidentRegistration.checkValidRegistrationYear(birthYear);
isValid = true;
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
}
return birthYear;
}
private static int inputBirthMonth() {
int birthMonth = 0;
boolean isValid = false;
while (! isValid) {
System.out.print("출생월을 입력해 주세요.(mm):");
try {
checkIntType();
birthMonth = scanner.nextInt();
ResidentRegistration.checkValidRegistrationMonth(birthMonth);
isValid = true;
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
}
return birthMonth;
}
private static int inputBirthMonth(int monthLimit) {
int birthMonth = 0;
boolean isValid = false;
while (! isValid) {
System.out.print("출생월을 입력해 주세요.(mm):");
try {
checkIntType();
birthMonth = scanner.nextInt();
ResidentRegistration.checkValidRegistrationMonth(birthMonth, monthLimit);
isValid = true;
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
}
return birthMonth;
}
private static int inputBirthDay(int dayLimit) {
int birthDay = 0;
boolean isValid = false;
while (! isValid) {
System.out.print("출생일을 입력해 주세요.(dd):");
try {
checkIntType();
birthDay = scanner.nextInt();
ResidentRegistration.checkValidRegistrationDay(birthDay, dayLimit);
isValid = true;
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
}
return birthDay;
}
private static char inputGender() {
char gender = '-';
boolean isValid = false;
while (! isValid) {
System.out.print("성별을 입력해 주세요.(m/f):");
try {
gender = scanner.next().charAt(0);
ResidentRegistration.checkValidGender(gender);
isValid = true;
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
}
return gender;
}
private static void checkIntType() {
if (! scanner.hasNextInt()) {
removeBuffer();
throw new InputMismatchException("숫자만 입력 가능합니다.");
}
}
private static void removeBuffer() { scanner.next(); }
private static void closeResource() { scanner.close(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment