Skip to content

Commit

Permalink
Merge pull request #69 from Tetrerox/branch-delete-staff
Browse files Browse the repository at this point in the history
Delete staff details
  • Loading branch information
Gabau authored Oct 10, 2021
2 parents 7203080 + ae5fe45 commit 3efd667
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 15 deletions.
127 changes: 118 additions & 9 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,121 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Role;
import seedu.address.model.person.Status;

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

public static final String COMMAND_WORD = "delete";
public static final int INVALID_INDEX = -1;

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";
+ ": Deletes the person identified by the index number or name used in the displayed person list.\n"
+ "Parameters: INDEX (must be a positive integer) OR NAME but not both\n"
+ "Examples: " + COMMAND_WORD + " " + PREFIX_INDEX + "1\n"
+ COMMAND_WORD + " " + PREFIX_NAME + "Alex Yeoh";

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";
public static final String MESSAGE_DELETE_PEOPLE_SUCCESS = "Deleted these people:\n\n";

private final Index targetIndex;
private Index targetIndex = null;
private Name name = null;
private Role role = null;
private Status status = null;

/**
* Constructs delete command using a targetIndex.
*
* @param targetIndex The index of a person to be deleted
*/
public DeleteCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

/**
* Constructs delete command using a name.
*
* @param name The name of a person to be deleted
*/
public DeleteCommand(Name name) {
this.name = name;
}

/**
* Constructs delete command using a role.
*
* @param role The role of the people to be deleted
*/
public DeleteCommand(Role role) {
this.role = role;
}

/**
* Constructs delete command using a status.
*
* @param status The status of the people to be deleted
*/
public DeleteCommand(Status status) {
this.status = status;
}

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

if (role != null) {
return executeBasedOnRole(model, staffs);
} else if (status != null) {
return executeBasedOnStatus(model, staffs);
} else if (targetIndex != null) {
return executeBasedOnTargetIndex(model, lastShownList);
} else {
return executeBasedOnName(model, lastShownList);
}
}

private CommandResult executeBasedOnRole(Model model, List<Person> staffs) {
StringBuilder deletedPeople = new StringBuilder(MESSAGE_DELETE_PEOPLE_SUCCESS);
for (Person staff : staffs) {
if (staff.getRole() == role) {
model.deletePerson(staff);
deletedPeople.append(staff).append("\n\n");
}
}
return new CommandResult(deletedPeople.toString());
}

private CommandResult executeBasedOnStatus(Model model, List<Person> staffs) {
StringBuilder deletedPeople = new StringBuilder(MESSAGE_DELETE_PEOPLE_SUCCESS);
for (Person staff : staffs) {
if (staff.getStatus() == status) {
model.deletePerson(staff);
deletedPeople.append(staff).append("\n\n");
}
}
return new CommandResult(deletedPeople.toString());
}

private CommandResult executeBasedOnTargetIndex(Model model, List<Person> lastShownList)
throws CommandException {
if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Expand All @@ -44,10 +125,38 @@ public CommandResult execute(Model model) throws CommandException {
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, staffToDelete));
}

private CommandResult executeBasedOnName(Model model, List<Person> lastShownList)
throws CommandException {
int index = getIndexByName(name, lastShownList);
if (index == INVALID_INDEX) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_SEARCHED);
}

Person staffToDelete = lastShownList.get(index);
model.deletePerson(staffToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, staffToDelete));
}


private int getIndexByName(Name name, List<Person> lastShownList) {
for (int i = 0; i < lastShownList.size(); i++) {
if (lastShownList.get(i).getName().equals(name)) {
return i;
}
}
return INVALID_INDEX;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeleteCommand // instanceof handles nulls
&& targetIndex.equals(((DeleteCommand) other).targetIndex)); // state check
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DeleteCommand that = (DeleteCommand) o;
return Objects.equals(targetIndex, that.targetIndex) && Objects.equals(name, that.name)
&& role == that.role && status == that.status;
}
}
45 changes: 43 additions & 2 deletions src/main/java/seedu/address/logic/parser/DeleteCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ROLE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STATUS;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Name;
import seedu.address.model.person.Role;
import seedu.address.model.person.Status;

