Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2122S1#79 from jovyntls/reminders-add…
Browse files Browse the repository at this point in the history
…-date-parsing

LGTM
  • Loading branch information
justintanyf authored Oct 18, 2021
2 parents f788828 + 8d29ea8 commit 0a8b772
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Messages {
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_CCA_DISPLAYED_INDEX = "The CCA index provided is invalid :(";
public static final String MESSAGE_INVALID_REMINDER_DISPLAYED_INDEX = "The reminder index provided is invalid :(";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_CCAS_LISTED_OVERVIEW = "%1$d CCAs listed!";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CCA_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_FREQUENCY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_OCCURRENCES;
import static seedu.address.logic.parser.CliSyntax.PREFIX_START_DATE;

import seedu.address.logic.commands.Command;
Expand All @@ -25,14 +23,10 @@ public class ReminderAddCommand extends Command {
+ PREFIX_CCA_ID + "CCA_ID "
+ PREFIX_NAME + "REMINDER_NAME "
+ PREFIX_START_DATE + "START_DATE "
+ "[" + PREFIX_FREQUENCY + "FREQUENCY] "
+ "[" + PREFIX_OCCURRENCES + "OCCURRENCES]\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_CCA_ID + "1 "
+ PREFIX_NAME + "NUSSO rehearsal "
+ PREFIX_START_DATE + "31-10-2021 "
+ PREFIX_FREQUENCY + "1w "
+ PREFIX_OCCURRENCES + "15";
+ PREFIX_START_DATE + "2021-10-31";

public static final String MESSAGE_SUCCESS = "New Reminder added: %1$s";
public static final String MESSAGE_DUPLICATE_REMINDER = "This Reminder already exists in the address book";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package seedu.address.logic.commands.reminder;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.reminder.Reminder;

public class ReminderDeleteCommand extends Command {
public static final String COMMAND_WORD = "deleter";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the Reminder identified by the index number used in the displayed Reminder list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_DELETE_REMINDER_SUCCESS = "Deleted Reminder: %1$s";

private final Index targetReminderIndex;

public ReminderDeleteCommand(Index targetReminderIndex) {
this.targetReminderIndex = targetReminderIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Reminder> lastShownList = model.getFilteredReminderList();

if (targetReminderIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_REMINDER_DISPLAYED_INDEX);
}

Reminder reminderToDelete = lastShownList.get(targetReminderIndex.getZeroBased());
model.deleteReminder(reminderToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_REMINDER_SUCCESS, reminderToDelete));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ReminderDeleteCommand // instanceof handles nulls
&& targetReminderIndex.equals(((ReminderDeleteCommand) other).targetReminderIndex)); // state check
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import seedu.address.logic.commands.person.PersonEditCommand;
import seedu.address.logic.commands.person.PersonFindCommand;
import seedu.address.logic.commands.reminder.ReminderAddCommand;
import seedu.address.logic.commands.reminder.ReminderDeleteCommand;
import seedu.address.logic.parser.cca.CcaAddCommandParser;
import seedu.address.logic.parser.cca.CcaDeleteCommandParser;
import seedu.address.logic.parser.cca.CcaEnrolCommandParser;
Expand All @@ -33,6 +34,7 @@
import seedu.address.logic.parser.person.PersonEditCommandParser;
import seedu.address.logic.parser.person.PersonFindCommandParser;
import seedu.address.logic.parser.reminder.ReminderAddCommandParser;
import seedu.address.logic.parser.reminder.ReminderDeleteCommandParser;


/**
Expand Down Expand Up @@ -107,6 +109,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ReminderAddCommand.COMMAND_WORD:
return new ReminderAddCommandParser().parse(arguments);

case ReminderDeleteCommand.COMMAND_WORD:
return new ReminderDeleteCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -17,6 +18,7 @@
import seedu.address.model.person.Phone;
import seedu.address.model.person.Pid;
import seedu.address.model.reminder.ReminderName;
import seedu.address.model.reminder.ReminderStartDate;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -86,6 +88,27 @@ public static ReminderName parseReminderName(String name) throws ParseException
return new ReminderName(trimmedName);
}

/**
* Parses a {@code String name} into a {@code ReminderName}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code name} is invalid.
*/
public static ReminderStartDate parseReminderStartDate(String date) throws ParseException {
requireNonNull(date);
String trimmedDateText = date.trim();
if (!ReminderStartDate.isValidDate(trimmedDateText)) {
throw new ParseException(ReminderStartDate.MESSAGE_CONSTRAINTS);
}
Date startDate;
try {
startDate = ReminderStartDate.PARSE_INPUT_DATE_FORMAT.parse(trimmedDateText);
} catch (java.text.ParseException e) {
throw new ParseException(ReminderStartDate.PARSE_DATE_CONSTRAINTS);
}
return new ReminderStartDate(startDate);
}

/**
* Parses a {@code String phone} into a {@code Phone}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_START_DATE;

import java.util.stream.Stream;

Expand All @@ -14,6 +15,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.reminder.Reminder;
import seedu.address.model.reminder.ReminderName;
import seedu.address.model.reminder.ReminderStartDate;

public class ReminderAddCommandParser implements Parser<ReminderAddCommand> {
/**
Expand All @@ -23,19 +25,20 @@ public class ReminderAddCommandParser implements Parser<ReminderAddCommand> {
*/
public ReminderAddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME);
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_START_DATE);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME)
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_START_DATE)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ReminderAddCommand.MESSAGE_USAGE));
}

