forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from TSAI-HSIAO-HAN/ReminderAssociation
Reminder association
- Loading branch information
Showing
20 changed files
with
845 additions
and
5 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/seedu/mark/logic/commands/AddReminderCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package seedu.mark.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.mark.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.mark.logic.parser.CliSyntax.PREFIX_NOTE; | ||
import static seedu.mark.logic.parser.CliSyntax.PREFIX_TIME; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import seedu.mark.commons.core.Messages; | ||
import seedu.mark.commons.core.index.Index; | ||
import seedu.mark.logic.commands.exceptions.CommandException; | ||
import seedu.mark.logic.commands.results.CommandResult; | ||
import seedu.mark.model.Model; | ||
import seedu.mark.model.bookmark.Bookmark; | ||
import seedu.mark.model.reminder.Note; | ||
import seedu.mark.model.reminder.Reminder; | ||
import seedu.mark.storage.Storage; | ||
|
||
/** | ||
* Adds a reminder to Mark. | ||
*/ | ||
public class AddReminderCommand extends Command { | ||
public static final String COMMAND_WORD = "reminder"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a reminder to Mark.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ PREFIX_TIME + "TIME " | ||
+ "[" + PREFIX_NOTE + "NOTE] \n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_TIME + "07/01/2020 1300 " | ||
+ PREFIX_NOTE + "Check the schedule "; | ||
|
||
public static final String MESSAGE_SUCCESS = "New reminder added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_REMINDER = "This bookmark already has a reminder."; | ||
|
||
private final Index index; | ||
private final Note note; | ||
private final LocalDateTime time; | ||
|
||
/** | ||
* Creates an AddReminderCommand to add the specified {@code Reminder} that opens bookmark at {@code Index}. | ||
* | ||
* @param index the index of bookmark opened by the reminder. | ||
* @param note the note of the reminder to be added. | ||
* @param time the time of the reminder to be added. | ||
*/ | ||
public AddReminderCommand(Index index, Note note, LocalDateTime time) { | ||
requireAllNonNull(index, note, time); | ||
|
||
this.index = index; | ||
this.note = note; | ||
this.time = time; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model, Storage storage) throws CommandException { | ||
requireNonNull(model); | ||
List<Bookmark> lastShownList = model.getFilteredBookmarkList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_BOOKMARK_DISPLAYED_INDEX); | ||
} | ||
|
||
Bookmark bookmarkToOpen = lastShownList.get(index.getZeroBased()); | ||
Reminder reminderToAdd = new Reminder(bookmarkToOpen, time, note); | ||
|
||
if (model.isBookmarkHasReminder(bookmarkToOpen)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_REMINDER); | ||
} | ||
|
||
model.addReminder(bookmarkToOpen, reminderToAdd); | ||
model.saveMark(); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, reminderToAdd)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof AddReminderCommand // instanceof handles nulls | ||
&& note.equals(((AddReminderCommand) other).note) | ||
&& time.equals(((AddReminderCommand) other).time) | ||
&& index.equals(((AddReminderCommand) other).index)); | ||
} | ||
} | ||
|
62 changes: 62 additions & 0 deletions
62
src/main/java/seedu/mark/logic/parser/AddReminderCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package seedu.mark.logic.parser; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import static seedu.mark.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.mark.logic.parser.CliSyntax.PREFIX_NOTE; | ||
import static seedu.mark.logic.parser.CliSyntax.PREFIX_TIME; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.stream.Stream; | ||
|
||
import seedu.mark.commons.core.index.Index; | ||
import seedu.mark.logic.commands.AddReminderCommand; | ||
import seedu.mark.logic.parser.exceptions.ParseException; | ||
import seedu.mark.model.reminder.Note; | ||
|
||
/** | ||
* Parses input arguments and creates a new AddReminderCommand object | ||
*/ | ||
public class AddReminderCommandParser implements Parser<AddReminderCommand> { | ||
@Override | ||
public AddReminderCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_TIME, PREFIX_NOTE); | ||
|
||
Index index; | ||
|
||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
System.out.println(argMultimap.getPreamble()); | ||
} catch (ParseException pe) { | ||
System.out.println("no index"); | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
AddReminderCommand.MESSAGE_USAGE), pe); | ||
} | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_TIME)) { | ||
System.out.println("no time"); | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddReminderCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
|
||
|
||
// compulsory fields | ||
LocalDateTime time = ParserUtil.parseTime(argMultimap.getValue(PREFIX_TIME).get()); | ||
|
||
// optional fields | ||
Note note = ParserUtil.parseNote(argMultimap.getValue(PREFIX_NOTE).orElse(Note.DEFAULT_VALUE)); | ||
|
||
return new AddReminderCommand(index, note, time); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.