forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from jayjay19630/feat/create-command-abstractions
Create Command Abstractions
- Loading branch information
Showing
25 changed files
with
427 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/seedu/address/logic/commands/AddPersonCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Adds a person to the address book. | ||
*/ | ||
public class AddPersonCommand extends AddCommand { | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + " " + "person" | ||
+ ": Adds a person to the address book. " | ||
+ "Parameters: " | ||
+ PREFIX_NAME + "NAME " | ||
+ PREFIX_PHONE + "PHONE " | ||
+ PREFIX_EMAIL + "EMAIL " | ||
+ PREFIX_ADDRESS + "ADDRESS " | ||
+ "[" + PREFIX_TAG + "TAG]...\n" | ||
+ "Example: " + COMMAND_WORD + " " + "person" + " " | ||
+ PREFIX_NAME + "John Doe " | ||
+ PREFIX_PHONE + "98765432 " | ||
+ PREFIX_EMAIL + "johnd@example.com " | ||
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 " | ||
+ PREFIX_TAG + "friends " | ||
+ PREFIX_TAG + "owesMoney"; | ||
|
||
public static final String MESSAGE_SUCCESS = "New person added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book"; | ||
|
||
private final Person toAdd; | ||
|
||
/** | ||
* Creates an AddPersonCommand to add the specified {@code Person} | ||
*/ | ||
public AddPersonCommand(Person person) { | ||
requireNonNull(person); | ||
toAdd = person; | ||
} | ||
|
||
@Override | ||
protected boolean alreadyExists(Model model) { | ||
return model.hasPerson(toAdd); | ||
} | ||
|
||
@Override | ||
protected void addEntity(Model model) { | ||
model.addPerson(toAdd); | ||
} | ||
|
||
@Override | ||
protected String getSuccessMessage() { | ||
return MESSAGE_SUCCESS; | ||
} | ||
|
||
@Override | ||
protected String getDuplicateEntityMessage() { | ||
return MESSAGE_DUPLICATE_PERSON; | ||
} | ||
|
||
@Override | ||
protected String formatEntity() { | ||
return Messages.formatPerson(toAdd); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AddPersonCommand)) { | ||
return false; | ||
} | ||
|
||
AddPersonCommand otherAddPersonCommand = (AddPersonCommand) other; | ||
return toAdd.equals(otherAddPersonCommand.toAdd); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("toAdd", toAdd) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/seedu/address/logic/commands/DeleteCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
|
||
/** | ||
* Deletes an entity identified using it's displayed index from the address book. | ||
*/ | ||
public abstract class DeleteCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "delete"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Deletes the entity identified by the index number used in the displayed list.\n" | ||
+ "Parameters: INDEX (must be a positive integer)\n" | ||
+ "Example: " + COMMAND_WORD + " " + "person" + " " + "1"; | ||
|
||
protected final Index targetIndex; | ||
|
||
/** | ||
* @param targetIndex Index of entity to be deleted. | ||
*/ | ||
public DeleteCommand(Index targetIndex) { | ||
requireNonNull(targetIndex); | ||
this.targetIndex = targetIndex; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<?> lastShownList = getFilteredList(model); | ||
|
||
if (targetIndex.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(getInvalidIndexMessage()); | ||
} | ||
|
||
Object entityToDelete = lastShownList.get(targetIndex.getZeroBased()); | ||
deleteEntity(model, entityToDelete); | ||
return new CommandResult(String.format(getSuccessMessage(), formatEntity(entityToDelete))); | ||
} | ||
|
||
/** | ||
* Gets the filtered list of entities in the model. | ||
*/ | ||
protected abstract List<?> getFilteredList(Model model); | ||
|
||
/** | ||
* Deletes the entity from the model. | ||
*/ | ||
protected abstract void deleteEntity(Model model, Object entity) throws CommandException; | ||
|
||
/** | ||
* Returns the success message to display upon deleting entity. | ||
*/ | ||
protected abstract String getSuccessMessage(); | ||
|
||
/** | ||
* Returns the invalid index message when the index is out of bounds. | ||
*/ | ||
protected abstract String getInvalidIndexMessage(); | ||
|
||
/** | ||
* Formats the entity for displaying in the success message. | ||
*/ | ||
protected abstract String formatEntity(Object entity); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.