diff --git a/build.gradle b/build.gradle index 46b06c1e42ec..8a473fb47220 100644 --- a/build.gradle +++ b/build.gradle @@ -52,6 +52,8 @@ allprojects { compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion" compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonDataTypeVersion" compile "com.google.guava:guava:$guavaVersion" + compile group: 'com.joestelmach', name: 'natty', version: '0.12' + compile group: 'org.slf4j', name: 'slf4j-nop', version: '1.7.10' testCompile "junit:junit:$junitVersion" testCompile "org.testfx:testfx-core:$testFxVersion" diff --git a/src/main/java/seedu/address/MainApp.java b/src/main/java/seedu/address/MainApp.java index 7ecf5d6396d8..eb60c21902f4 100644 --- a/src/main/java/seedu/address/MainApp.java +++ b/src/main/java/seedu/address/MainApp.java @@ -1,6 +1,9 @@ package seedu.address; import com.google.common.eventbus.Subscribe; +import com.joestelmach.natty.DateGroup; +import com.joestelmach.natty.Parser; + import javafx.application.Application; import javafx.application.Platform; import javafx.stage.Stage; @@ -23,6 +26,8 @@ import java.io.FileNotFoundException; import java.io.IOException; +import java.util.Date; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.logging.Logger; @@ -198,6 +203,8 @@ public void handleStoragePathChangedEvent(StoragePathChangedEvent event) { } public static void main(String[] args) { - launch(args); - } + launch(args); + } +} + diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index 3cc15de31285..99086f2030f4 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -5,7 +5,11 @@ import seedu.address.model.tag.UniqueTagList; import seedu.address.model.task.*; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.HashSet; +import java.util.List; import java.util.Set; /** @@ -15,65 +19,74 @@ public class AddCommand extends Command { public static final String COMMAND_WORD = "add"; - public static final String MESSAGE_USAGE = COMMAND_WORD + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Add an event with a starting and ending date or a task (with or without deadline) to the task manager.\n" + "Parameters: EVENT_NAME s/START_DATE e/END_DATE [t/TAG]... or TASK_NAME [d/DEADLINE] [t/TAG]...\n"// [p/PRIORITY_LEVEL] + "Example: " + COMMAND_WORD - + " Lecture s/7.10.2016-14 e/7.10.2016-16 t/CS2103, add Project Deadline d/14.10.2016 t/CS2103"; //p/3 + + " Lecture s/7.10.2016-14 e/7.10.2016-16 t/CS2103, add Project Deadline d/14.10.2016 t/CS2103"; // p/3 public static final String MESSAGE_EVENT_SUCCESS = "New event added: %1$s"; public static final String MESSAGE_TASK_SUCCESS = "New task added: %1$s"; public static final String MESSAGE_DUPLICATE_TASK = "It's already exists in the task manager"; + public static final String DATE_VALIDATION_REGEX = "^[0-3]?[0-9].[0-1]?[0-9].([0-9]{4})(-[0-2]?[0-9]?)?"; private final Task toAdd; /** * Convenience constructor using raw values. * - * @throws IllegalValueException if any of the raw values are invalid + * @throws IllegalValueException + * if any of the raw values are invalid */ - public AddCommand(String name, String deadline, Set tags,String freq) - throws IllegalValueException { + public AddCommand(String name, String deadline, Set tags, String freq) throws IllegalValueException { final Set tagSet = new HashSet<>(); + DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); + if (!deadline.matches(DATE_VALIDATION_REGEX) && !deadline.equals("")) { + List date = new com.joestelmach.natty.Parser().parse(deadline).get(0).getDates(); + deadline = dateFormat.format(date.get(0)).toString() + "-" + date.get(0).toString().substring(11, 13); + } for (String tagName : tags) { tagSet.add(new Tag(tagName)); } - if(deadline!=""&&freq!=""){ - this.toAdd = new Task( - new Name(name), - new Deadline(deadline), - new UniqueTagList(tagSet), - new Recurring(freq) - ); - }else if(deadline!=""){ - this.toAdd=new Task(new Name(name),new Deadline(deadline),new UniqueTagList(tagSet)); - }else{ - this.toAdd=new Task(new Name(name),new UniqueTagList(tagSet)); + if (deadline != "" && freq != "") { + this.toAdd = new Task(new Name(name), new Deadline(deadline), new UniqueTagList(tagSet), + new Recurring(freq)); + } else if (deadline != "") { + this.toAdd = new Task(new Name(name), new Deadline(deadline), new UniqueTagList(tagSet)); + } else { + this.toAdd = new Task(new Name(name), new UniqueTagList(tagSet)); } } /** * Convenience constructor using raw values. * - * @throws IllegalValueException if any of the raw values are invalid + * @throws IllegalValueException + * if any of the raw values are invalid */ - public AddCommand(String name, String startDate, String endDate, Set tags,String freq) + public AddCommand(String name, String startDate, String endDate, Set tags, String freq) throws IllegalValueException { final Set tagSet = new HashSet<>(); + DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); + List date; + if (!startDate.matches(DATE_VALIDATION_REGEX) && !startDate.equals("")) { + date = new com.joestelmach.natty.Parser().parse(startDate).get(0).getDates(); + startDate = dateFormat.format(date.get(0)).toString() + "-" + date.get(0).toString().substring(11, 13); + } + if (!endDate.matches(DATE_VALIDATION_REGEX) && !endDate.equals("")) { + date = new com.joestelmach.natty.Parser().parse(endDate).get(0).getDates(); + endDate = dateFormat.format(date.get(0)).toString() + "-" + date.get(0).toString().substring(11, 13); + } for (String tagName : tags) { tagSet.add(new Tag(tagName)); } - if(freq!=""&&startDate!=""){ - this.toAdd = new Task( - new Name(name), - new EventDate(startDate, endDate), - new UniqueTagList(tagSet), - new Recurring(freq) - ); - }else if(startDate!=""){ - this.toAdd=new Task(new Name(name),new EventDate(startDate,endDate),new UniqueTagList(tagSet)); - }else{ - this.toAdd=new Task(new Name(name),new UniqueTagList(tagSet)); + if (freq != "" && startDate != "") { + this.toAdd = new Task(new Name(name), new EventDate(startDate, endDate), new UniqueTagList(tagSet), + new Recurring(freq)); + } else if (startDate != "") { + this.toAdd = new Task(new Name(name), new EventDate(startDate, endDate), new UniqueTagList(tagSet)); + } else { + this.toAdd = new Task(new Name(name), new UniqueTagList(tagSet)); } } @@ -89,7 +102,7 @@ public CommandResult execute() { return new CommandResult(MESSAGE_DUPLICATE_TASK); } } - + public static String getSuccessMessage(Task toAdd) { if (toAdd.isEvent()) { return MESSAGE_EVENT_SUCCESS; diff --git a/src/main/java/seedu/address/logic/parser/Parser.java b/src/main/java/seedu/address/logic/parser/Parser.java index d5061da60ffd..2cb64fd9f2d8 100644 --- a/src/main/java/seedu/address/logic/parser/Parser.java +++ b/src/main/java/seedu/address/logic/parser/Parser.java @@ -48,6 +48,7 @@ public class Parser { Pattern.compile("(?[^/]+)" + "s/(?[^/]+)" + "e/(?[^/]+)" + "(?(?: t/[^/]+)*)"); // variable number of // tags + private static final String MESSAGE_INVALID_DATE = "Date format entered is invalid"; public static final Prefix deadlinePrefix = new Prefix("d/"); public static final Prefix tagPrefix = new Prefix("t/"); @@ -107,7 +108,7 @@ public Command parseCommand(String userInput) { case DoneCommand.COMMAND_WORD: return prepareMarkAsDone(arguments); - + case RefreshCommand.COMMAND_WORD: return new RefreshCommand(); @@ -161,12 +162,13 @@ private Command prepareAdd(String args) { argsTokenizer.tokenize(args); try { if (argsTokenizer.getTokenizedArguments().containsKey(namePrefix)) { - - if (!argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix) - && !argsTokenizer.getTokenizedArguments().containsKey(deadlinePrefix)) { - // non-recurring task - return new AddCommand(argsTokenizer.getValue(namePrefix).get(), "", - toSet(argsTokenizer.getAllValues(tagPrefix)), "");} + + if (!argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix) + && !argsTokenizer.getTokenizedArguments().containsKey(deadlinePrefix)) { + // non-recurring task + return new AddCommand(argsTokenizer.getValue(namePrefix).get(), "", + toSet(argsTokenizer.getAllValues(tagPrefix)), ""); + } // check if task is recurring floating task if (argsTokenizer.getTokenizedArguments().containsKey(deadlinePrefix) && argsTokenizer.getTokenizedArguments().containsKey(recurringPrefix)) { @@ -178,8 +180,7 @@ private Command prepareAdd(String args) { return new AddCommand(argsTokenizer.getValue(namePrefix).get(), argsTokenizer.getValue(deadlinePrefix).get(), toSet(argsTokenizer.getAllValues(tagPrefix)), ""); - } - else if (argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix) + } else if (argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix) && argsTokenizer.getTokenizedArguments().containsKey(endDatePrefix)) { if (!argsTokenizer.getTokenizedArguments().containsKey(recurringPrefix)) // non-recurring event @@ -199,6 +200,8 @@ else if (argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix) return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); } catch (IllegalValueException ive) { return new IncorrectCommand(ive.getMessage()); + } catch (Exception e) { + return new IncorrectCommand(MESSAGE_INVALID_DATE); } } diff --git a/src/test/java/guitests/AddCommandTest.java b/src/test/java/guitests/AddCommandTest.java index b35e27574d60..1d262a1ac9f7 100644 --- a/src/test/java/guitests/AddCommandTest.java +++ b/src/test/java/guitests/AddCommandTest.java @@ -12,27 +12,26 @@ public class AddCommandTest extends TaskManagerGuiTest { @Test public void add() { - //add one person + // add one person TestTask[] currentList = td.getTypicalTasks(); TestTask taskToAdd = td.project; assertAddSuccess(taskToAdd, currentList); currentList = TestUtil.addTasksToList(currentList, taskToAdd); - //add another person + // add another person taskToAdd = td.workshop; - //currentList = TestUtil.addTasksToList(currentList, taskToAdd); + // currentList = TestUtil.addTasksToList(currentList, taskToAdd); assertAddSuccess(taskToAdd, currentList); - - //add to empty list + + // add to empty list commandBox.runCommand("clear"); assertAddSuccess(td.friend); - - //add project to current list with flexi command - taskToAdd=td.project; - assertFlexiAddSuccess(taskToAdd,td.friend); - - //invalid command + // add project to current list with flexi command + taskToAdd = td.project; + assertFlexiAddSuccess(taskToAdd, td.friend); + + // invalid command commandBox.runCommand("adds Johnny"); assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND); } @@ -40,23 +39,25 @@ public void add() { private void assertAddSuccess(TestTask personToAdd, TestTask... currentList) { commandBox.runCommand(personToAdd.getAddCommand()); - //confirm the new card contains the right data + // confirm the new card contains the right data TaskCardHandle addedCard = taskListPanel.navigateToTask(personToAdd.getName().taskName); assertMatching(personToAdd, addedCard); - //confirm the list now contains all previous persons plus the new person + // confirm the list now contains all previous persons plus the new + // person TestTask[] expectedList = TestUtil.addTasksToList(currentList, personToAdd); assertTrue(taskListPanel.isListMatching(expectedList)); } - + private void assertFlexiAddSuccess(TestTask personToAdd, TestTask... currentList) { commandBox.runCommand(personToAdd.getFlexiAddCommand()); - //confirm the new card contains the right data + // confirm the new card contains the right data TaskCardHandle addedCard = taskListPanel.navigateToTask(personToAdd.getName().taskName); assertMatching(personToAdd, addedCard); - //confirm the list now contains all previous persons plus the new person + // confirm the list now contains all previous persons plus the new + // person TestTask[] expectedList = TestUtil.addTasksToList(currentList, personToAdd); assertTrue(taskListPanel.isListMatching(expectedList)); } diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index f205b6d22945..bab849a4d359 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -161,8 +161,6 @@ public void execute_add_invalidArgsFormat() throws Exception { public void execute_add_invalidTaskData() throws Exception { assertCommandBehavior( "add n/[]\\[;] d/11.12.2016", Name.MESSAGE_NAME_CONSTRAINTS); - assertCommandBehavior( - "add n/Valid Name d/not_numbers", Deadline.MESSAGE_DEADLINE_CONSTRAINTS); assertCommandBehavior( "add n/Valid Name d/11.12.2016-14 t/invalid_-[.tag", Tag.MESSAGE_TAG_CONSTRAINTS);