Last active
September 17, 2022 11:16
-
-
Save soeunkk/213fa9bb83c7f52d056d1ea29f94384b to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* 김소은 | |
* 과제5. 달력 출력 프로그램 | |
* | |
* 달력과 관련된 상수, 함수를 묶어 클래스를 만들었습니다. | |
* 입력값이 잘못됐으면 재입력할 수 있도록 구현하였습니다. | |
*/ | |
import java.time.LocalDate; | |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
class Calender { | |
private static final int DAYS_OF_WEEK = 7; | |
private static final String[] dayNamesOfWeek = new String[] {"일", "월", "화", "수", "목", "금", "토"}; | |
public static void print(int year, int month) { | |
StringBuilder calenderResult = new StringBuilder(); | |
calenderResult.append(String.format("[%4d년 %02d월]", year, month)).append("\n"); | |
calenderResult.append(toStringDayNamesOfWeek()); | |
calenderResult.append(toStringDaysOfMonth(LocalDate.of(year, month, 1))); | |
System.out.println(calenderResult); | |
} | |
private static String toStringDayNamesOfWeek() { | |
StringBuilder sb = new StringBuilder(); | |
for (String dayNameOfWeek: dayNamesOfWeek) { | |
sb.append(String.format("%s\t", dayNameOfWeek)); | |
} | |
sb.append("\n"); | |
return sb.toString(); | |
} | |
private static String toStringDaysOfMonth(LocalDate localDate) { | |
StringBuilder sb = new StringBuilder(); | |
int dayOfWeek = localDate.getDayOfWeek().getValue() % DAYS_OF_WEEK; // 1일에 해당하는 요일 | |
int FirstSunday = ((DAYS_OF_WEEK - dayOfWeek) + 1) % DAYS_OF_WEEK; // 첫 번쨰로 돌아오는 일요일 날짜 | |
int endDayOfMonth = localDate.lengthOfMonth(); // 달의 마지막 날짜 | |
int day = 1; | |
while (day <= endDayOfMonth) { | |
if (dayOfWeek > 0) { | |
sb.append("\t"); | |
dayOfWeek--; | |
} else { | |
sb.append(String.format("%02d\t", day++)); | |
if (day % DAYS_OF_WEEK == FirstSunday) { | |
sb.append("\n"); | |
} | |
} | |
} | |
return sb.toString(); | |
} | |
} | |
public class Test5 { | |
private static final Scanner scanner = new Scanner(System.in); | |
public static void main(String[] args) { | |
System.out.println("[달력 출력 프로그램]"); | |
int year = inputYear(); | |
int month = inputMonth(); | |
System.out.print("\n\n"); | |
Calender.print(year, month); | |
closeResource(); | |
} | |
private static int inputYear() { | |
int year = 0; | |
boolean isValid = false; | |
while (! isValid) { | |
System.out.print("달력의 년도를 입력해 주세요.(yyyy):"); | |
try { | |
checkIntType(); | |
year = scanner.nextInt(); | |
checkValidYear(year); | |
isValid = true; | |
} catch (InputMismatchException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
return year; | |
} | |
private static int inputMonth() { | |
int month = 0; | |
boolean isValid = false; | |
while (! isValid) { | |
System.out.print("달력의 월을 입력해 주세요.(mm):"); | |
try { | |
checkIntType(); | |
month = scanner.nextInt(); | |
checkValidMonth(month); | |
isValid = true; | |
} catch (InputMismatchException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
return month; | |
} | |
private static void checkIntType() { | |
if (! scanner.hasNextInt()) { | |
removeBuffer(); | |
throw new InputMismatchException("숫자만 입력 가능합니다."); | |
} | |
} | |
private static void checkValidYear(int year) { | |
if (year < 0 || year > 10000) { | |
throw new InputMismatchException("0 ~ 9999 사이의 값만 가능합니다."); | |
} | |
} | |
private static void checkValidMonth(int month) { | |
if (month < 1 || month > 12) { | |
throw new InputMismatchException("1 ~ 12 사이의 값만 가능합니다."); | |
} | |
} | |
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