[비밀번호 - Easy]


/**
 * <PRE>
 * 1. 개요
 *  
 * 비밀번호 6자리를 설정하는 경우에, 동일한 숫자가 3번 이상 반복되거나, 연속된 숫자가 3번 이상 반복되는지 체크하는 로직을 구현하시오.
 * 
 * 2. 예상 결과
 * 예상 결과는 하기 코드 실행시 예상되는 결과 값이다.
 * 예 1: 123985 : 연속된 숫자가 3번이상 반복되었습니다.
 * 예 2: 498733 : 연속된 숫자가 3번이상 반복되었습니다.
 * 예 3: 243339 : 동일한 숫자가 3번이상 반복되었습니다.
 * 예 4: 131313 : 사용가능한 비밀번호 입니다.
 * </PRE>
 *
 * @author 양석진
 */
public class Test04 {

	private static final String result = "결과 : %s => %s";

	public static void main(String[] args) {
		// 시작시간 기록
		long startTime = System.currentTimeMillis();

		// STEP 1. TEST 데이터 준비
		String password = "131313";

		// STEP 2. 유효한 비밀번호 인지 검증한다.
		String retmsg = checkPasswdPolicy(password);
		System.out.println(String.format(result, password, retmsg));

		long endTime = System.currentTimeMillis() - startTime;
		System.out.println("소요시간 : " + endTime + "ms.");

	}

	/**
	 * 
	 * <PRE>
	 * 유효한 비밀번호 인지 검증한다.
	 * </PRE>
	 * 
	 * @param password : 비밀번호
	 * @return result : 검증 결과
	 */
	public static String checkPasswdPolicy(String password) {
		String result = null;
		char[] numbers = password.toCharArray();
		int size = numbers.length;
		int beforeNum = 0;
		int serialCntAsc = 1;
		int serialCntDesc = 1;
		int sameCnt = 1;

		for (int inx = 0; inx < size; inx++) {

			int num = Character.digit(numbers[inx], 10);

			// STEP 1. 연속된 숫자가 3번이상 반복되었습니다.(오름차순 / 내림차순)
			if (num - beforeNum == 1) {
				serialCntAsc++;
			} else {
				serialCntAsc = 1;
			}
			if (beforeNum - num == 1) {
				serialCntDesc++;
			} else {
				serialCntDesc = 1;
			}

			if (serialCntAsc == 3 || serialCntDesc == 3) {
				result = "연속된 숫자가 3번이상 반복되었습니다.";
				break;
			}

			// STEP 2. 동일한 숫자가 3번이상 반복되었습니다.
			if (beforeNum == num) {
				sameCnt++;
			} else {
				sameCnt = 1;
			}
			if (sameCnt == 3) {
				result = "동일한 숫자가 3번이상 반복되었습니다.";
				break;
			}
			beforeNum = num;
		}
		if (result == null) {
			result = "사용가능한 비밀번호 입니다.";
		}
		return result;
	}
}