diff --git a/src/main/java/spinbox/DateTime.java b/src/main/java/spinbox/DateTime.java index 74e2adf66d..15b84af7ce 100644 --- a/src/main/java/spinbox/DateTime.java +++ b/src/main/java/spinbox/DateTime.java @@ -4,6 +4,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; public class DateTime implements Comparable { @@ -31,6 +32,36 @@ public Date getDateTime() { return dateTime; } + /** + * Return the day of the month. + * @return day of the month + */ + public int getDayOfMonth() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dateTime); + return calendar.get(Calendar.DAY_OF_MONTH); + } + + /** + * Return the day of the week. + * @return day of the week + */ + public int getDayOfWeek() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dateTime); + return calendar.get(Calendar.DAY_OF_WEEK); + } + + /** + * Return hour. + * @return hour + */ + public int getHour() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dateTime); + return calendar.get(Calendar.HOUR_OF_DAY); + } + /** * Converts the Date object back to the string version in the format of MM/dd/yyyy HH:mm * This can be reused to create an identical dateTime object. @@ -38,8 +69,7 @@ public Date getDateTime() { */ public String toString() { DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm"); - String dateString = dateFormat.format(dateTime); - return dateString; + return dateFormat.format(dateTime); } public boolean before(DateTime dateTime) { @@ -59,5 +89,55 @@ public int compareTo(DateTime dateTimeTwo) { return this.getDateTime().compareTo(dateTimeTwo.getDateTime()); } + /** + * Return another DateTime with date + * set as start of the week relative to this DateTime. + * @return start of the week + */ + public DateTime getStartOfTheWeek() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dateTime); + while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) { + calendar.add(Calendar.DATE, -1); + } + return new DateTime(calendar.getTime()); + } + + /** + * Return another DateTime with date + * set as end of the week relative to this DateTime. + * @return end of the week + */ + public DateTime getEndOfTheWeek() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dateTime); + while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) { + calendar.add(Calendar.DATE, 1); + } + return new DateTime(calendar.getTime()); + } + /** + * Return another DateTime with date + * set as start of the month relative to this DateTime. + * @return start of the month + */ + public DateTime getStartOfTheMonth() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dateTime); + calendar.set(Calendar.DAY_OF_MONTH, 1); + return new DateTime(calendar.getTime()); + } + + /** + * Return another DateTime with date + * set as end of the month relative to this DateTime. + * @return end of the month + */ + public DateTime getEndOfTheMonth() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dateTime); + calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); + return new DateTime(calendar.getTime()); + } } diff --git a/src/main/java/spinbox/containers/lists/TaskList.java b/src/main/java/spinbox/containers/lists/TaskList.java index 2b723a103f..cfa2c92c8f 100644 --- a/src/main/java/spinbox/containers/lists/TaskList.java +++ b/src/main/java/spinbox/containers/lists/TaskList.java @@ -91,7 +91,29 @@ public List viewList() { List output = new ArrayList<>(); output.add("Here are the tasks in your module:"); for (int i = 0; i < list.size(); i++) { - output.add((Integer.toString(i + 1) + ". " + list.get(i).toString())); + output.add(((i + 1) + ". " + list.get(i).toString())); + } + return output; + } + + /** + * Return list of task that overlaps with start and end interval. + * @param startInterval start of the interval. + * @param endInterval end of the interval. + * @return list of task + */ + public List viewListInterval(DateTime startInterval, DateTime endInterval) { + Task currentTask; + List output = new ArrayList<>(); + output.add("Here are the task in your module:"); + for (int i = 0; i < list.size(); i++) { + currentTask = list.get(i); + if (currentTask.isSchedulable()) { + Schedulable task = (Schedulable) currentTask; + if (task.isOverlapping(startInterval, endInterval)) { + output.add(((i + 1) + ". " + task.toString())); + } + } } return output; } diff --git a/src/main/java/spinbox/entities/Calendar.java b/src/main/java/spinbox/entities/Calendar.java new file mode 100644 index 0000000000..fc969662a2 --- /dev/null +++ b/src/main/java/spinbox/entities/Calendar.java @@ -0,0 +1,43 @@ +package spinbox.entities; + +import spinbox.DateTime; +import spinbox.containers.lists.TaskList; +import spinbox.exceptions.CalendarSelectorException; + +import java.util.List; + +public class Calendar { + private static final String MIDNIGHT = " 00:00"; + private static final String BEFORE_MIDNIGHT = " 23:59"; + DateTime startDate; + DateTime endDate; + int modifier; + + public Calendar(int modifier, String date) throws CalendarSelectorException { + this.modifier = modifier; + setDates(date); + } + + private void setDates(String date) throws CalendarSelectorException { + switch (modifier) { + case 1: + startDate = new DateTime(date + MIDNIGHT); + endDate = new DateTime(date + BEFORE_MIDNIGHT); + break; + case 2: + startDate = new DateTime(date + MIDNIGHT).getStartOfTheWeek(); + endDate = new DateTime(date + BEFORE_MIDNIGHT).getEndOfTheWeek(); + break; + case 3: + startDate = new DateTime(date + MIDNIGHT).getStartOfTheMonth(); + endDate = new DateTime(date + BEFORE_MIDNIGHT).getEndOfTheMonth(); + break; + default: + throw new CalendarSelectorException(); + } + } + + public List tasksInCalendar(TaskList taskList) { + return taskList.viewListInterval(startDate, endDate); + } +} \ No newline at end of file diff --git a/src/main/java/spinbox/exceptions/CalendarSelectorException.java b/src/main/java/spinbox/exceptions/CalendarSelectorException.java new file mode 100644 index 0000000000..7a97cca9f1 --- /dev/null +++ b/src/main/java/spinbox/exceptions/CalendarSelectorException.java @@ -0,0 +1,10 @@ +package spinbox.exceptions; + +public class CalendarSelectorException extends SpinBoxException { + private static final String ERROR_MESSAGE = "Error: Please supply 1, 2, 3 to corresponding" + + " to day, month or year for calendar selection"; + + public CalendarSelectorException() { + super(ERROR_MESSAGE); + } +} diff --git a/src/test/java/DateTimeTest.java b/src/test/java/DateTimeTest.java index a4920abdeb..ac63cb43bf 100644 --- a/src/test/java/DateTimeTest.java +++ b/src/test/java/DateTimeTest.java @@ -2,12 +2,14 @@ import spinbox.exceptions.SpinBoxException; import org.junit.jupiter.api.Test; +import java.time.DayOfWeek; + import static org.junit.jupiter.api.Assertions.assertEquals; public class DateTimeTest { @Test - public void correctDateInputShouldCreateReusableString() throws SpinBoxException { + public void dateTimeCreation_variousDateTimeString_successfulCreationAndExpectedStringOutput() { DateTime test = new DateTime("05/02/2020 12:00"); assertEquals("05/02/2020 12:00", test.toString()); @@ -23,7 +25,7 @@ public void correctDateInputShouldCreateReusableString() throws SpinBoxException } @Test - public void parseDifferentDateFormattedString() throws SpinBoxException { + public void dateTimeCreation_differentlyFormattedDateTimeString_successfulCreationAndExpectedStringOutput() { DateTime test = new DateTime("the day before 12/16/2019 4pm"); assertEquals("12/15/2019 16:00", test.toString()); @@ -38,4 +40,51 @@ public void parseDifferentDateFormattedString() throws SpinBoxException { assertEquals("12/25/2019 06:00", santaVacationStartDate.toString()); assertEquals("12/24/2020 23:59", santaVacationEndDate.toString()); } + + @Test + public void getDayOfMonth_DateTimeString_expectedDay() { + DateTime test = new DateTime("10/16/2019 12:33"); + assertEquals(16, test.getDayOfMonth()); + } + + @Test + public void getDayOfWeek_DateTimeString_expectedDay() { + DateTime test = new DateTime("10/16/2019 12:33"); + assertEquals(4, test.getDayOfWeek()); + } + + @Test + public void getHour_DateTimeString_expectedHour() { + String date = "10/16/2019 12:33"; + DateTime test = new DateTime(date); + assertEquals(12, test.getHour()); + } + + @Test + public void getStartOfTheWeek_DateTimeString_expectedDateTimeString() { + String date = "10/16/2019"; + DateTime test = new DateTime(date + " 00:00"); + assertEquals("10/13/2019 00:00", test.getStartOfTheWeek().toString()); + } + + @Test + public void getEndOfTheWeek_DateTimeString_expectedDateTimeString() { + String date = "10/16/2019"; + DateTime test = new DateTime(date + " 23:59"); + assertEquals("10/19/2019 23:59", test.getEndOfTheWeek().toString()); + } + + @Test + public void getStartOfTheMonth_DateTimeString_expectedDateTimeString() { + String date = "10/16/2019"; + DateTime test = new DateTime(date + " 00:00"); + assertEquals("10/01/2019 00:00", test.getStartOfTheMonth().toString()); + } + + @Test + public void getEndOfTheMonth_DateTimeString_expectedDateTimeString() { + String date = "10/16/2019"; + DateTime test = new DateTime(date + " 23:59"); + assertEquals("10/31/2019 23:59", test.getEndOfTheMonth().toString()); + } }