diff options
Diffstat (limited to 'src/Q6')
| -rw-r--r-- | src/Q6/WeekDay.java | 5 | ||||
| -rw-r--r-- | src/Q6/WeekDayTest.java | 10 |
2 files changed, 15 insertions, 0 deletions
diff --git a/src/Q6/WeekDay.java b/src/Q6/WeekDay.java index 08233b0..31d90b4 100644 --- a/src/Q6/WeekDay.java +++ b/src/Q6/WeekDay.java @@ -8,6 +8,7 @@ public class WeekDay { private static final int[] MONTHS = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; public String getWeekDay(int day, int month, int year) { + this.ensureValidDate(year, month, day); return DAYS[daysSinceEpoch(day, month, year) % 7]; } @@ -32,6 +33,10 @@ public class WeekDay { return MONTHS[month - 1]; } + private void ensureValidDate(int year, int month, int day) { + if (day > MONTHS[month - 1]) throw new IllegalArgumentException(); + } + public static void main(String[] args) { Scanner in = new Scanner(System.in); diff --git a/src/Q6/WeekDayTest.java b/src/Q6/WeekDayTest.java index ddd4f56..281e39e 100644 --- a/src/Q6/WeekDayTest.java +++ b/src/Q6/WeekDayTest.java @@ -63,4 +63,14 @@ public class WeekDayTest extends TestCase { public void test_getWeekDay_2100_01_31() { assertEquals("Sunday", subject.getWeekDay(31, 1, 2100)); } + + public void test_getWeekDay_invalid_day() { + boolean raised = false; + try { + subject.getWeekDay(32, 1, 2100); + } catch (IllegalArgumentException error) { + raised = true; + } + assertTrue(raised); + } } |
