From d7fa2c7434fb4972ef1a6ea0ea43787a6dcb3fd2 Mon Sep 17 00:00:00 2001 From: Antonia Devina Date: Tue, 25 Oct 2016 21:22:59 +0800 Subject: [PATCH 1/3] Tidy up the DateTimeInfo class TODO: method header (undone commit) --- .../seedu/flexitrack/model/ModelManager.java | 15 +- .../flexitrack/model/task/DateTimeInfo.java | 219 +++++++++++++----- .../flexitrack/model/task/ReadOnlyTask.java | 8 +- .../seedu/flexitrack/model/task/Task.java | 5 + .../seedu/flexitrack/testutil/TestTask.java | 5 + 5 files changed, 176 insertions(+), 76 deletions(-) diff --git a/src/main/java/seedu/flexitrack/model/ModelManager.java b/src/main/java/seedu/flexitrack/model/ModelManager.java index 03276e1c55c2..163a3c24950c 100644 --- a/src/main/java/seedu/flexitrack/model/ModelManager.java +++ b/src/main/java/seedu/flexitrack/model/ModelManager.java @@ -204,7 +204,7 @@ public String toString() { } } - //TODO: + private class DateQualifier implements Qualifier { private String keyWords; private String dateInfo; @@ -217,23 +217,24 @@ private class DateQualifier implements Qualifier { .replace(ListCommand.LIST_NEXT_WEEK_COMMAND, "").replace(ListCommand.LIST_NEXT_MONTH_COMMAND, "").trim(); } + //TODO: need to refactor @Override public boolean run(ReadOnlyTask task) { boolean willBeShown=true; if (keyWords.contains(ListCommand.LIST_FUTURE_COMMAND)) { if (task.getIsTask()){ - willBeShown = DateTimeInfo.isInTheFuture(task.getDueDate()); + willBeShown = DateTimeInfo.isInTheFuture(DateTimeInfo.getCurrentTimeInString(), task.getDueDate()); } else if (task.getIsEvent()){ - willBeShown = DateTimeInfo.isInTheFuture(task.getEndTime()); + willBeShown = DateTimeInfo.isInTheFuture(DateTimeInfo.getCurrentTimeInString(), task.getEndTime()); }else { willBeShown = !task.getIsDone(); } } else if (keyWords.contains(ListCommand.LIST_PAST_COMMAND)){ if (task.getIsTask()){ - willBeShown = DateTimeInfo.isInThePast(task.getDueDate()); + willBeShown = DateTimeInfo.isInThePast(DateTimeInfo.getCurrentTimeInString(), task.getDueDate()); } else { - willBeShown = DateTimeInfo.isInThePast(task.getEndTime()); + willBeShown = DateTimeInfo.isInThePast(DateTimeInfo.getCurrentTimeInString(), task.getEndTime()); } } else if (keyWords.contains(ListCommand.LIST_LAST_COMMAND) || keyWords.contains(ListCommand.LIST_NEXT_COMMAND)){ willBeShown = DateTimeInfo.withInTheDuration(keyWords,task); @@ -254,10 +255,6 @@ public boolean run(ReadOnlyTask task) { } -// @Override -// public String toString() { -// return "name=" + String.join(", ", dateKeyWords); -// } } diff --git a/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java b/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java index 31e79e38b427..f266c9bac891 100644 --- a/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java +++ b/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java @@ -41,6 +41,10 @@ public void setDateGroupTime(String givenTime) throws IllegalValueException { formatTiming(parsedTiming.isInferred()); } + /** + * Change the format of the timing saved in setTime + * @param inferred + */ private void formatTiming(boolean inferred) { if (inferred) { setTime = getDateMonthYear() + " 07:59"; @@ -116,6 +120,12 @@ public static String durationOfTheEvent(String startingTime, String endingTime) return durationBetweenTwoTiming(startingTime,endingTime); } + /** + * prepare variables needed to calculate the duration between two timing + * @param startingTime + * @param endingTime + * @return the duration of the event in a string + */ private static String durationBetweenTwoTiming(String startingTime, String endingTime) { int years = yearsOfTheEvent(startingTime, endingTime); int months = monthsOfTheEvent(startingTime, endingTime); @@ -126,6 +136,15 @@ private static String durationBetweenTwoTiming(String startingTime, String endin return combineDuratingOfEvent(years, months, days, hours, minutes); } + /** + * Calculate the duration of the event + * @param years + * @param months + * @param days + * @param hours + * @param minutes + * @return the duration of the event in a string + */ private static String combineDuratingOfEvent(int years, int months, int days, int hours, int minutes) { String duration1 = new String(""); @@ -255,36 +274,49 @@ public int hashCode() { return setTime.hashCode(); } + /** + * @return true if the date not specified; + */ public boolean isDateNull() { return this.setTime.equals("Feb 29 2000 00:00"); } + /** + * If the time is inferred, replace "07:59" with "16:59" + */ public void isEndTimeInferred() { if (setTime.substring(12, 17).equals("07:59")) { setTime = setTime.substring(0, 12) + "16:59"; } } - public static boolean isInTheFuture(DateTimeInfo Date) { + /** + * @param timeNow + * @param Date + * @return true if the timing timeNow is after the timing Date + */ + public static boolean isInTheFuture(DateTimeInfo timeNow, DateTimeInfo Date) { String result = MESSAGE_FROM_IS_AFTER_TO; - try { - result = durationBetweenTwoTiming(new DateTimeInfo ("now").toString(),Date.toString()); - } catch (IllegalValueException e) { - e.printStackTrace(); - } + result = durationBetweenTwoTiming(timeNow.toString(),Date.toString()); return !result.equals(MESSAGE_FROM_IS_AFTER_TO); } - public static boolean isInThePast(DateTimeInfo Date) { - String result = MESSAGE_FROM_IS_AFTER_TO; - try { - result = durationBetweenTwoTiming(Date.toString(),new DateTimeInfo ("now").toString()); - } catch (IllegalValueException e) { - e.printStackTrace(); - } - return !result.equals(MESSAGE_FROM_IS_AFTER_TO); + /** + * + * @param timeNow + * @param Date + * @return true if the timing timeNow is before the timing Date + */ + public static boolean isInThePast(DateTimeInfo timeNow, DateTimeInfo Date) { + return isInTheFuture(Date,timeNow); } - + + /** + * Prepare the keyword and proces if the task is within the specified date. + * @param keyWords + * @param task + * @return + */ public static boolean isOnTheDate(String keyWords, ReadOnlyTask task) { String dateInfo = keyWords.replace(ListCommand.LIST_MARK_COMMAND, "").replace(ListCommand.LIST_UNMARK_COMMAND, "").trim(); try { @@ -292,83 +324,146 @@ public static boolean isOnTheDate(String keyWords, ReadOnlyTask task) { } catch (IllegalValueException e) { e.printStackTrace(); } - if ( task.getDueDate().toString().contains(dateInfo) || task.getEndTime().toString().contains(dateInfo) - || task.getStartTime().toString().contains(dateInfo)){ - return true; - } - return false; + return isTaskOnTheSpecifiedDate(task, dateInfo); } - public static boolean withInTheDuration(String keyWords, ReadOnlyTask task) { - - String dateNow=null; - boolean timeDifference=false; + /** + * @param task + * @param dateInfo + * @return true if the task has anything to do with the day of interest + */ + private static boolean isTaskOnTheSpecifiedDate(ReadOnlyTask task, String dateInfo) { + return task.getDueDate().toString().contains(dateInfo) || task.getEndTime().toString().contains(dateInfo) + || task.getStartTime().toString().contains(dateInfo) + || isTaskAnEventPassingThisDate(task, dateInfo); + } + /** + * @param task + * @param dateInfo + * @return true if a task is an event and the day interest is within the starting date and the ending date + */ + private static boolean isTaskAnEventPassingThisDate(ReadOnlyTask task, String dateInfo) { + if (!task.getIsEvent()){ + return false; + } + DateTimeInfo dateSpecified =null; try { - dateNow = new DateTimeInfo ("now").toString(); + dateSpecified = new DateTimeInfo (dateInfo); } catch (IllegalValueException e) { + // TODO Auto-generated catch block e.printStackTrace(); - } + } + return isInTheFuture(task.getEndTime(), dateSpecified) && isInTheFuture(dateSpecified, task.getStartTime()); + } + + public static boolean withInTheDuration(String keyWords, ReadOnlyTask task) { + String dateNow = getCurrentTimeInString().toString(); + boolean isWithInTime = isTaskWithinTheDurationSpecified(keyWords, task, dateNow); + return isWithInTime; + } + + /** + * @param keyWords + * @param task + * @param dateNow + * @return if the task is within the specified duration + */ + private static boolean isTaskWithinTheDurationSpecified(String keyWords, ReadOnlyTask task, String dateNow) { + boolean isWithInTime = false; if ( keyWords.contains(ListCommand.LIST_LAST_WEEK_COMMAND) ){ - if (task.getIsTask()){ - timeDifference = isDurationLessThanSpecified(dateNow, task.getDueDate().toString(), DAYS_IN_A_WEEK); - } else if (task.getIsEvent()){ - timeDifference = isDurationLessThanSpecified(dateNow, task.getEndTime().toString(), DAYS_IN_A_WEEK); - } else { - return false; - } + return isTaskAndWithinTheTime(task, dateNow, -DAYS_IN_A_WEEK); } else if ( keyWords.contains(ListCommand.LIST_LAST_MONTH_COMMAND) ){ - if (task.getIsTask()){ - timeDifference = isDurationLessThanSpecified(dateNow, task.getDueDate().toString(), AVERAGE_DAYS_IN_A_MONTH); - } else if (task.getIsEvent()){ - timeDifference = isDurationLessThanSpecified(dateNow, task.getEndTime().toString(), AVERAGE_DAYS_IN_A_MONTH); - } else { - return false; - } + return isTaskAndWithinTheTime(task, dateNow, -AVERAGE_DAYS_IN_A_MONTH); } else if ( keyWords.contains(ListCommand.LIST_NEXT_MONTH_COMMAND) ){ - if (task.getIsTask()){ - timeDifference = isDurationLessThanSpecified(task.getDueDate().toString(), dateNow, AVERAGE_DAYS_IN_A_MONTH); - } else if (task.getIsEvent()){ - timeDifference = isDurationLessThanSpecified(task.getEndTime().toString(), dateNow, AVERAGE_DAYS_IN_A_MONTH); - } else { - return false; - } + return isTaskAndWithinTheTime(task, dateNow, AVERAGE_DAYS_IN_A_MONTH); } else if ( keyWords.contains(ListCommand.LIST_NEXT_WEEK_COMMAND) ){ - if (task.getIsTask()){ - timeDifference = isDurationLessThanSpecified(task.getDueDate().toString(), dateNow, DAYS_IN_A_WEEK); - } else if (task.getIsEvent()){ - timeDifference = isDurationLessThanSpecified(task.getEndTime().toString(), dateNow, DAYS_IN_A_WEEK); - } else { - return false; - } + return isTaskAndWithinTheTime(task, dateNow, DAYS_IN_A_WEEK); + } + return isWithInTime; + } + + /** + * @param task + * @param dateNow + * @param expectedDays + * @return true if the task is not a floating task and it is within the specified timing + */ + private static boolean isTaskAndWithinTheTime(ReadOnlyTask task, String dateNow, int expectedDays) { + if (task.isNotFloatingTask()){ + return isTaskWithInTheDuration(task, dateNow, expectedDays); + } else { + return false; + } + } + + /** + * @param task + * @param dateNow + * @param expectedDays + * @return true if the task is within the stated duration + */ + private static boolean isTaskWithInTheDuration(ReadOnlyTask task, String dateNow, int expectedDays) { + boolean isTimeWithinExpectedTime=false; + if (task.getIsTask()){ + isTimeWithinExpectedTime = isTimeDifferenceLessThanSpecified(dateNow, task.getDueDate().toString(), expectedDays); + } else if (task.getIsEvent()){ + isTimeWithinExpectedTime = isTimeDifferenceLessThanSpecified(dateNow, task.getEndTime().toString(), expectedDays); } - return timeDifference; + return isTimeWithinExpectedTime; } - private static boolean isDurationLessThanSpecified(String startTime, String endTime, int maxDuration) { + /** + * Provide an easy access to the current timing in String + * @return String of the current time MMM DD YYYY HH:MM format. + */ + public static DateTimeInfo getCurrentTimeInString() { + DateTimeInfo dateNow = null; + try { + dateNow = new DateTimeInfo ("now"); + } catch (IllegalValueException e) { + e.printStackTrace(); + } + return dateNow; + } + + /** + * @param startTime + * @param endTime + * @param limitTimeDuration + * @return true if the time Difference is less than specified + */ + private static boolean isTimeDifferenceLessThanSpecified(String startTime, String endTime, int limitTimeDuration) { + if (limitTimeDuration<0){ + limitTimeDuration = limitTimeDuration*(-1); + return isTimeDifferenceLessThanSpecified (endTime, startTime, limitTimeDuration); + } + int years = yearsOfTheEvent(startTime, endTime); int months = monthsOfTheEvent(startTime, endTime); int days = daysOfTheEvent(startTime, endTime); int hours = hoursOfTheEvent(startTime, endTime); - + if (hours < 0) { hours = Math.floorMod(hours, 24); days=days-1; } if (days < 0) { - return false; + days = Math.floorMod(days, 30); + months=months-1; } - if (months < 0 || months>0) { + if (months < 0 || months>1) { return false; } if (years < 0 || years > 0) { return false; } - if(days < maxDuration || (months == 1 && days == 0)){ + if(days <= limitTimeDuration && months==0){ return true; - } - else { + } else if ( limitTimeDuration==AVERAGE_DAYS_IN_A_MONTH && months==1){ + return true; + } else { return false; } } diff --git a/src/main/java/seedu/flexitrack/model/task/ReadOnlyTask.java b/src/main/java/seedu/flexitrack/model/task/ReadOnlyTask.java index 2e7fd94e4e2e..0a9ae2788f38 100644 --- a/src/main/java/seedu/flexitrack/model/task/ReadOnlyTask.java +++ b/src/main/java/seedu/flexitrack/model/task/ReadOnlyTask.java @@ -12,17 +12,14 @@ public interface ReadOnlyTask { Name getName(); DateTimeInfo getDueDate(); - DateTimeInfo getStartTime(); - DateTimeInfo getEndTime(); boolean getIsTask(); - boolean getIsEvent(); - boolean getIsDone(); - + boolean isNotFloatingTask(); + /** * The returned TagList is a deep copy of the internal TagList, changes on * the returned list will not affect the person's internal tags. @@ -70,4 +67,5 @@ default String tagsString() { } } + } diff --git a/src/main/java/seedu/flexitrack/model/task/Task.java b/src/main/java/seedu/flexitrack/model/task/Task.java index 6d41ed99b418..eb90f4199237 100644 --- a/src/main/java/seedu/flexitrack/model/task/Task.java +++ b/src/main/java/seedu/flexitrack/model/task/Task.java @@ -157,4 +157,9 @@ public void setIsTask(Boolean bool) { public void setIsEvent(Boolean bool) { this.isEvent = bool; } + + public boolean isNotFloatingTask(){ + return (this.isEvent || this.isTask); + } + } diff --git a/src/test/java/seedu/flexitrack/testutil/TestTask.java b/src/test/java/seedu/flexitrack/testutil/TestTask.java index 4e41181ce52d..3962e24b7d2f 100644 --- a/src/test/java/seedu/flexitrack/testutil/TestTask.java +++ b/src/test/java/seedu/flexitrack/testutil/TestTask.java @@ -159,4 +159,9 @@ public static String getUnMarkCommand(int taskToUnMark) { return sb.toString(); } + @Override + public boolean isNotFloatingTask() { + return !( this.isEvent || this.isTask ); + } + } From 8c21e80d9f15f90750d15a0b91f888a243faeed0 Mon Sep 17 00:00:00 2001 From: Antonia Devina Date: Wed, 26 Oct 2016 17:29:42 +0800 Subject: [PATCH 2/3] Adding all the method description in DateTimeInfo class --- .../seedu/flexitrack/model/ModelManager.java | 2 +- .../flexitrack/model/task/DateTimeInfo.java | 94 +++++++++---------- .../flexitrack/model/task/ReadOnlyTask.java | 5 +- .../seedu/flexitrack/model/task/Task.java | 18 +++- .../seedu/flexitrack/testutil/TestTask.java | 20 +++- 5 files changed, 87 insertions(+), 52 deletions(-) diff --git a/src/main/java/seedu/flexitrack/model/ModelManager.java b/src/main/java/seedu/flexitrack/model/ModelManager.java index 163a3c24950c..01ae8d13a168 100644 --- a/src/main/java/seedu/flexitrack/model/ModelManager.java +++ b/src/main/java/seedu/flexitrack/model/ModelManager.java @@ -237,7 +237,7 @@ public boolean run(ReadOnlyTask task) { willBeShown = DateTimeInfo.isInThePast(DateTimeInfo.getCurrentTimeInString(), task.getEndTime()); } } else if (keyWords.contains(ListCommand.LIST_LAST_COMMAND) || keyWords.contains(ListCommand.LIST_NEXT_COMMAND)){ - willBeShown = DateTimeInfo.withInTheDuration(keyWords,task); + willBeShown = DateTimeInfo.withInTheDuration(keyWords, task, DateTimeInfo.getCurrentTimeInString().toString()); } else if (!dateInfo.equals("")){ willBeShown = DateTimeInfo.isOnTheDate(keyWords, task); } diff --git a/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java b/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java index f266c9bac891..3e82445f1074 100644 --- a/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java +++ b/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java @@ -9,6 +9,7 @@ import seedu.flexitrack.commons.exceptions.IllegalValueException; import seedu.flexitrack.logic.commands.ListCommand; +//@@author A0127686R /** * Represents a DateTimeInfo class in FlexiTrack */ @@ -23,7 +24,7 @@ public class DateTimeInfo { private String setTime; - public DateTimeInfo(String givenTime) throws IllegalValueException { + public DateTimeInfo(String givenTime) { setDateGroupTime(givenTime); } @@ -32,13 +33,19 @@ public DateTimeInfo(String givenTime) throws IllegalValueException { * * @param givenTime */ - public void setDateGroupTime(String givenTime) throws IllegalValueException { + public void setDateGroupTime(String givenTime) { assert givenTime != null; final Matcher matcher = TIME_TYPE_DATA_ARGS_FORMAT.matcher(givenTime.trim()); matcher.matches(); - DateTimeInfoParser parsedTiming = new DateTimeInfoParser(matcher.group("info")); - this.setTime = parsedTiming.getParsedTimingInfo(); - formatTiming(parsedTiming.isInferred()); + DateTimeInfoParser parsedTiming; + try { + parsedTiming = new DateTimeInfoParser(matcher.group("info")); + this.setTime = parsedTiming.getParsedTimingInfo(); + formatTiming(parsedTiming.isInferred()); + } catch (IllegalValueException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } /** @@ -53,6 +60,10 @@ private void formatTiming(boolean inferred) { } } + /** + * Extract the month, date and year of a particular date + * @return timing in MMM DD YYYY format + */ private String getDateMonthYear() { return setTime.substring(5, 12) + setTime.substring(25, 29); } @@ -231,7 +242,7 @@ private static int daysOfTheEvent(String startingTime, String endingTime) { } /** - * Calculate the day difference between the end and the start + * Calculate the year difference between the end and the start * * @param startingTime * @param endingTime @@ -291,6 +302,7 @@ public void isEndTimeInferred() { } /** + * Process the task if the task is in the future * @param timeNow * @param Date * @return true if the timing timeNow is after the timing Date @@ -302,7 +314,7 @@ public static boolean isInTheFuture(DateTimeInfo timeNow, DateTimeInfo Date) { } /** - * + * Process the task if the task is in the past * @param timeNow * @param Date * @return true if the timing timeNow is before the timing Date @@ -312,22 +324,21 @@ public static boolean isInThePast(DateTimeInfo timeNow, DateTimeInfo Date) { } /** - * Prepare the keyword and proces if the task is within the specified date. + * Prepare the keyword and process if the task is within the specified date. * @param keyWords * @param task * @return */ public static boolean isOnTheDate(String keyWords, ReadOnlyTask task) { String dateInfo = keyWords.replace(ListCommand.LIST_MARK_COMMAND, "").replace(ListCommand.LIST_UNMARK_COMMAND, "").trim(); - try { - dateInfo = new DateTimeInfo (dateInfo).toString().substring(0,11); - } catch (IllegalValueException e) { - e.printStackTrace(); - } + dateInfo = new DateTimeInfo (dateInfo).toString().substring(0,11); return isTaskOnTheSpecifiedDate(task, dateInfo); } /** + * Process if the task given has any relation with the dateInfo. + * For a task relation is defined as the due date is the dateInfo date. + * For an event, the event duration (inclusive the starting and the ending date) is within the specified DateInfo date. * @param task * @param dateInfo * @return true if the task has anything to do with the day of interest @@ -339,6 +350,7 @@ private static boolean isTaskOnTheSpecifiedDate(ReadOnlyTask task, String dateIn } /** + * Process the data if it the task is a event and it is passing through the date specified. * @param task * @param dateInfo * @return true if a task is an event and the day interest is within the starting date and the ending date @@ -346,52 +358,41 @@ private static boolean isTaskOnTheSpecifiedDate(ReadOnlyTask task, String dateIn private static boolean isTaskAnEventPassingThisDate(ReadOnlyTask task, String dateInfo) { if (!task.getIsEvent()){ return false; - } - DateTimeInfo dateSpecified =null; - try { - dateSpecified = new DateTimeInfo (dateInfo); - } catch (IllegalValueException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } + DateTimeInfo dateSpecified =null; + dateSpecified = new DateTimeInfo (dateInfo); return isInTheFuture(task.getEndTime(), dateSpecified) && isInTheFuture(dateSpecified, task.getStartTime()); } - public static boolean withInTheDuration(String keyWords, ReadOnlyTask task) { - - String dateNow = getCurrentTimeInString().toString(); - boolean isWithInTime = isTaskWithinTheDurationSpecified(keyWords, task, dateNow); - return isWithInTime; - } - - /** + /** + * Process if the a task specified is with in the duration stated. * @param keyWords * @param task - * @param dateNow - * @return if the task is within the specified duration + * @return true if the date is within the duration */ - private static boolean isTaskWithinTheDurationSpecified(String keyWords, ReadOnlyTask task, String dateNow) { + public static boolean withInTheDuration(String keyWords, ReadOnlyTask task, String dateNow) { boolean isWithInTime = false; if ( keyWords.contains(ListCommand.LIST_LAST_WEEK_COMMAND) ){ - return isTaskAndWithinTheTime(task, dateNow, -DAYS_IN_A_WEEK); + return isNotFloatingTaskAndWithinTheTime(task, dateNow, -DAYS_IN_A_WEEK); } else if ( keyWords.contains(ListCommand.LIST_LAST_MONTH_COMMAND) ){ - return isTaskAndWithinTheTime(task, dateNow, -AVERAGE_DAYS_IN_A_MONTH); + return isNotFloatingTaskAndWithinTheTime(task, dateNow, -AVERAGE_DAYS_IN_A_MONTH); } else if ( keyWords.contains(ListCommand.LIST_NEXT_MONTH_COMMAND) ){ - return isTaskAndWithinTheTime(task, dateNow, AVERAGE_DAYS_IN_A_MONTH); + return isNotFloatingTaskAndWithinTheTime(task, dateNow, AVERAGE_DAYS_IN_A_MONTH); } else if ( keyWords.contains(ListCommand.LIST_NEXT_WEEK_COMMAND) ){ - return isTaskAndWithinTheTime(task, dateNow, DAYS_IN_A_WEEK); - } + return isNotFloatingTaskAndWithinTheTime(task, dateNow, DAYS_IN_A_WEEK); + } return isWithInTime; } /** + * Process if the task given is either a deadline task or an event within the specified timing * @param task * @param dateNow * @param expectedDays * @return true if the task is not a floating task and it is within the specified timing */ - private static boolean isTaskAndWithinTheTime(ReadOnlyTask task, String dateNow, int expectedDays) { - if (task.isNotFloatingTask()){ + private static boolean isNotFloatingTaskAndWithinTheTime(ReadOnlyTask task, String dateNow, int expectedDays) { + if (task.getIsNotFloatingTask()){ return isTaskWithInTheDuration(task, dateNow, expectedDays); } else { return false; @@ -399,6 +400,7 @@ private static boolean isTaskAndWithinTheTime(ReadOnlyTask task, String dateNow, } /** + * Process if a deadline task or an event is within the duration specified. * @param task * @param dateNow * @param expectedDays @@ -406,29 +408,25 @@ private static boolean isTaskAndWithinTheTime(ReadOnlyTask task, String dateNow, */ private static boolean isTaskWithInTheDuration(ReadOnlyTask task, String dateNow, int expectedDays) { boolean isTimeWithinExpectedTime=false; - if (task.getIsTask()){ - isTimeWithinExpectedTime = isTimeDifferenceLessThanSpecified(dateNow, task.getDueDate().toString(), expectedDays); - } else if (task.getIsEvent()){ - isTimeWithinExpectedTime = isTimeDifferenceLessThanSpecified(dateNow, task.getEndTime().toString(), expectedDays); + if (task.getIsNotFloatingTask()){ + isTimeWithinExpectedTime = isTimeDifferenceLessThanSpecified(dateNow, + task.getStartingTimeOrDueDate().toString(), expectedDays); } return isTimeWithinExpectedTime; } - + /** * Provide an easy access to the current timing in String * @return String of the current time MMM DD YYYY HH:MM format. */ public static DateTimeInfo getCurrentTimeInString() { DateTimeInfo dateNow = null; - try { - dateNow = new DateTimeInfo ("now"); - } catch (IllegalValueException e) { - e.printStackTrace(); - } + dateNow = new DateTimeInfo ("now"); return dateNow; } /** + * Process if the difference of two given time is less than the time duration. * @param startTime * @param endTime * @param limitTimeDuration diff --git a/src/main/java/seedu/flexitrack/model/task/ReadOnlyTask.java b/src/main/java/seedu/flexitrack/model/task/ReadOnlyTask.java index 0a9ae2788f38..858cac91beb3 100644 --- a/src/main/java/seedu/flexitrack/model/task/ReadOnlyTask.java +++ b/src/main/java/seedu/flexitrack/model/task/ReadOnlyTask.java @@ -14,11 +14,13 @@ public interface ReadOnlyTask { DateTimeInfo getDueDate(); DateTimeInfo getStartTime(); DateTimeInfo getEndTime(); + DateTimeInfo getStartingTimeOrDueDate(); + DateTimeInfo getEndingTimeOrDueDate(); boolean getIsTask(); boolean getIsEvent(); boolean getIsDone(); - boolean isNotFloatingTask(); + boolean getIsNotFloatingTask(); /** * The returned TagList is a deep copy of the internal TagList, changes on @@ -68,4 +70,5 @@ default String tagsString() { } + } diff --git a/src/main/java/seedu/flexitrack/model/task/Task.java b/src/main/java/seedu/flexitrack/model/task/Task.java index eb90f4199237..0830a79d1793 100644 --- a/src/main/java/seedu/flexitrack/model/task/Task.java +++ b/src/main/java/seedu/flexitrack/model/task/Task.java @@ -158,8 +158,24 @@ public void setIsEvent(Boolean bool) { this.isEvent = bool; } - public boolean isNotFloatingTask(){ + public boolean getIsNotFloatingTask(){ return (this.isEvent || this.isTask); } + public DateTimeInfo getStartingTimeOrDueDate(){ + if (this.getIsTask()){ + return this.getDueDate(); + } else { + return this.getStartTime(); + } + } + + public DateTimeInfo getEndingTimeOrDueDate(){ + if (this.getIsTask()){ + return this.getDueDate(); + } else { + return this.getEndTime(); + } + } + } diff --git a/src/test/java/seedu/flexitrack/testutil/TestTask.java b/src/test/java/seedu/flexitrack/testutil/TestTask.java index 3962e24b7d2f..0dde85e3c084 100644 --- a/src/test/java/seedu/flexitrack/testutil/TestTask.java +++ b/src/test/java/seedu/flexitrack/testutil/TestTask.java @@ -160,8 +160,26 @@ public static String getUnMarkCommand(int taskToUnMark) { } @Override - public boolean isNotFloatingTask() { + public boolean getIsNotFloatingTask() { return !( this.isEvent || this.isTask ); } + @Override + public DateTimeInfo getStartingTimeOrDueDate(){ + if (this.getIsTask()){ + return this.getDueDate(); + } else { + return this.getStartTime(); + } + } + + @Override + public DateTimeInfo getEndingTimeOrDueDate(){ + if (this.getIsTask()){ + return this.getDueDate(); + } else { + return this.getEndTime(); + } + } + } From 64ae002c69973383abe92e7ae08fb4992e568c6f Mon Sep 17 00:00:00 2001 From: Antonia Devina Date: Wed, 26 Oct 2016 22:01:47 +0800 Subject: [PATCH 3/3] add some test cases to TimeDateInfo --- .../seedu/flexitrack/model/ModelManager.java | 46 +++--- .../flexitrack/model/task/DateTimeInfo.java | 46 ++++-- .../model/task/DateTimeInfoTest.java | 155 ++++++++++++++++++ 3 files changed, 209 insertions(+), 38 deletions(-) create mode 100644 src/test/java/seedu/flexitrack/model/task/DateTimeInfoTest.java diff --git a/src/main/java/seedu/flexitrack/model/ModelManager.java b/src/main/java/seedu/flexitrack/model/ModelManager.java index 01ae8d13a168..40f59ae687f9 100644 --- a/src/main/java/seedu/flexitrack/model/ModelManager.java +++ b/src/main/java/seedu/flexitrack/model/ModelManager.java @@ -220,31 +220,13 @@ private class DateQualifier implements Qualifier { //TODO: need to refactor @Override public boolean run(ReadOnlyTask task) { - boolean willBeShown=true; - - if (keyWords.contains(ListCommand.LIST_FUTURE_COMMAND)) { - if (task.getIsTask()){ - willBeShown = DateTimeInfo.isInTheFuture(DateTimeInfo.getCurrentTimeInString(), task.getDueDate()); - } else if (task.getIsEvent()){ - willBeShown = DateTimeInfo.isInTheFuture(DateTimeInfo.getCurrentTimeInString(), task.getEndTime()); - }else { - willBeShown = !task.getIsDone(); - } - } else if (keyWords.contains(ListCommand.LIST_PAST_COMMAND)){ - if (task.getIsTask()){ - willBeShown = DateTimeInfo.isInThePast(DateTimeInfo.getCurrentTimeInString(), task.getDueDate()); - } else { - willBeShown = DateTimeInfo.isInThePast(DateTimeInfo.getCurrentTimeInString(), task.getEndTime()); - } - } else if (keyWords.contains(ListCommand.LIST_LAST_COMMAND) || keyWords.contains(ListCommand.LIST_NEXT_COMMAND)){ - willBeShown = DateTimeInfo.withInTheDuration(keyWords, task, DateTimeInfo.getCurrentTimeInString().toString()); - } else if (!dateInfo.equals("")){ - willBeShown = DateTimeInfo.isOnTheDate(keyWords, task); - } + + boolean willBeShown = isTaskGoingToBeShown(task); if (willBeShown==false){ return false; } + if (keyWords.contains(ListCommand.LIST_UNMARK_COMMAND)){ return !task.getIsDone(); } else if (keyWords.contains(ListCommand.LIST_MARK_COMMAND)){ @@ -254,6 +236,28 @@ public boolean run(ReadOnlyTask task) { return willBeShown; } + + /** + * @param task + * @param willBeShown + * @return + */ + private boolean isTaskGoingToBeShown(ReadOnlyTask task) { + if (keyWords.contains(ListCommand.LIST_FUTURE_COMMAND)) { + if (task.getIsNotFloatingTask()){ + return DateTimeInfo.isInTheFuture(DateTimeInfo.getCurrentTimeInString(), task.getEndingTimeOrDueDate()); + }else { + return !task.getIsDone(); + } + } else if (keyWords.contains(ListCommand.LIST_PAST_COMMAND)){ + return DateTimeInfo.isInThePast(DateTimeInfo.getCurrentTimeInString(), task.getEndingTimeOrDueDate()); + } else if (keyWords.contains(ListCommand.LIST_LAST_COMMAND) || keyWords.contains(ListCommand.LIST_NEXT_COMMAND)){ + return DateTimeInfo.withInTheDuration(keyWords, task, DateTimeInfo.getCurrentTimeInString().toString()); + } else { + return DateTimeInfo.isOnTheDate(dateInfo, task); + } + // if (!dateInfo.equals("")) + } } diff --git a/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java b/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java index 3e82445f1074..206e5a9dfa84 100644 --- a/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java +++ b/src/main/java/seedu/flexitrack/model/task/DateTimeInfo.java @@ -15,16 +15,16 @@ */ public class DateTimeInfo { public static final String MESSAGE_DATETIMEINFO_CONSTRAINTS = "Invalid time inputed. Please check your spelling!"; - public static final boolean DUE_DATE_OR_START_TIME = true; - public static final boolean END_TIME = false; + public static final String MESSAGE_FROM_IS_AFTER_TO = "Please check the timing inputed! The given starting time is after the ending time."; + private static final Pattern TIME_TYPE_DATA_ARGS_FORMAT = Pattern.compile("(?.+)"); - private static final String MESSAGE_FROM_IS_AFTER_TO = "Please check the timing inputed! The given starting time is after the ending time."; private static final int AVERAGE_DAYS_IN_A_MONTH = 30; - private static final int DAYS_IN_A_WEEK = 7; + private static final int DAYS_IN_A_WEEK = 7; + private static final boolean FAIL_DUE_TO_EXCEPTION = false; private String setTime; - public DateTimeInfo(String givenTime) { + public DateTimeInfo(String givenTime) throws IllegalValueException { setDateGroupTime(givenTime); } @@ -32,8 +32,9 @@ public DateTimeInfo(String givenTime) { * Set the setTime (DateGroup object) as the date inputed by the user * * @param givenTime + * @throws IllegalValueException */ - public void setDateGroupTime(String givenTime) { + public void setDateGroupTime(String givenTime) throws IllegalValueException { assert givenTime != null; final Matcher matcher = TIME_TYPE_DATA_ARGS_FORMAT.matcher(givenTime.trim()); matcher.matches(); @@ -43,8 +44,7 @@ public void setDateGroupTime(String givenTime) { this.setTime = parsedTiming.getParsedTimingInfo(); formatTiming(parsedTiming.isInferred()); } catch (IllegalValueException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new IllegalValueException (MESSAGE_DATETIMEINFO_CONSTRAINTS); } } @@ -270,7 +270,7 @@ private static int monthsOfTheEvent(String startingTime, String endingTime) { @Override public String toString() { - return setTime.substring(0, 17); + return setTime; } @Override @@ -329,9 +329,12 @@ public static boolean isInThePast(DateTimeInfo timeNow, DateTimeInfo Date) { * @param task * @return */ - public static boolean isOnTheDate(String keyWords, ReadOnlyTask task) { - String dateInfo = keyWords.replace(ListCommand.LIST_MARK_COMMAND, "").replace(ListCommand.LIST_UNMARK_COMMAND, "").trim(); - dateInfo = new DateTimeInfo (dateInfo).toString().substring(0,11); + public static boolean isOnTheDate(String dateInfo, ReadOnlyTask task) { + try { + dateInfo = new DateTimeInfo (dateInfo).toString().substring(0,11); + } catch (IllegalValueException e) { + new IllegalValueException (MESSAGE_DATETIMEINFO_CONSTRAINTS); + } return isTaskOnTheSpecifiedDate(task, dateInfo); } @@ -359,9 +362,14 @@ private static boolean isTaskAnEventPassingThisDate(ReadOnlyTask task, String da if (!task.getIsEvent()){ return false; } - DateTimeInfo dateSpecified =null; - dateSpecified = new DateTimeInfo (dateInfo); - return isInTheFuture(task.getEndTime(), dateSpecified) && isInTheFuture(dateSpecified, task.getStartTime()); + DateTimeInfo dateSpecified; + try { + dateSpecified = new DateTimeInfo (dateInfo); + return isInTheFuture(task.getStartTime(), dateSpecified) && isInTheFuture(dateSpecified, task.getEndTime()); + } catch (IllegalValueException e) { + new IllegalValueException (MESSAGE_DATETIMEINFO_CONSTRAINTS); + } + return FAIL_DUE_TO_EXCEPTION; } /** @@ -421,12 +429,16 @@ private static boolean isTaskWithInTheDuration(ReadOnlyTask task, String dateNow */ public static DateTimeInfo getCurrentTimeInString() { DateTimeInfo dateNow = null; - dateNow = new DateTimeInfo ("now"); + try { + dateNow = new DateTimeInfo ("now"); + } catch (IllegalValueException e) { + new IllegalValueException (MESSAGE_DATETIMEINFO_CONSTRAINTS); + } return dateNow; } /** - * Process if the difference of two given time is less than the time duration. + * Process if the two given time is less than the time duration. * @param startTime * @param endTime * @param limitTimeDuration diff --git a/src/test/java/seedu/flexitrack/model/task/DateTimeInfoTest.java b/src/test/java/seedu/flexitrack/model/task/DateTimeInfoTest.java new file mode 100644 index 000000000000..1f708b2b1bd7 --- /dev/null +++ b/src/test/java/seedu/flexitrack/model/task/DateTimeInfoTest.java @@ -0,0 +1,155 @@ +package seedu.flexitrack.model.task; + +import org.junit.Before; +import org.junit.Test; +import seedu.flexitrack.model.task.DateTimeInfo; +import seedu.flexitrack.commons.exceptions.IllegalValueException; + +import static org.junit.Assert.*; +import static seedu.flexitrack.model.task.DateTimeInfo.MESSAGE_FROM_IS_AFTER_TO; + +public class DateTimeInfoTest { + + private DateTimeInfo testTime1; + private DateTimeInfo testTime2; + + @Before + public void setup() { + testTime1 = null; + testTime2 = null; + } + +// @Test +// public void emptyInput_returnsIncorrect() { +// final String[] emptyInputs = { "", " ", "\n \n" }; +// final String resultMessage = String.format(MESSAGE_DATETIMEINFO_CONSTRAINTS); +// DateTimeInfoAndAssertIncorrectWithMessage(resultMessage, emptyInputs); +// } + + @Test + public void ValidInputWithNoSpecificHoursAndMinutes_returnsDate() { + final String validInput = "3 July 2018"; + final String expectedSetTime = "Jul 03 2018 07:59"; + DateTimeInfoAndAssertCorrect(validInput, expectedSetTime); + } + + @Test + public void ValidInputWithSpecificHours_returnsDate() { + final String validInput = "22 january 2017 4pm"; + final String expectedSetTime = "Jan 22 2017 16:00"; + DateTimeInfoAndAssertCorrect(validInput, expectedSetTime); + } + + @Test + public void RelaxValidInputWithNoSpecificHoursAndMinutes_returnsDate() { + final String validInput = "mon"; + final String expectedSetTime = "Oct 31 2016 07:59"; + DateTimeInfoAndAssertCorrect(validInput, expectedSetTime); + } + + @Test + public void RelaxValidInputWithSpecificHours_returnsDate() { + final String validInput = "tuesday 10am"; + final String expectedSetTime = "Nov 01 2016 10:00"; + DateTimeInfoAndAssertCorrect(validInput, expectedSetTime); + } + + @Test + public void RelativeValidInputWithNoSpecificHoursAndMinutes_returnsDate() { + final String validInput = "next month"; + final String expectedSetTime = "Nov 26 2016 07:59"; + DateTimeInfoAndAssertCorrect(validInput, expectedSetTime); + } + + @Test + public void RelativeValidInputWithSpecificHours_returnsDate() { + final String validInput = "last month"; + final String expectedSetTime = "Sep 26 2016 07:59"; + DateTimeInfoAndAssertCorrect(validInput, expectedSetTime); + } + + @Test + public void IsDateNull_DateIsNull_True() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Feb 29 2000 00:00"); + assertTrue(testTime1.isDateNull()); + } + + @Test + public void IsDateNull_DateIsNotNull_False() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Feb 29 2001 00:00"); + assertTrue(!testTime1.isDateNull()); + } + + @Test + public void durationOfTheEvent_DurationIsPositive_7Days3hours() throws IllegalValueException { + testTime1 = new DateTimeInfo ("May 12 2017 07:00"); + testTime2 = new DateTimeInfo ("May 19 2017 10:00"); + String expected = "Duration of the event is: 7 days 3 hours."; + assertTrue(DateTimeInfo.durationOfTheEvent(testTime1.toString(), testTime2.toString()).equals(expected)); + } + + @Test + public void durationOfTheEvent_DurationIsNegative_Message() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Aug 12 2017 07:00"); + testTime2 = new DateTimeInfo ("Mar 19 2017 10:00"); + String expected = MESSAGE_FROM_IS_AFTER_TO; + assertTrue(DateTimeInfo.durationOfTheEvent(testTime1.toString(), testTime2.toString()).equals(expected)); + } + + @Test + public void durationOfTheEvent_ExactSameTiming_Message() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Jun 26 2017 07:00"); + testTime2 = new DateTimeInfo ("Jun 26 2017 07:00"); + String expected = "Event starts and end at the same time."; + assertTrue(DateTimeInfo.durationOfTheEvent(testTime1.toString(), testTime2.toString()).equals(expected)); + } + + @Test + public void isInTheFuture_validInput_True() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Jun 26 2017 07:00"); + testTime2 = new DateTimeInfo ("Jun 26 2018 07:00"); + assertTrue(DateTimeInfo.isInTheFuture(testTime1, testTime2)); + } + + @Test + public void isInTheFuture_invalidInput_False() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Jun 26 2017 07:00"); + testTime2 = new DateTimeInfo ("Jun 26 2018 07:00"); + assertTrue(!DateTimeInfo.isInTheFuture(testTime2, testTime1)); + } + + @Test + public void isInTheFuture_sameInput_True() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Jun 26 2017 07:00"); + assertTrue(DateTimeInfo.isInTheFuture(testTime1, testTime1)); + } + + @Test + public void isInThePast_validInput_True() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Jun 26 2017 07:00"); + testTime2 = new DateTimeInfo ("Jun 26 2018 07:00"); + assertTrue(DateTimeInfo.isInThePast(testTime2, testTime1)); + } + + @Test + public void isInThePast_invalidInput_False() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Jun 26 2017 07:00"); + testTime2 = new DateTimeInfo ("Jun 26 2018 07:00"); + assertTrue(!DateTimeInfo.isInThePast(testTime1, testTime2)); + } + + @Test + public void isInThePast_sameInput_True() throws IllegalValueException { + testTime1 = new DateTimeInfo ("Jun 26 2017 07:00"); + assertTrue(DateTimeInfo.isInThePast(testTime1, testTime1)); + } + + private void DateTimeInfoAndAssertCorrect(String validInput, String expectedSetTime) { + try { + testTime1 = new DateTimeInfo (validInput); + } catch (IllegalValueException e) { + } + assertTrue(expectedSetTime.equals(testTime1.toString())); + } + +}