/**
* Parses input arguments and creates a new DeleteCommand object
Expand All @@ -18,13 +26,46 @@ public class DeleteCommandParser implements Parser<DeleteCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_INDEX, PREFIX_NAME, PREFIX_ROLE, PREFIX_STATUS);

if (!exactlyOneAcceptedPrefix(argMultimap)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
if (argMultimap.getValue(PREFIX_INDEX).isPresent()) {
Index index = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_INDEX).get());
return new DeleteCommand(index);
} else if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
return new DeleteCommand(name);
} else if (argMultimap.getValue(PREFIX_ROLE).isPresent()) {
Role role = ParserUtil.parseRole(argMultimap.getValue(PREFIX_ROLE).get());
return new DeleteCommand(role);
} else if (argMultimap.getValue(PREFIX_STATUS).isPresent()) {
Status status = ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).get());
return new DeleteCommand(status);
} else {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
}
}

private boolean exactlyOneAcceptedPrefix(ArgumentMultimap argMultimap) {
Prefix[] prefixes = {PREFIX_INDEX, PREFIX_NAME, PREFIX_ROLE, PREFIX_STATUS};
boolean isTrueOnlyOnce = false;
for (Prefix prefix : prefixes) {
if (argMultimap.getValue(prefix).isPresent()) {
if (isTrueOnlyOnce) {
return false;
}
isTrueOnlyOnce = true;
}
}
return isTrueOnlyOnce;
}
}
2 changes: 1 addition & 1 deletion src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void execute_invalidCommandFormat_throwsParseException() {

@Test
public void execute_commandExecutionError_throwsCommandException() {
String deleteCommand = "delete 9";
String deleteCommand = "delete i/9";
assertCommandException(deleteCommand, MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/seedu/address/logic/commands/DeleteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.GEORGE;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.Test;
Expand All @@ -16,6 +17,7 @@
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;

/**
Expand Down Expand Up @@ -76,6 +78,16 @@ public void execute_invalidIndexFilteredList_throwsCommandException() {
assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
public void execute_nameNotFound_throwsCommandException() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);
Name outOfBoundName = GEORGE.getName();

DeleteCommand deleteCommand = new DeleteCommand(outOfBoundName);

assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_SEARCHED);
}

@Test
public void equals() {
DeleteCommand deleteFirstCommand = new DeleteCommand(INDEX_FIRST_PERSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.parser.CliSyntax.EDIT_PREFIX_INDEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;

Expand Down Expand Up @@ -50,7 +51,7 @@ public void parseCommand_clear() throws Exception {
@Test
public void parseCommand_delete() throws Exception {
DeleteCommand command = (DeleteCommand) parser.parseCommand(
DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_PERSON.getOneBased());
DeleteCommand.COMMAND_WORD + " " + PREFIX_INDEX + INDEX_FIRST_PERSON.getOneBased());
assertEquals(new DeleteCommand(INDEX_FIRST_PERSON), command);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalPersons.ALICE;

import org.junit.jupiter.api.Test;

Expand All @@ -21,8 +22,13 @@ public class DeleteCommandParserTest {
private DeleteCommandParser parser = new DeleteCommandParser();

@Test
public void parse_validArgs_returnsDeleteCommand() {
assertParseSuccess(parser, "1", new DeleteCommand(INDEX_FIRST_PERSON));
public void parse_validIndex_returnsDeleteCommand() {
assertParseSuccess(parser, " i/1", new DeleteCommand(INDEX_FIRST_PERSON));
}

@Test
public void parse_validName_returnsDeleteCommand() {
assertParseSuccess(parser, " n/Alice Pauline", new DeleteCommand(ALICE.getName()));
}

@Test
Expand Down

0 comments on commit 3efd667

Please sign in to comment.