Skip to content

Commit

Permalink
add delete_client delete_lead command
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxunn committed Oct 14, 2023
1 parent 69379ac commit 5aa612a
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 43 deletions.
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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_LEAD_DISPLAYED = "The person provided is not a lead";
public static final String MESSAGE_INVALID_CLIENT_DISPLAYED = "The person provided is not a client";

public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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.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;
import seedu.address.model.tag.Tag;

/**
* Deletes a person identified using it's displayed index from the address book.
*/
public class DeleteClientCommand extends Command {

public static final String COMMAND_WORD = "delete_client";

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

public static final String MESSAGE_DELETE_CLIENT_SUCCESS = "Deleted Client: %1$s";

private final Index targetIndex;

public DeleteClientCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

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

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
if (personToDelete.getTags().contains(new Tag("Client"))) {
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_CLIENT_SUCCESS, Messages.format(personToDelete)));
}
throw new CommandException(Messages.MESSAGE_INVALID_CLIENT_DISPLAYED);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

DeleteClientCommand otherDeleteCommand = (DeleteClientCommand) other;
return targetIndex.equals(otherDeleteCommand.targetIndex);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", targetIndex)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

/**
* Deletes a person identified using it's displayed index from the address book.
*/
public class DeleteCommand extends Command {
public class DeleteLeadCommand extends Command {

public static final String COMMAND_WORD = "delete";
public static final String COMMAND_WORD = "delete_lead";

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

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

private final Index targetIndex;

public DeleteCommand(Index targetIndex) {
public DeleteLeadCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

Expand All @@ -41,8 +41,11 @@ public CommandResult execute(Model model) throws CommandException {
}

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
if (personToDelete.getTags().contains(new Tag("Lead"))) {
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_LEAD_SUCCESS, Messages.format(personToDelete)));
}
throw new CommandException(Messages.MESSAGE_INVALID_LEAD_DISPLAYED);
}

@Override
Expand All @@ -52,11 +55,11 @@ public boolean equals(Object other) {
}

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

DeleteCommand otherDeleteCommand = (DeleteCommand) other;
DeleteLeadCommand otherDeleteCommand = (DeleteLeadCommand) other;
return targetIndex.equals(otherDeleteCommand.targetIndex);
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeleteClientCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.DeleteLeadCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -59,8 +60,11 @@ public Command parseCommand(String userInput) throws ParseException {
case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);

case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(arguments);
case DeleteClientCommand.COMMAND_WORD:
return new DeleteClientCommandParser().parse(arguments);

case DeleteLeadCommand.COMMAND_WORD:
return new DeleteLeadCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteClientCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteCommand object
*/
public class DeleteClientCommandParser implements Parser<DeleteClientCommand> {

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns a DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteClientCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteClientCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteClientCommand.MESSAGE_USAGE), pe);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeleteLeadCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteCommand object
*/
public class DeleteCommandParser implements Parser<DeleteCommand> {
public class DeleteLeadCommandParser implements Parser<DeleteLeadCommand> {

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns a DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteCommand parse(String args) throws ParseException {
public DeleteLeadCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
return new DeleteLeadCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteLeadCommand.MESSAGE_USAGE), pe);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
* Contains integration tests (interaction with the Model) and unit tests for
* {@code DeleteCommand}.
*/
public class DeleteCommandTest {
public class DeleteClientCommandTest {

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_validIndexUnfilteredList_success() {
Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteClientCommand deleteCommand = new DeleteClientCommand(INDEX_FIRST_PERSON);

String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS,
String expectedMessage = String.format(DeleteClientCommand.MESSAGE_DELETE_CLIENT_SUCCESS,
Messages.format(personToDelete));

ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
Expand All @@ -44,19 +44,19 @@ public void execute_validIndexUnfilteredList_success() {
@Test
public void execute_invalidIndexUnfilteredList_throwsCommandException() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1);
DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex);
DeleteClientCommand deleteClientCommand = new DeleteClientCommand(outOfBoundIndex);

assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandFailure(deleteClientCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
public void execute_validIndexFilteredList_success() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);

Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteClientCommand deleteCommand = new DeleteClientCommand(INDEX_FIRST_PERSON);

String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS,
String expectedMessage = String.format(DeleteClientCommand.MESSAGE_DELETE_CLIENT_SUCCESS,
Messages.format(personToDelete));

Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
Expand All @@ -74,21 +74,21 @@ public void execute_invalidIndexFilteredList_throwsCommandException() {
// ensures that outOfBoundIndex is still in bounds of address book list
assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size());

DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex);
DeleteClientCommand deleteClientCommand = new DeleteClientCommand(outOfBoundIndex);

assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandFailure(deleteClientCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
public void equals() {
DeleteCommand deleteFirstCommand = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteCommand deleteSecondCommand = new DeleteCommand(INDEX_SECOND_PERSON);
DeleteClientCommand deleteFirstCommand = new DeleteClientCommand(INDEX_FIRST_PERSON);
DeleteClientCommand deleteSecondCommand = new DeleteClientCommand(INDEX_SECOND_PERSON);

// same object -> returns true
assertTrue(deleteFirstCommand.equals(deleteFirstCommand));

// same values -> returns true
DeleteCommand deleteFirstCommandCopy = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteClientCommand deleteFirstCommandCopy = new DeleteClientCommand(INDEX_FIRST_PERSON);
assertTrue(deleteFirstCommand.equals(deleteFirstCommandCopy));

// different types -> returns false
Expand All @@ -104,9 +104,9 @@ public void equals() {
@Test
public void toStringMethod() {
Index targetIndex = Index.fromOneBased(1);
DeleteCommand deleteCommand = new DeleteCommand(targetIndex);
String expected = DeleteCommand.class.getCanonicalName() + "{targetIndex=" + targetIndex + "}";
assertEquals(expected, deleteCommand.toString());
DeleteClientCommand deleteClientCommand = new DeleteClientCommand(targetIndex);
String expected = DeleteClientCommand.class.getCanonicalName() + "{targetIndex=" + targetIndex + "}";
assertEquals(expected, deleteClientCommand.toString());
}

/**
Expand Down
Loading

0 comments on commit 5aa612a

Please sign in to comment.