diff options
| author | mo <mo.khan@gmail.com> | 2019-07-13 16:00:56 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2019-07-13 16:00:56 -0600 |
| commit | 6d8f6b80e9bf6670f73e9ce6e9757cf0a38580be (patch) | |
| tree | 9cbe4dfbdd1de481d8f4b57e58b8e68598411d38 /src/Q6 | |
| parent | 259209789902352ac48249e41719c89f4515d550 (diff) | |
validate day overflow
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); + } } |
