diff --git a/knoda/src/C069_Festival.java b/knoda/src/C069_Festival.java new file mode 100644 index 0000000000000000000000000000000000000000..b64383cdc058fda88a43357cca4ab58d5715cee2 --- /dev/null +++ b/knoda/src/C069_Festival.java @@ -0,0 +1,82 @@ +package hellojunit; + +import java.util.Scanner; + +public class C069_Festival { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + final int year = scan.nextInt(); + final int month = scan.nextInt(); + final int day = scan.nextInt(); + final int festivalMonth = scan.nextInt(); + final int festivalDay = scan.nextInt(); + + Festival festival = new Festival(year, month, day, festivalMonth, festivalDay); + System.out.println(festival.getDayUntilFestival()); + + scan.close(); + } +} + + +class Festival { + private int year; + private int month; + private int day; + private final int festivalMonth; + private final int festivalDay; + private static final int ODD_NUMBER_MONTH_DAY = 13; // 奇数月の日数 + private static final int EVEN_NUMBER_MONTH_DAY = 15; // 偶数月の日数 + + public Festival(final int year, final int month, final int day, final int festivalMonth, final int festivalDay) { + this.year = year; + this.month = month; + this.day = day; + this.festivalMonth = festivalMonth; + this.festivalDay = festivalDay; + } + + // ある月の日数を取得するメソッド + private int getDaysOfMonth(final int month) { + if (month % 2 == 0) { + return EVEN_NUMBER_MONTH_DAY; + } + return ODD_NUMBER_MONTH_DAY; + } + + // 祭りが開催される年であるかを判定するメソッド + private boolean isFestivalYear(final int year) { + return year % 4 == 1; + } + + // 年と月をカウントアップするメソッド + private void countUpMonth() { + month++; + if (month > 13) { + month = 1; + year++; + } + } + + // 祭り開催までの残り日数を取得するメソッド + public int getDayUntilFestival() { + int tempYear = year + 1; + int dayUntilFestival = 0; + while (!isFestivalYear(tempYear)) { + tempYear++; + } + + final int festivalYear = tempYear; + + dayUntilFestival += getDaysOfMonth(month) - day; + countUpMonth(); + + while ((year != festivalYear) || (month != festivalMonth)) { + dayUntilFestival += getDaysOfMonth(month); + countUpMonth(); + } + dayUntilFestival += festivalDay; + return dayUntilFestival; + } +} diff --git a/knoda/test/C069_FestivalTest.java b/knoda/test/C069_FestivalTest.java new file mode 100644 index 0000000000000000000000000000000000000000..cf883eb8f76fc71e41dfabdcc441df2eea29a2a3 --- /dev/null +++ b/knoda/test/C069_FestivalTest.java @@ -0,0 +1,26 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class C069_FestivalTest { + + @Test + public void 日数を取得するテスト1() { + Festival sut = new Festival(2000, 12, 10, 1, 10); + int expected = 28; + int actual = sut.getDayUntilFestival(); + assertThat(actual, is(expected)); + } + + @Test + public void 日数を取得するテスト2() { + Festival sut = new Festival(1994, 4, 8, 7, 13); + int expected = 591; + int actual = sut.getDayUntilFestival(); + assertThat(actual, is(expected)); + } + +}