Skip to content

Commit

Permalink
Merge pull request #117 from mannggoo/fix-edit-command
Browse files Browse the repository at this point in the history
Fix edit command
  • Loading branch information
sianghwee authored Oct 16, 2019
2 parents 718d77c + ee9db41 commit 6ab6642
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.commons.core.item.Reminder;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.ShowCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.AddressBookParser;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -119,7 +120,8 @@ public CommandResult execute(String commandText) throws CommandException, ParseE

if (commandResult.isUndo()) {
model.setToCurrState();
} else if (commandResult.isSwitchViews()) {
} else if (command instanceof ShowCommand) {
//} else if (commandResult.isSwitchViews()) {
/*switching view is not counted as a change in state,
hence this block is left blank*/
} else {
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Add an Item to the item list.
*/
public class AddCommand extends Command {
public abstract class AddCommand extends Command {

public static final String COMMAND_WORD = "add";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a Task to the Task List. "
Expand All @@ -26,9 +26,9 @@ public class AddCommand extends Command {
+ "<Optional> " + PREFIX_TAG + "Tag ";

public static final String MESSAGE_SUCCESS = "New Item added: %1$s";
public static final String MESSAGE_DUPLICATE_ITEM = "This item already exists in the List";
public static final String MESSAGE_DUPLICATE_ITEM = "This item already exists.";

private final Item toAdd;
protected final Item toAdd;

/**
* Creates an AddCommand to add the specified {@code Item}
Expand All @@ -38,19 +38,7 @@ public AddCommand(Item item) {
toAdd = item;
}

@Override
public CommandResult execute(ItemModel model) throws CommandException {
requireNonNull(model);

/*
if (model.hasItem(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}
*/

model.addItem(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}
public abstract CommandResult execute(ItemModel model) throws CommandException;

@Override
public boolean equals(Object other) {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddEventCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.core.item.Item;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.ItemModel;

/**
* Adds an Event to the item model.
*/
public class AddEventCommand extends AddCommand {

public static final String SHOW_EVENT_VIEW = "E";
public static final String MESSAGE_SUCCESS = "New Event added: %1$s";

public AddEventCommand(Item item) {
super(item);
}

@Override
public CommandResult execute(ItemModel model) throws CommandException {
requireNonNull(model);

// Check if item already exists, else, add it to the model.
if (model.hasItem(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_ITEM);
} else {
model.addItem(toAdd);
}

// Notify Ui to change the view the that of the newly added item.
try {
model.setVisualList(SHOW_EVENT_VIEW);
} catch (Exception e) {
// should not enter here as itemType is definitely valid.
}

//return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd), SHOW_EVENT_VIEW);
}
}
42 changes: 42 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddReminderCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.core.item.Item;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.ItemModel;

/**
* Adds a Reminder to the item model.
*/
public class AddReminderCommand extends AddCommand {

public static final String SHOW_REMINDER_VIEW = "R";
public static final String MESSAGE_SUCCESS = "New Reminder added: %1$s";

public AddReminderCommand(Item item) {
super(item);
}

@Override
public CommandResult execute(ItemModel model) throws CommandException {
requireNonNull(model);

// Check if item already exists, else, add it to the model.
if (model.hasItem(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_ITEM);
} else {
model.addItem(toAdd);
}

// Notify Ui to change the view the that of the newly added item.
try {
model.setVisualList(SHOW_REMINDER_VIEW);
} catch (Exception e) {
// should not enter here as itemType is definitely valid.
}

//return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd), SHOW_REMINDER_VIEW);
}
}
42 changes: 42 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddTaskCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.core.item.Item;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.ItemModel;

/**
* Adds a Task to the item model.
*/
public class AddTaskCommand extends AddCommand {

public static final String SHOW_TASK_VIEW = "T";
public static final String MESSAGE_SUCCESS = "New Task added: %1$s";

public AddTaskCommand(Item item) {
super(item);
}

@Override
public CommandResult execute(ItemModel model) throws CommandException {
requireNonNull(model);

// Check if item already exists, else, add it to the model.
if (model.hasItem(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_ITEM);
} else {
model.addItem(toAdd);
}

// Notify Ui to change the view the that of the newly added item.
try {
model.setVisualList(SHOW_TASK_VIEW);
} catch (Exception e) {
// should not enter here as itemType is definitely valid.
}

//return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd), SHOW_TASK_VIEW);
}
}
14 changes: 10 additions & 4 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,17 @@ private static Item createEditedItem(Item itemToEdit, EditItemDescriptor editIte
Set<Tag> updatedTags = editItemDescriptor.getTags().orElse(itemToEdit.getTags());

if (lastShownList instanceof TaskList) {
// Change the Priority of this Task. If no priority is given, change back to the default MEDIUM.
updatedTask.get().changePriority(editItemDescriptor.getPriority().orElse(Priority.MEDIUM));
// Change the Priority of this Task. If no priority is given, use the priority of the old item.
updatedTask = Optional.of(updatedTask.get()
.changePriority(editItemDescriptor
.getPriority()
.orElse(itemToEdit.getTask().get().getPriority())));
} else if (lastShownList instanceof EventList) {
// Change the Priority of this Event. If no priority is given, change back to the default MEDIUM.
updatedEvent.get().changePriority(editItemDescriptor.getPriority().orElse(Priority.MEDIUM));
// Change the Priority of this Event. If no priority is given, use the priority of the old item.
updatedEvent = Optional.of(updatedEvent.get()
.changePriority(editItemDescriptor
.getPriority()
.orElse(itemToEdit.getEvent().get().getPriority())));
}

ItemBuilder itemBuilder = new ItemBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.commons.core.item.Priority;
import seedu.address.commons.core.item.Reminder;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddEventCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

Expand Down Expand Up @@ -67,7 +68,8 @@ public AddCommand parse(String desc, String args) throws ParseException {
throw new ParseException(e.getMessage());
}

return new AddCommand(newItem);
AddCommand addCommand = new AddEventCommand(newItem);
return addCommand;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.commons.core.item.ItemDescription;
import seedu.address.commons.core.item.Reminder;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddReminderCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

Expand All @@ -40,7 +41,6 @@ public AddCommand parse(String desc, String args) throws ParseException {
ItemDescription description = ParserUtil.parseDescription(desc);
// Reminder must be present.
Reminder itemReminder = ParserUtil.parseReminder(argMultimap.getValue(PREFIX_REMINDER).get()).get();
//Optional<Priority> priority = ParserUtil.parsePriority(argMultimap.getValue(PREFIX_PRIORITY).orElse(null));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

ItemBuilder itemBuilder = new ItemBuilder();
Expand All @@ -55,7 +55,8 @@ public AddCommand parse(String desc, String args) throws ParseException {
throw new ParseException(e.getMessage());
}

return new AddCommand(newItem);
AddCommand addCommand = new AddReminderCommand(newItem);
return addCommand;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import seedu.address.commons.core.item.Reminder;
import seedu.address.commons.core.item.Task;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddTaskCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

Expand Down Expand Up @@ -59,6 +60,7 @@ public AddCommand parse(String desc, String args) throws ParseException {
throw new ParseException(e.getMessage());
}

return new AddCommand(newItem);
AddCommand addCommand = new AddTaskCommand(newItem);
return addCommand;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private static List<PrefixPosition> findPrefixPositions(String argsString, Prefi
* {@code fromIndex} = 0, this method returns 5.
*/
private static int findPrefixPosition(String argsString, String prefix, int fromIndex) {
int prefixIndex = argsString.indexOf(" " + prefix, fromIndex);
int prefixIndex = argsString.indexOf(" " + prefix + " ", fromIndex);
return prefixIndex == -1 ? -1
: prefixIndex + 1; // +1 as offset for whitespace
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public EditCommand parse(String description, String args) throws ParseException
ParserUtil.parseDescription(
argMultimap.getValue(PREFIX_REMINDER_DESCRIPTION).get()));
}
if (argMultimap.getValue(PREFIX_DATETIME).isPresent()) {
editItemDescriptor.setEvent(
ParserUtil.parseDateTime(
argMultimap.getValue(PREFIX_DATETIME).get()).get());
}
if (argMultimap.getValue(PREFIX_REMINDER).isPresent()) {
editItemDescriptor.setReminder(
ParserUtil.parseReminder(
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/model/ItemModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public interface ItemModel {

public void sort();

public boolean hasItem(Item item);

public void addToSeparateList(Item item);

public void setState(ElisaState state);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/seedu/address/model/ItemModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,15 @@ public void sort() {
this.visualList = visualList.sort();
}

/**
* Checks if the item storage already contains this item.
* @param item to check
* @return true if the item storage contains this item, false otherwise
*/
public boolean hasItem(Item item) {
return itemStorage.contains(item);
}

/**
* Enable and disable the priority mode
* @return a boolean value. If true, means priority mode is on, else returns false.
Expand Down Expand Up @@ -478,5 +487,4 @@ public Item markComplete(int index) throws IllegalListException {

return item;
}

}

0 comments on commit 6ab6642

Please sign in to comment.