Skip to content

Commit

Permalink
Merge pull request #52 from jayjay19630/feat/create-command-abstractions
Browse files Browse the repository at this point in the history
Create Command Abstractions
  • Loading branch information
zaidansani authored Oct 9, 2024
2 parents 49aaa29 + 9317742 commit 6fce161
Show file tree
Hide file tree
Showing 25 changed files with 427 additions and 200 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePref
/**
* Formats the {@code person} for display to the user.
*/
public static String format(Person person) {
public static String formatPerson(Person person) {
final StringBuilder builder = new StringBuilder();
builder.append(person.getName())
.append("; Phone: ")
Expand Down
82 changes: 33 additions & 49 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,62 @@
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.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;

/**
* Adds a person to the address book.
* Adds an entity (person or appointment) to the address book.
*/
public class AddCommand extends Command {
public abstract class AddCommand extends Command {

public static final String COMMAND_WORD = "add person";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
public static final String COMMAND_WORD = "add";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Adds an entity (person or appointment) to the address book. "
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ "ENTITY_TYPE "
+ "ENTITY_ARGUMENTS..."
+ "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 AddCommand to add the specified {@code Person}
*/
public AddCommand(Person person) {
requireNonNull(person);
toAdd = person;
}

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

if (model.hasPerson(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
if (alreadyExists(model)) {
throw new CommandException(getDuplicateEntityMessage());
}

model.addPerson(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
addEntity(model);
return new CommandResult(String.format(getSuccessMessage(), formatEntity()));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
/*
* Checks if the entity being added to model already exists.
*/
protected abstract boolean alreadyExists(Model model);

// instanceof handles nulls
if (!(other instanceof AddCommand)) {
return false;
}
/*
* Adds the entity to the model.
*/
protected abstract void addEntity(Model model);

AddCommand otherAddCommand = (AddCommand) other;
return toAdd.equals(otherAddCommand.toAdd);
}
/*
* Returns success message to display upon adding entity.
*/
protected abstract String getSuccessMessage();

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", toAdd)
.toString();
}
/*
* Returns the message to display when there is a duplicate.
*/
protected abstract String getDuplicateEntityMessage();

/**
* Formats the entity for displaying in the success message.
*/
protected abstract String formatEntity();
}
95 changes: 95 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddPersonCommand.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public class ClearCommand extends Command {

public static final String COMMAND_WORD = "clear person";
public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "Address book has been cleared!";


Expand Down
70 changes: 70 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
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);
}
45 changes: 29 additions & 16 deletions src/main/java/seedu/address/logic/commands/DeletePersonCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,48 @@
/**
* Deletes a person identified using it's displayed index from the address book.
*/
public class DeletePersonCommand extends Command {

public static final String COMMAND_WORD = "delete person";
public class DeletePersonCommand extends DeleteCommand {

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

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";

private final Index targetIndex;

public DeletePersonCommand(Index targetIndex) {
this.targetIndex = targetIndex;
super(targetIndex);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();
protected List<Person> getFilteredList(Model model) {
return model.getFilteredPersonList();
}

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
@Override
protected void deleteEntity(Model model, Object entity) throws CommandException {
requireNonNull(entity);

assert entity instanceof Person;

model.deletePerson((Person) entity);
}

@Override
protected String getSuccessMessage() {
return MESSAGE_DELETE_PERSON_SUCCESS;
}

@Override
protected String getInvalidIndexMessage() {
return Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
}

@Override
protected String formatEntity(Object entity) {
assert entity instanceof Person;

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
return Messages.formatPerson((Person) entity);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
public class EditCommand extends Command {

public static final String COMMAND_WORD = "edit person";
public static final String COMMAND_WORD = "edit";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified "
+ "by the index number used in the displayed person list. "
Expand Down Expand Up @@ -85,7 +85,7 @@ public CommandResult execute(Model model) throws CommandException {

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.formatPerson(editedPerson)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class FindCommand extends Command {

public static final String COMMAND_WORD = "find person";
public static final String COMMAND_WORD = "find";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of "
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public class ListCommand extends Command {

public static final String COMMAND_WORD = "list person";
public static final String COMMAND_WORD = "list";

public static final String MESSAGE_SUCCESS = "Listed all persons";

Expand Down
Loading

0 comments on commit 6fce161

Please sign in to comment.