Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103-F09-1#118 from somethingfishyfishy…
Browse files Browse the repository at this point in the history
…/branch-DeleteThroughName

Update Delete Feature through specifying name
  • Loading branch information
wuzengfu authored Oct 23, 2024
2 parents b8df070 + 427137d commit fc97c95
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 18 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 @@ -16,10 +16,13 @@ public class Messages {
+ "Only the following will be accepted as the first word of the command:\n"
+ "add, edit, delete, find, list, help, clear, exit";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_NAME_FIELD_MISSING = "Invalid command format! MISSING 'n/' \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
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): ";
public static final String MESSAGE_PERSON_NOT_IN_ADDRESS_BOOK =
"This person is not in address book, please use Full Name";
public static final String MESSAGE_NO_PARAMETER_FOUND = "Please enter something for me to search";
public static final String MESSAGE_EMPTY_PREFIX_FIELD = "Fields cannot be empty";

Expand Down
51 changes: 48 additions & 3 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,56 @@
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;

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

public static final String COMMAND_WORD = "delete";

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

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";

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";
private static final int invalidTargetIndex = -1;

private final Index targetIndex;
private Index targetIndex;
private final Name targetName;

/**
* Constructor method that takes in an Index Object
* @param targetIndex index from the addressbook whereby is expected to be deleted.
*/
public DeleteCommand(Index targetIndex) {
this.targetIndex = targetIndex;
this.targetName = null;
}

/**
* Constructor method that takes in a Name Object
* @param name the name which is expected to be deleted
*/
public DeleteCommand(Name name) {
this.targetIndex = null;
this.targetName = name;
}

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

if (targetIndex == null) {
setTargetIndex(lastShownList);
}

assert(targetIndex != null);

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Expand All @@ -45,6 +68,17 @@ public CommandResult execute(Model model) throws CommandException {
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
}

private void setTargetIndex(List<Person> lastShownList) throws CommandException {
int temp = lastShownList.stream()
.filter(person -> person.getName().equalsIgnoreCase(targetName))
.map(lastShownList::indexOf)
.reduce(invalidTargetIndex, (x, y) -> y);
if (temp == invalidTargetIndex) {
throw new CommandException(Messages.MESSAGE_PERSON_NOT_IN_ADDRESS_BOOK);
}
this.targetIndex = Index.fromZeroBased(temp);
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -57,6 +91,17 @@ public boolean equals(Object other) {
}

DeleteCommand otherDeleteCommand = (DeleteCommand) other;

if (targetIndex == null) {
if (otherDeleteCommand.targetIndex == null) {
if (targetName == null) {
return otherDeleteCommand.targetName == null;
}
return targetName.equals(otherDeleteCommand.targetName);
}
return false;
}

return targetIndex.equals(otherDeleteCommand.targetIndex);
}

Expand Down
26 changes: 21 additions & 5 deletions src/main/java/seedu/address/logic/parser/DeleteCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package seedu.address.logic.parser;

// import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_NAME_FIELD_MISSING;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

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;

/**
* Parses input arguments and creates a new DeleteCommand object
Expand All @@ -17,13 +21,25 @@ 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 {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME);

if (argMultimap.getValue(PREFIX_NAME).isEmpty()) {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
} catch (ParseException ex) {
throw new ParseException(
String.format(MESSAGE_NAME_FIELD_MISSING, DeleteCommand.MESSAGE_USAGE), ex);
}
}

String str = argMultimap.getValue(PREFIX_NAME).get();
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
} catch (ParseException pe) {
Name name = ParserUtil.parseName(str);
return new DeleteCommand(name);
} catch (Exception ex) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ public boolean equals(Object other) {
}

ContainsKeywordsPredicate otherContainsKeywordsPredicate = (ContainsKeywordsPredicate) other;
boolean IsEqualNameKeywords = nameKeywords.equals(otherContainsKeywordsPredicate.nameKeywords);
boolean IsEqualTelegramHandleKeywords =
boolean isEqualNameKeywords = nameKeywords.equals(otherContainsKeywordsPredicate.nameKeywords);
boolean isEqualTelegramHandleKeywords =
telegramHandleKeywords.equals(otherContainsKeywordsPredicate.telegramHandleKeywords);
boolean IsEqualEmailKeywords = emailKeywords.equals(otherContainsKeywordsPredicate.emailKeywords);
boolean IsEqualStudentStatusKeywords =
boolean isEqualEmailKeywords = emailKeywords.equals(otherContainsKeywordsPredicate.emailKeywords);
boolean isEqualStudentStatusKeywords =
studentStatusKeywords.equals(otherContainsKeywordsPredicate.studentStatusKeywords);
boolean IsEqualRoleKeywords = roleKeywords.equals(otherContainsKeywordsPredicate.roleKeywords);
boolean IsEqualNicknameKeywords = nicknameKeywords.equals(otherContainsKeywordsPredicate.nicknameKeywords);
return IsEqualNameKeywords && IsEqualTelegramHandleKeywords && IsEqualEmailKeywords
&& IsEqualStudentStatusKeywords && IsEqualRoleKeywords && IsEqualNicknameKeywords;
boolean isEqualRoleKeywords = roleKeywords.equals(otherContainsKeywordsPredicate.roleKeywords);
boolean isEqualNicknameKeywords = nicknameKeywords.equals(otherContainsKeywordsPredicate.nicknameKeywords);
return isEqualNameKeywords && isEqualTelegramHandleKeywords && isEqualEmailKeywords
&& isEqualStudentStatusKeywords && isEqualRoleKeywords && isEqualNicknameKeywords;
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/model/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public boolean equals(Object other) {
return fullName.equals(otherName.fullName);
}

/**
* Identifies if the name is the same ignoring case sensitivity
* @param other - the other Name in question
* @return true or false
*/
public boolean equalsIgnoreCase(Name other) {
if (other == null) {
return false;
}
return fullName.equalsIgnoreCase(other.fullName);
}

@Override
public int hashCode() {
return fullName.hashCode();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
// import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_NAME_FIELD_MISSING;
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;
Expand All @@ -27,6 +28,6 @@ public void parse_validArgs_returnsDeleteCommand() {

@Test
public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
assertParseFailure(parser, "a", String.format(MESSAGE_NAME_FIELD_MISSING, DeleteCommand.MESSAGE_USAGE));
}
}

0 comments on commit fc97c95

Please sign in to comment.