//TODO: update
ReminderName reminderName = ParserUtil.parseReminderName(argMultimap.getValue(PREFIX_NAME).get());
ReminderStartDate reminderStartDate = ParserUtil.parseReminderStartDate(
argMultimap.getValue(PREFIX_START_DATE).get());

// Create a new reminder here
// TODO: update constructor
Reminder reminder = new Reminder(reminderName);
// Create a new reminder
Reminder reminder = new Reminder(reminderName, reminderStartDate);

return new ReminderAddCommand(reminder);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.address.logic.parser.reminder;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.reminder.ReminderDeleteCommand;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new ReminderDeleteCommand object
*/
public class ReminderDeleteCommandParser implements Parser<ReminderDeleteCommand> {
/**
* Parses the given {@code String} of arguments in the context of the PersonDeleteCommand
* and returns a PersonDeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ReminderDeleteCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new ReminderDeleteCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ReminderDeleteCommand.MESSAGE_USAGE), pe);
}
}
}
1 change: 0 additions & 1 deletion src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public boolean hasReminder(Reminder reminder) {
*/
public void addReminder(Reminder reminder) {
reminders.add(reminder);
reminder.setRid(reminders.getCurrentIndex() + 1);
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/seedu/address/model/reminder/Reminder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@
public class Reminder {
// Identity fields
private final ReminderName reminderName;
private int rid;
private final ReminderStartDate reminderStartDate;

/**
* Every field must be present and not null.
*/
public Reminder(ReminderName reminderName) {
public Reminder(ReminderName reminderName, ReminderStartDate reminderStartDate) {
requireAllNonNull(reminderName);
this.reminderName = reminderName;
this.reminderStartDate = reminderStartDate;
}

public ReminderName getName() {
return reminderName;
}

public void setRid(int rid) {
this.rid = rid;
}

public int getRid() {
return rid;
public ReminderStartDate getStartDate() {
return reminderStartDate;
}

/**
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/seedu/address/model/reminder/ReminderStartDate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package seedu.address.model.reminder;

import static java.util.Objects.requireNonNull;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ReminderStartDate {
public static final String MESSAGE_CONSTRAINTS =
"Dates should be entered in YYYY-MM-DD format, e.g. 2021-5-23";
public static final String PARSE_DATE_CONSTRAINTS = "This date could not be parsed. Is it a valid date?";

/*
* The date must be in YYYY-MM-DD format.
*/
public static final String VALIDATION_REGEX = "\\d{4}-\\d{1,2}-\\d{1,2}";
public static final SimpleDateFormat PARSE_INPUT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
public static final SimpleDateFormat PARSE_DATE_TO_STRING_FORMAT = new SimpleDateFormat("E, dd MMM yyyy");


public final Date startDate;

/**
* Constructs a {@code ReminderStartDate}.
*
* @param date A valid date in YYYY-MM-DD format.
*/
public ReminderStartDate(Date date) {
requireNonNull(date);
startDate = date;
}

/**
* Returns true if a given string is a valid name.
*/
public static boolean isValidDate(String test) {
return test.matches(VALIDATION_REGEX);
}

@Override
public String toString() {
return PARSE_DATE_TO_STRING_FORMAT.format(startDate);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ReminderStartDate // instanceof handles nulls
&& startDate.toString().equals(((ReminderStartDate) other).startDate.toString())); // state check
}

@Override
public int hashCode() {
return startDate.hashCode();
}
}
12 changes: 0 additions & 12 deletions src/main/java/seedu/address/model/reminder/UniqueReminderList.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ public class UniqueReminderList implements Iterable<Reminder> {
private final ObservableList<Reminder> internalUnmodifiableList =
FXCollections.unmodifiableObservableList(internalList);

/**
* Returns the current largest rid
* @return the current largest rid
*/
public int getCurrentIndex() {
if (this.internalList.size() == 0) {
return 0;
} else {
return this.internalList.get(this.internalList.size() - 1).getRid();
}
}

/**
* Returns true if the list contains an equivalent Reminder as the given argument.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/seedu/address/ui/ReminderCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class ReminderCard extends UiPart<Region> {
@FXML
private Label name;
@FXML
private Label date;
@FXML
private Label id;
@FXML
private Label frequency;
Expand All @@ -54,8 +56,9 @@ public ReminderCard(Reminder reminder, int displayedIndex) {
this.reminder = reminder;
id.setText(displayedIndex + ". ");
this.name.setText(reminder.getName().fullName);
this.frequency.setText(""); // to test if this shows up as an empty line
this.occurrences.setText("temp occ");
this.date.setText(reminder.getStartDate().toString());
// this.frequency.setText(""); // to test if this shows up as an empty line
// this.occurrences.setText("temp occ");

// each CCA has a field for color? can do an enum
// then mapping from enum value to the binding
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/view/ReminderListCard.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Label fx:id="name" text="\$first" styleClass="cell_big_label" />
</HBox>
<FlowPane fx:id="tags" />
<Label fx:id="date" text="\$date" styleClass="cell_small_label" />
<Label fx:id="frequency" styleClass="cell_small_label" text="\$frequency" />
<Label fx:id="occurrences" styleClass="cell_small_label" text="\$occurrences" />
</VBox>
Expand Down

0 comments on commit 0a8b772

Please sign in to comment.