diff --git a/src/main/java/seedu/address/commons/core/Messages.java b/src/main/java/seedu/address/commons/core/Messages.java index 1deb3a1e4695..12e2d2b64ee8 100644 --- a/src/main/java/seedu/address/commons/core/Messages.java +++ b/src/main/java/seedu/address/commons/core/Messages.java @@ -8,6 +8,7 @@ public class Messages { public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command"; 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_EVENT_DISPLAYED_INDEX = "The event index provided is invalid"; public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; } diff --git a/src/main/java/seedu/address/logic/commands/AddECommand.java b/src/main/java/seedu/address/logic/commands/AddECommand.java new file mode 100644 index 000000000000..e1e66d72efcf --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/AddECommand.java @@ -0,0 +1,71 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; +import static seedu.address.logic.parser.CliSyntax.PREFIX_END_TIME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_LABEL; +import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_START_TIME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_VENUE; + +import seedu.address.logic.CommandHistory; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.event.Event; + +/** + * Adds a person to the address book. + */ +public class AddECommand extends Command { + + public static final String COMMAND_WORD = "addE"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds an event to the address book. " + + "Parameters: " + + PREFIX_NAME + "NAME " + + PREFIX_DESCRIPTION + "DESCRIPTION " + + PREFIX_VENUE + "VENUE " + + PREFIX_START_TIME + "START_TIME " + + PREFIX_END_TIME + "END_TIME " + + PREFIX_LABEL + "LABEL\n" + + "Example: " + COMMAND_WORD + " " + + PREFIX_NAME + " " + + PREFIX_DESCRIPTION + "CS2103 project meeting " + + PREFIX_VENUE + "com1 level2 " + + PREFIX_START_TIME + "2019-01-31 14:00:00 " + + PREFIX_END_TIME + "2019-01-31 16:00:00 " + + PREFIX_LABEL + "URGENT"; + + public static final String MESSAGE_SUCCESS = "New event added: %1$s"; + public static final String MESSAGE_DUPLICATE_EVENT = "This event already exists in the address book"; + + private final Event toAdd; + + /** + * Creates an AddECommand to add the specified {@code Event} + */ + public AddECommand(Event event) { + requireNonNull(event); + toAdd = event; + } + + @Override + public CommandResult execute(Model model, CommandHistory history) throws CommandException { + requireNonNull(model); + + if (model.hasEvent(toAdd)) { + throw new CommandException(MESSAGE_DUPLICATE_EVENT); + } + + model.addEvent(toAdd); + model.commitAddressBook(); + return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd)); + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof AddECommand // instanceof handles nulls + && toAdd.equals(((AddECommand) other).toAdd)); + } +} diff --git a/src/main/java/seedu/address/logic/commands/DeleteECommand.java b/src/main/java/seedu/address/logic/commands/DeleteECommand.java new file mode 100644 index 000000000000..4b7ed7aa1715 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/DeleteECommand.java @@ -0,0 +1,56 @@ +package seedu.address.logic.commands; + +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.CommandHistory; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.event.Event; + + +/** + * Deletes an event identified using it's displayed index from the address book. + */ +public class DeleteECommand extends Command { + + public static final String COMMAND_WORD = "deleteE"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Deletes the event identified by the index number used in the displayed event list.\n" + + "Parameters: INDEX (must be a positive integer)\n" + + "Example: " + COMMAND_WORD + " 1"; + + public static final String MESSAGE_DELETE_EVENT_SUCCESS = "Deleted Event: %1$s"; + + private final Index targetIndex; + + public DeleteECommand(Index targetIndex) { + this.targetIndex = targetIndex; + } + + @Override + public CommandResult execute(Model model, CommandHistory history) throws CommandException { + requireNonNull(model); + List lastShownList = model.getFilteredEventList(); + + if (targetIndex.getZeroBased() >= lastShownList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_EVENT_DISPLAYED_INDEX); + } + + Event eventToDelete = lastShownList.get(targetIndex.getZeroBased()); + model.deleteEvent(eventToDelete); + model.commitAddressBook(); + return new CommandResult(String.format(MESSAGE_DELETE_EVENT_SUCCESS, eventToDelete)); + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof DeleteECommand // instanceof handles nulls + && targetIndex.equals(((DeleteECommand) other).targetIndex)); // state check + } +} diff --git a/src/main/java/seedu/address/logic/commands/EditECommand.java b/src/main/java/seedu/address/logic/commands/EditECommand.java new file mode 100644 index 000000000000..70d58713e591 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/EditECommand.java @@ -0,0 +1,254 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; +import static seedu.address.logic.parser.CliSyntax.PREFIX_END_TIME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_LABEL; +import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_START_TIME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_VENUE; +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_EVENTS; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import seedu.address.commons.core.Messages; +import seedu.address.commons.core.index.Index; +import seedu.address.commons.util.CollectionUtil; +import seedu.address.logic.CommandHistory; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.event.DateTime; +import seedu.address.model.event.Description; +import seedu.address.model.event.Event; +import seedu.address.model.event.Label; +import seedu.address.model.event.Name; +import seedu.address.model.event.Venue; +import seedu.address.model.person.Person; + +/** + * Edits the details of an existing event in the address book. + */ +public class EditECommand extends Command { + + public static final String COMMAND_WORD = "editE"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the event identified " + + "by the index number used in the displayed event list. " + + "Existing values will be overwritten by the input values.\n" + + "Parameters: INDEX (must be a positive integer) " + + "[" + PREFIX_NAME + "NAME] " + + "[" + PREFIX_DESCRIPTION + "DESCRIPTION] " + + "[" + PREFIX_VENUE + "VENUE] " + + "[" + PREFIX_START_TIME + "START_TIME] " + + "[" + PREFIX_END_TIME + "END_TIME] " + + "[" + PREFIX_LABEL + "LABEL] \n" + + "Example: " + COMMAND_WORD + " 1 " + + PREFIX_VENUE + "BIZ2 level4 " + + PREFIX_LABEL + "IMPORTANT"; + + public static final String MESSAGE_EDIT_EVENT_SUCCESS = "Edited Event: %1$s"; + public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; + public static final String MESSAGE_DUPLICATE_EVENT = "This event already exists in the address book."; + + private final Index index; + private final EditEventDescriptor editEventDescriptor; + + /** + * @param index of the event in the filtered event list to edit + * @param editEventDescriptor details to edit the event with + */ + public EditECommand(Index index, EditEventDescriptor editEventDescriptor) { + requireNonNull(index); + requireNonNull(editEventDescriptor); + + this.index = index; + this.editEventDescriptor = new EditEventDescriptor(editEventDescriptor); + } + + @Override + public CommandResult execute(Model model, CommandHistory history) throws CommandException { + requireNonNull(model); + List lastShownList = model.getFilteredEventList(); + + if (index.getZeroBased() >= lastShownList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_EVENT_DISPLAYED_INDEX); + } + + Event eventToEdit = lastShownList.get(index.getZeroBased()); + Event editedEvent = createEditedEvent(eventToEdit, editEventDescriptor); + + if (!eventToEdit.isSameEvent(editedEvent) && model.hasEvent(editedEvent)) { + throw new CommandException(MESSAGE_DUPLICATE_EVENT); + } + + model.setEvent(eventToEdit, editedEvent); + model.updateFilteredEventList(PREDICATE_SHOW_ALL_EVENTS); + model.commitAddressBook(); + return new CommandResult(String.format(MESSAGE_EDIT_EVENT_SUCCESS, editedEvent)); + } + + /** + * Creates and returns a {@code Event} with the details of {@code eventToEdit} + * edited with {@code editEventDescriptor}. + */ + private static Event createEditedEvent(Event eventToEdit, EditEventDescriptor editEventDescriptor) { + assert eventToEdit != null; + + Name updatedName = editEventDescriptor.getName().orElse(eventToEdit.getName()); + Description updatedDescription = editEventDescriptor.getDescription().orElse(eventToEdit.getDescription()); + Venue updatedVenue = editEventDescriptor.getVenue().orElse(eventToEdit.getVenue()); + DateTime updatedStartTime = editEventDescriptor.getStartDateTime().orElse(eventToEdit.getStartDateTime()); + DateTime updatedEndTime = editEventDescriptor.getEndDateTime().orElse(eventToEdit.getEndDateTime()); + Label updatedLabel = editEventDescriptor.getLabel().orElse(eventToEdit.getLabel()); + Set updatedPersons = editEventDescriptor.getPersons().orElse(eventToEdit.getPersons()); + + return new Event(updatedName, updatedDescription, updatedVenue, updatedStartTime, updatedEndTime, updatedLabel, + updatedPersons); + } + + @Override + public boolean equals(Object other) { + // short circuit if same object + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof EditECommand)) { + return false; + } + + // state check + EditECommand e = (EditECommand) other; + return index.equals(e.index) + && editEventDescriptor.equals(e.editEventDescriptor); + } + + /** + * Stores the details to edit the event with. Each non-empty field value will replace the + * corresponding field value of the event. + */ + public static class EditEventDescriptor { + private Name name; + private Description description; + private Venue venue; + private DateTime startDateTime; + private DateTime endDateTime; + private Label label; + private Set persons; + + public EditEventDescriptor() {} + + /** + * Copy constructor. + * A defensive copy of {@code tags} is used internally. + */ + public EditEventDescriptor(EditEventDescriptor toCopy) { + setName(toCopy.name); + setDescription(toCopy.description); + setVenue(toCopy.venue); + setStartDateTime(toCopy.startDateTime); + setEndDateTime(toCopy.endDateTime); + setLabel(toCopy.label); + setPersons(toCopy.persons); + } + + /** + * Returns true if at least one field is edited. + */ + public boolean isAnyFieldEdited() { + return CollectionUtil.isAnyNonNull(name, description, venue, startDateTime, endDateTime, label); + } + + public void setName(Name name) { + this.name = name; + } + + public Optional getName() { + return Optional.ofNullable(name); + } + + public void setDescription(Description description) { + this.description = description; + } + + public Optional getDescription() { + return Optional.ofNullable(description); + } + + public void setVenue(Venue venue) { + this.venue = venue; + } + + public Optional getVenue() { + return Optional.ofNullable(venue); + } + + public void setStartDateTime(DateTime startDateTime) { + this.startDateTime = startDateTime; + } + + public Optional getStartDateTime() { + return Optional.ofNullable(startDateTime); + } + + public void setEndDateTime(DateTime endDateTime) { + this.endDateTime = endDateTime; + } + + public Optional getEndDateTime() { + return Optional.ofNullable(endDateTime); + } + + public void setLabel(Label label) { + this.label = label; + } + + public Optional