Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY1819S2#37 from jwl1997/master
Browse files Browse the repository at this point in the history
add, delete, edit and list commands for event list v1.2 week 9
  • Loading branch information
jwl1997 authored Mar 18, 2019
2 parents 4788ce9 + 56c623e commit 72b16cd
Show file tree
Hide file tree
Showing 16 changed files with 876 additions and 3 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 @@ -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!";

}
71 changes: 71 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddECommand.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
56 changes: 56 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteECommand.java
Original file line number Diff line number Diff line change
@@ -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<Event> 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
}
}
254 changes: 254 additions & 0 deletions src/main/java/seedu/address/logic/commands/EditECommand.java
Original file line number Diff line number Diff line change
@@ -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<Event> 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<Person> 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<Person> 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<Name> getName() {
return Optional.ofNullable(name);
}

public void setDescription(Description description) {
this.description = description;
}

public Optional<Description> getDescription() {
return Optional.ofNullable(description);
}

public void setVenue(Venue venue) {
this.venue = venue;
}

public Optional<Venue> getVenue() {
return Optional.ofNullable(venue);
}

public void setStartDateTime(DateTime startDateTime) {
this.startDateTime = startDateTime;
}

public Optional<DateTime> getStartDateTime() {
return Optional.ofNullable(startDateTime);
}

public void setEndDateTime(DateTime endDateTime) {
this.endDateTime = endDateTime;
}

public Optional<DateTime> getEndDateTime() {
return Optional.ofNullable(endDateTime);
}

public void setLabel(Label label) {
this.label = label;
}

public Optional<Label> getLabel() {
return Optional.ofNullable(label);
}

/**
* Sets {@code persons} to this object's {@code persons}.
* A defensive copy of {@code persons} is used internally.
*/
public void setPersons(Set<Person> persons) {
this.persons = (persons != null) ? new HashSet<>(persons) : null;
}

/**
* Returns an unmodifiable person set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code persons} is null.
*/
public Optional<Set<Person>> getPersons() {
return (persons != null) ? Optional.of(Collections.unmodifiableSet(persons)) : Optional.empty();
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EditEventDescriptor)) {
return false;
}

// state check
EditEventDescriptor e = (EditEventDescriptor) other;

return getName().equals(e.getName())
&& getDescription().equals(e.getDescription())
&& getVenue().equals(e.getVenue())
&& getStartDateTime().equals(e.getStartDateTime())
&& getEndDateTime().equals(e.getEndDateTime());
}
}
}
Loading

0 comments on commit 72b16cd

Please sign in to comment.