Skip to content

Commit

Permalink
Merge pull request #91 from CS2103AUG2016-T14-C2/V0.4-Modify-Remove
Browse files Browse the repository at this point in the history
Merge V0.4-Modify-Remove
  • Loading branch information
qhng authored Oct 26, 2016
2 parents bf1cf2e + 6df335e commit 826ec60
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class SmartDefaultDates {
private final Calendar today;

/**
*
* Determines the smart defaults for the dates provided. Can set both
* start and end dates as null to get a basic smart default.
* @param startDateTime Starting date time
* @param endDateTime Ending date time
* @throws InvalidDateException If endDateTime is earlier than startDateTime
*/
public SmartDefaultDates(InferredDate startDateTime, InferredDate endDateTime) {
calendar = Calendar.getInstance();
Expand Down Expand Up @@ -50,7 +50,11 @@ public SmartDefaultDates(InferredDate startDateTime, InferredDate endDateTime) {
public Date getEnd(InferredDate endDateTime) {
if (endDateTime == null) return null;
calendar.setTime(endDateTime.getInferredDateTime());
if (endDateTime.isDateInferred()) {
if (endDateTime.isDateInferred() && endDateTime.isTimeInferred()) {
// user didn't specify anything
// remove date field
return null;
} else if (endDateTime.isDateInferred()) {
// date not supplied
// defaults to today
calendar.set(Calendar.DATE, today.get(Calendar.DATE));
Expand Down Expand Up @@ -94,7 +98,11 @@ private void parseEnd(InferredDate endDateTime) {
public Date getStart(InferredDate startDateTime) {
if (startDateTime == null) return null;
calendar.setTime(startDateTime.getInferredDateTime());
if (startDateTime.isDateInferred()) {
if (startDateTime.isDateInferred() && startDateTime.isTimeInferred()) {
// user didn't specify anything
// remove date field
return null;
} else if (startDateTime.isDateInferred()) {
// date not supplied
// defaults to today
calendar.set(Calendar.DATE, today.get(Calendar.DATE));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package seedu.savvytasker.logic.commands;

import seedu.savvytasker.commons.core.UnmodifiableObservableList;
import seedu.savvytasker.commons.util.SmartDefaultDates;
import seedu.savvytasker.logic.parser.DateParser.InferredDate;
import seedu.savvytasker.model.task.PriorityLevel;
import seedu.savvytasker.model.task.ReadOnlyTask;
Expand Down Expand Up @@ -62,8 +61,7 @@ private void createTask() {
final boolean isArchived = false; // all tasks are first added as active tasks
final int taskId = 0; // taskId to be assigned by ModelManager, leave as 0

SmartDefaultDates sdd = new SmartDefaultDates(startDateTime, endDateTime);
this.toAdd = new Task(taskId, taskName, sdd.getStartDate(), sdd.getEndDate(),
this.toAdd = new Task(taskId, taskName, startDateTime, endDateTime,
location, priority, recurringType, numberOfRecurrence,
category, description, isArchived);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import seedu.savvytasker.commons.core.Messages;
import seedu.savvytasker.commons.core.UnmodifiableObservableList;
import seedu.savvytasker.commons.util.SmartDefaultDates;
import seedu.savvytasker.logic.parser.DateParser.InferredDate;
import seedu.savvytasker.model.task.PriorityLevel;
import seedu.savvytasker.model.task.ReadOnlyTask;
Expand Down Expand Up @@ -70,13 +69,10 @@ public CommandResult execute() {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}


SmartDefaultDates sdd = new SmartDefaultDates(null, null);

ReadOnlyTask taskToModify = lastShownList.get(index - 1);
replacement = new Task(taskToModify, taskName, sdd.getStart(startDateTime),
sdd.getEnd(endDateTime), location, priority,
replacement = new Task(taskToModify, taskName, startDateTime,
endDateTime, location, priority,
recurringType, numberOfRecurrence,
category, description);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class InferredDate {
private final boolean dateInferred;
private final boolean timeInferred;

private InferredDate(Date inferredDateTime, boolean dateInferred, boolean timeInferred) {
public InferredDate(Date inferredDateTime, boolean dateInferred, boolean timeInferred) {
this.inferredDateTime = inferredDateTime;
this.dateInferred = dateInferred;
this.timeInferred = timeInferred;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Command parse(String userInput) {
String trueHeader = extractHeader(header);
CommandParser<? extends Command> parser = commandParsers.get(trueHeader);
if (parser == null)
return new IncorrectCommand(header + body, MESSAGE_UNKNOWN_COMMAND);
return new IncorrectCommand(header + body, String.format(MESSAGE_UNKNOWN_COMMAND, HelpCommand.MESSAGE_USAGE));
if (parser.shouldPreprocess())
body = preprocessBody(body);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@@author A0139916U
package seedu.savvytasker.logic.parser;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -29,15 +30,15 @@ public class ModifyCommandParser implements CommandParser<ModifyCommand> {

private static final Pattern REGEX_PATTERN = Pattern.compile(
HEADER+"\\s+(?<"+REGEX_REF_INDEX+">([^/]+?(\\s+|$))+)((?<=\\s)(" +
"(t/(?<"+REGEX_REF_TASK_NAME+">[^/]+)(?!.*\\st/))|" +
"(s/(?<"+REGEX_REF_START_DATE+">[^/]+)(?!.*\\ss/))|" +
"(e/(?<"+REGEX_REF_END_DATE+">[^/]+)(?!.*\\se/))|" +
"(l/(?<"+REGEX_REF_LOCATION+">[^/]+)(?!.*\\sl/))|" +
"(t/(?<"+REGEX_REF_TASK_NAME+">[^/]*)(?!.*\\st/))|" +
"(s/(?<"+REGEX_REF_START_DATE+">[^/]*)(?!.*\\ss/))|" +
"(e/(?<"+REGEX_REF_END_DATE+">[^/]*)(?!.*\\se/))|" +
"(l/(?<"+REGEX_REF_LOCATION+">[^/]*)(?!.*\\sl/))|" +
"(p/(?<"+REGEX_REF_PRIORITY_LEVEL+">[^/]+)(?!.*\\sp/))|" +
"(r/(?<"+REGEX_REF_RECURRING_TYPE+">[^/]+)(?!.*\\sr/))|" +
"(n/(?<"+REGEX_REF_NUMBER_OF_RECURRENCE+">[^/]+?)(?!.*\\sn/))|" +
"(c/(?<"+REGEX_REF_CATEGORY+">[^/]+)(?!.*\\sc/))|" +
"(d/(?<"+REGEX_REF_DESCRIPTION+">[^/]+)(?!.*\\sd/))" +
"(n/(?<"+REGEX_REF_NUMBER_OF_RECURRENCE+">[^/]+)(?!.*\\sn/))|" +
"(c/(?<"+REGEX_REF_CATEGORY+">[^/]*)(?!.*\\sc/))|" +
"(d/(?<"+REGEX_REF_DESCRIPTION+">[^/]*)(?!.*\\sd/))" +
")(\\s|$)){0,11}", Pattern.CASE_INSENSITIVE);

private static final TaskFieldParser TASK_PARSER = new TaskFieldParser();
Expand All @@ -59,8 +60,8 @@ public ModifyCommand parse(String commandText) throws ParseException {
if (matcher.matches()) {

int index = parseIndex(matcher.group(REGEX_REF_INDEX));
InferredDate startDate = TASK_PARSER.parseStartDate(matcher.group(REGEX_REF_START_DATE));
InferredDate endDate = TASK_PARSER.parseEndDate(matcher.group(REGEX_REF_END_DATE));
InferredDate startDate = parseDate(matcher.group(REGEX_REF_START_DATE));
InferredDate endDate = parseDate(matcher.group(REGEX_REF_END_DATE));
String taskName = TASK_PARSER.parseTaskName(matcher.group(REGEX_REF_TASK_NAME));
String location = TASK_PARSER.parseLocation(matcher.group(REGEX_REF_LOCATION));
PriorityLevel priority = TASK_PARSER.parsePriorityLevel(matcher.group(REGEX_REF_PRIORITY_LEVEL));
Expand All @@ -86,4 +87,12 @@ private int parseIndex(String indexText) throws ParseException {
throw new ParseException(indexText, "INDEX: " + ex.getFailureDetails());
}
}

private InferredDate parseDate(String dateText) throws ParseException {
if (dateText != null && dateText.trim().isEmpty()) {
return TASK_PARSER.dateParser.new InferredDate(new Date(), true, true);
}

return TASK_PARSER.parseStartDate(dateText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* This class contains common parsing methods for parsing Task fields.
*/
public class TaskFieldParser {
private DateParser dateParser;
protected final DateParser dateParser;

public TaskFieldParser() {
this.dateParser = new DateParser();
Expand Down
82 changes: 79 additions & 3 deletions src/main/java/seedu/savvytasker/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.Date;
import java.util.Objects;

import seedu.savvytasker.commons.util.SmartDefaultDates;
import seedu.savvytasker.logic.parser.DateParser.InferredDate;

//@@author A0139915W
/**
* Represents a Task in the task list.
Expand All @@ -21,9 +24,46 @@ public class Task implements ReadOnlyTask {
private String description;
private boolean isArchived;

/**
* Constructor with smart defaults
*/
public Task(int id, String taskName, InferredDate startDateTime, InferredDate endDateTime, String location,
PriorityLevel priority, RecurrenceType recurringType, Integer numberOfRecurrence,
String category, String description, boolean isArchived) {

SmartDefaultDates sdd = new SmartDefaultDates(startDateTime, endDateTime);
this.id = id;
this.taskName = taskName;
this.startDateTime = sdd.getStartDate();
this.endDateTime = sdd.getEndDate();
this.location = location;
if (priority == null) {
this.priority = PriorityLevel.Medium;
} else {
this.priority = priority;
}
if (recurringType == null) {
this.recurringType = RecurrenceType.None;
} else {
this.recurringType = recurringType;
}
if (numberOfRecurrence == null) {
this.numberOfRecurrence = 0;
} else {
this.numberOfRecurrence = numberOfRecurrence.intValue();
}
this.category = category;
this.description = description;
this.isArchived = isArchived;
}

/**
* Constructor without smart defaults
*/
public Task(int id, String taskName, Date startDateTime, Date endDateTime, String location,
PriorityLevel priority, RecurrenceType recurringType, Integer numberOfRecurrence,
String category, String description, boolean isArchived) {

this.id = id;
this.taskName = taskName;
this.startDateTime = startDateTime;
Expand Down Expand Up @@ -70,7 +110,7 @@ public Task(ReadOnlyTask source) {
/**
* Copy and modify constructor
*/
public Task(ReadOnlyTask source, String taskName, Date startDateTime, Date endDateTime, String location,
public Task(ReadOnlyTask source, String taskName, InferredDate startDateTime, InferredDate endDateTime, String location,
PriorityLevel priority, RecurrenceType recurringType, Integer numberOfRecurrence, String category,
String description) {
this(source.getId(), source.getTaskName(), source.getStartDateTime(),
Expand All @@ -81,15 +121,51 @@ public Task(ReadOnlyTask source, String taskName, Date startDateTime, Date endDa
//this.id should follow that of the source.
//this.isArchived should follow that of the source.
this.taskName = taskName == null ? this.taskName : taskName;
this.startDateTime = startDateTime == null ? this.startDateTime : startDateTime;
this.endDateTime = endDateTime == null ? this.endDateTime : endDateTime;
setStartDate(startDateTime);
setEndDate(endDateTime);
this.location = location == null ? this.location : location;
this.priority = priority == null ? this.priority : priority;
this.recurringType = recurringType == null ? this.recurringType : recurringType;
this.numberOfRecurrence = numberOfRecurrence == null ? this.numberOfRecurrence : numberOfRecurrence.intValue();
this.category = category == null ? this.category : category;
this.description = description == null ? this.description : description;
}

private void setStartDate(InferredDate inferredDate) {
if (inferredDate == null) {
// user didn't specify s/
// keep existing start date
} else {
if (inferredDate.isDateInferred() && inferredDate.isTimeInferred()) {
// user specified s/ but with nothing tagged to it
// remove existing start date
this.startDateTime = null;
} else {
// user specified s/ with something tagged to it
// update existing start date
SmartDefaultDates sdd = new SmartDefaultDates(null, null);
this.startDateTime = sdd.getStart(inferredDate);
}
}
}

private void setEndDate(InferredDate inferredDate) {
if (inferredDate == null) {
// user didn't specify e/
// keep existing end date
} else {
if (inferredDate.isDateInferred() && inferredDate.isTimeInferred()) {
// user specified e/ but with nothing tagged to it
// remove existing end date
this.endDateTime = null;
} else {
// user specified e/ with something tagged to it
// update existing end date
SmartDefaultDates sdd = new SmartDefaultDates(null, null);
this.endDateTime = sdd.getStart(inferredDate);
}
}
}

@Override
public int getId() {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/guitests/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.junit.Test;

import seedu.savvytasker.commons.core.Messages;
import seedu.savvytasker.logic.commands.HelpCommand;
import seedu.savvytasker.testutil.TestTask;
import seedu.savvytasker.testutil.TestUtil;

import static org.junit.Assert.assertTrue;
import static seedu.savvytasker.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;

//@@author A0139915W
public class AddCommandTest extends SavvyTaskerGuiTest {
Expand All @@ -32,7 +34,7 @@ public void add() {

//invalid command
commandBox.runCommand("adds Bad Command Task");
assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
assertResultMessage(String.format(MESSAGE_UNKNOWN_COMMAND, HelpCommand.MESSAGE_USAGE));
}

private void assertAddSuccess(TestTask taskToAdd, TestTask... currentList) {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/guitests/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import org.junit.Test;

import seedu.savvytasker.commons.core.Messages;
import seedu.savvytasker.logic.commands.HelpCommand;
import seedu.savvytasker.testutil.TestTask;

import static org.junit.Assert.assertTrue;
import static seedu.savvytasker.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;

//@@author A0139915W
public class FindCommandTest extends SavvyTaskerGuiTest {
Expand Down Expand Up @@ -45,7 +47,7 @@ public void find_emptyList(){
@Test
public void find_invalidCommand_fail() {
commandBox.runCommand("findmyring");
assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
assertResultMessage(String.format(MESSAGE_UNKNOWN_COMMAND, HelpCommand.MESSAGE_USAGE));
}

private void assertFindResult(String command, TestTask... expectedHits) {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/guitests/ListCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import org.junit.Test;

import seedu.savvytasker.commons.core.Messages;
import seedu.savvytasker.logic.commands.HelpCommand;
import seedu.savvytasker.testutil.TestTask;

import static org.junit.Assert.assertTrue;
import static seedu.savvytasker.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;

//@@author A0139915W
public class ListCommandTest extends SavvyTaskerGuiTest {
Expand Down Expand Up @@ -46,7 +48,7 @@ public void list_emptyList(){
@Test
public void find_invalidCommand_fail() {
commandBox.runCommand("listmytasks");
assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
assertResultMessage(String.format(MESSAGE_UNKNOWN_COMMAND, HelpCommand.MESSAGE_USAGE));
}

private void assertListResult(String command, TestTask... expectedHits ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void assertCommandBehavior(String inputCommand, String expectedMessage,
@Test
public void execute_unknownCommandWord() throws Exception {
String unknownCommand = "uicfhmowqewca";
assertCommandBehavior(unknownCommand, MESSAGE_UNKNOWN_COMMAND);
assertCommandBehavior(unknownCommand, String.format(MESSAGE_UNKNOWN_COMMAND, HelpCommand.MESSAGE_USAGE));
}

@Test
Expand Down

0 comments on commit 826ec60

Please sign in to comment.