Skip to content

Commit

Permalink
Merge pull request #96 from TSAI-HSIAO-HAN/ReminderAssociation
Browse files Browse the repository at this point in the history
Reminder association
  • Loading branch information
TSAI-HSIAO-HAN committed Oct 16, 2019
2 parents 7593405 + fc5b115 commit 79383bd
Show file tree
Hide file tree
Showing 20 changed files with 845 additions and 5 deletions.
87 changes: 87 additions & 0 deletions src/main/java/seedu/mark/logic/commands/AddReminderCommand.java
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));
}
}

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());
}


}
2 changes: 2 additions & 0 deletions src/main/java/seedu/mark/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public class CliSyntax {
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_FOLDER = new Prefix("f/");
public static final Prefix PREFIX_PARENT_FOLDER = new Prefix("p/");
public static final Prefix PREFIX_NOTE = new Prefix("n/");
public static final Prefix PREFIX_TIME = new Prefix("t/");
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/mark/logic/parser/MarkParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import seedu.mark.logic.commands.AddCommand;
import seedu.mark.logic.commands.AddFolderCommand;
import seedu.mark.logic.commands.AddReminderCommand;
import seedu.mark.logic.commands.ClearCommand;
import seedu.mark.logic.commands.Command;
import seedu.mark.logic.commands.DeleteCommand;
Expand Down Expand Up @@ -96,6 +97,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddFolderCommand.COMMAND_WORD:
return new AddFolderCommandParser().parse(arguments);

case AddReminderCommand.COMMAND_WORD:
return new AddReminderCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/seedu/mark/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static java.util.Objects.requireNonNull;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -13,6 +16,7 @@
import seedu.mark.model.bookmark.Name;
import seedu.mark.model.bookmark.Remark;
import seedu.mark.model.bookmark.Url;
import seedu.mark.model.reminder.Note;
import seedu.mark.model.tag.Tag;

/**
Expand All @@ -22,6 +26,9 @@ public class ParserUtil {

public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer.";

private static final String DATE_FORMATTER = "dd/MM/yyyy HHmm";
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER);

/**
* Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be
* trimmed.
Expand Down Expand Up @@ -126,4 +133,52 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
}
return tagSet;
}

/**
* Parses a {@code String note} into a {@code Note}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code note} is invalid.
*/
public static Note parseNote(String note) throws ParseException {
requireNonNull(note);
String trimmedNote = note.trim();

if (!Note.isValidNote(note)) {
throw new ParseException(Note.MESSAGE_CONSTRAINTS);
}
return new Note(trimmedNote);
}

/**
* Parses a {@code String time} into a {@code LocalDateTime}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code time} is invalid.
*/
public static LocalDateTime parseTime(String time) throws ParseException {
requireNonNull(time);
String trimmedTime = time.trim();
LocalDateTime getTime;

try {
getTime = LocalDateTime.parse(trimmedTime, formatter);
} catch (DateTimeParseException e) {
throw new ParseException("Invalid time format! Please use the following format: " + DATE_FORMATTER);
}

return getTime;
}

/**
* Parses to get the formatted time from {@code time}.
*
* @param time the time to parse.
* @return the formatted time.
*/
public static String getFormattedTime(LocalDateTime time) {
requireNonNull(time);
String formatTime = time.format(formatter);
return formatTime;
}
}
70 changes: 70 additions & 0 deletions src/main/java/seedu/mark/model/Mark.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import java.util.Objects;

import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;

import seedu.mark.model.bookmark.Bookmark;
import seedu.mark.model.bookmark.Folder;
import seedu.mark.model.bookmark.UniqueBookmarkList;
import seedu.mark.model.folderstructure.FolderStructure;
import seedu.mark.model.reminder.Reminder;
import seedu.mark.model.reminder.ReminderAssociation;

/**
* Wraps all data at the bookmark-manager level
Expand All @@ -23,9 +26,12 @@ public class Mark implements ReadOnlyMark {

private final FolderStructure folderStructure;

private final ReminderAssociation reminderAssociation;

public Mark() {
bookmarks = new UniqueBookmarkList();
folderStructure = new FolderStructure(Folder.ROOT_FOLDER, new ArrayList<>());
reminderAssociation = new ReminderAssociation();
}

/**
Expand Down Expand Up @@ -114,6 +120,55 @@ public void addFolder(Folder folder, Folder parentFolder) {
this.folderStructure.addFolder(folder, parentFolder);
}

//// reminder operations

/**
* Replaces the association of reminder association with the specified {@code association}.
*
* @param association the specified association that is used.
*/
public void setReminderAssociation(ObservableMap<Bookmark, Reminder> association) {
this.reminderAssociation.setAssociation(association);
}

/**
* Gets a list of all reminders in time ascending order.
*
* @return a list of reminder in time ascending order.
*/
public ObservableList<Reminder> getReminders() {
return this.reminderAssociation.getReminderList();
}

/**
* Adds a reminder that opens a specific bookmark.
*
* @param bookmark the bookmark that is opened by the reminder.
* @param reminder the reminder to be added.
*/
public void addReminder(Bookmark bookmark, Reminder reminder) {
this.reminderAssociation.addReminder(bookmark, reminder);
}

/**
* Removes a specific reminder.
*
* @param reminder the reminder to be removed.
*/
public void removeReminder(Reminder reminder) {
this.reminderAssociation.deleteReminder(reminder);
}

/**
* Edits a specific reminder.
*
* @param targetReminder the reminder to be edited.
* @param replaceReminder the edited reminder.
*/
public void editReminder(Reminder targetReminder, Reminder replaceReminder) {
this.reminderAssociation.setReminder(targetReminder, replaceReminder);
}

//// util methods

@Override
Expand All @@ -132,6 +187,11 @@ public FolderStructure getFolderStructure() {
return folderStructure;
}

@Override
public ReminderAssociation getReminderAssociation() {
return reminderAssociation;
}


public boolean hasFolder(Folder folder) {
return getFolderStructure().hasFolder(folder);
Expand All @@ -145,6 +205,16 @@ public boolean equals(Object other) {
&& folderStructure.equals(((Mark) other).folderStructure));
}

/**
* Checks if the bookmark already has reminder.
*
* @param bookmark the bookmark to check.
* @return whether the bookmark already has a reminder.
*/
public boolean isBookmarkHasReminder(Bookmark bookmark) {
return reminderAssociation.isBookmarkHasReminder(bookmark);
}

@Override
public int hashCode() {
return Objects.hash(bookmarks, folderStructure);
Expand Down

0 comments on commit 79383bd

Please sign in to comment.