Skip to content

Commit

Permalink
Merge pull request #70 from samriddh2145/implement-delete
Browse files Browse the repository at this point in the history
Implement modified delete command
  • Loading branch information
Nihirraa authored Oct 17, 2024
2 parents 4ce338a + 7ac000a commit 5113c85
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 73 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 @@ -14,7 +14,7 @@ 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_PERSON_DELETED = "No matching person found. Please check the details.";
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
92 changes: 77 additions & 15 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
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 java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

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;
Expand All @@ -19,32 +23,87 @@ public class DeleteCommand extends Command {
public static final String COMMAND_WORD = "delete";

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 name, phone, or email.\n"
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL (atleast one) \n"
+ "Example: " + COMMAND_WORD + " n/John Doe";

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

private final Index targetIndex;

public DeleteCommand(Index targetIndex) {
this.targetIndex = targetIndex;
public static final String MESSAGE_MULTIPLE_PERSONS_FOUND = "Multiple patients with the same details found.";
public static final String MESSAGE_NO_PERSON_FOUND = "No matching person found. Please check the details.";


private final Optional<String> name;
private final Optional<String> phone;
private final Optional<String> email;

/**
* Constructs a {@code DeleteCommand} with the specified parameters.
*
* @param name the name of the person to delete, wrapped in an {@code Optional}.
* If the name is not provided, this should be {@code Optional.empty()}.
* @param phone the phone number of the person to delete, wrapped in an {@code Optional}.
* If the phone number is not provided, this should be {@code Optional.empty()}.
* @param email the email address of the person to delete, wrapped in an {@code Optional}.
* If the email is not provided, this should be {@code Optional.empty()}.
*/
public DeleteCommand(Optional<String> name, Optional<String> phone, Optional<String> email) {
this.name = name;
this.phone = phone;
this.email = email;
}

@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);
// Filter persons based on provided criteria
List<Person> matchingPersons = lastShownList.stream()
.filter(person -> matches(person))
.collect(Collectors.toList());

// Handle different cases of matches
if (matchingPersons.isEmpty()) {
throw new CommandException(MESSAGE_NO_PERSON_FOUND);
}

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
if (matchingPersons.size() > 1) {
throw new CommandException(MESSAGE_MULTIPLE_PERSONS_FOUND);
}

// Delete the single matching person
Person personToDelete = matchingPersons.get(0);
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
}

/**
* Checks if a person matches the criteria provided in the delete command.
*/
private boolean matches(Person person) {
boolean matches = true;

// If name is provided, check if it matches
if (name.isPresent()) {
matches &= person.getName().fullName.equalsIgnoreCase(name.get());
}

// If phone is provided, check if it matches
if (phone.isPresent()) {
matches &= person.getPhone().value.equals(phone.get());
}

// If email is provided, check if it matches
if (email.isPresent()) {
matches &= person.getEmail().value.equalsIgnoreCase(email.get());
}

return matches;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -57,13 +116,16 @@ public boolean equals(Object other) {
}

DeleteCommand otherDeleteCommand = (DeleteCommand) other;
return targetIndex.equals(otherDeleteCommand.targetIndex);
return name.equals(otherDeleteCommand.name)
&& phone.equals(otherDeleteCommand.phone)
&& email.equals(otherDeleteCommand.email);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", targetIndex)
return new ToStringBuilder(this).add("name", name)
.add("phone", phone)
.add("email", email)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public CommandResult execute(Model model) throws CommandException {
List<Person> lastShownList = model.getFilteredPersonList();

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

Person personToEdit = lastShownList.get(index.getZeroBased());
Expand Down
58 changes: 54 additions & 4 deletions src/main/java/seedu/address/logic/parser/DeleteCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
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 java.util.Optional;

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

Expand All @@ -18,12 +22,58 @@ public class DeleteCommandParser implements Parser<DeleteCommand> {
*/
public DeleteCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL);

if (!areAnyPrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}

Optional<String> name = argMultimap.getValue(PREFIX_NAME);
Optional<String> phone = argMultimap.getValue(PREFIX_PHONE);
Optional<String> email = argMultimap.getValue(PREFIX_EMAIL);

// Validate the phone number format if present
if (phone.isPresent() && !isValidPhone(phone.get())) {
throw new ParseException("ERROR: Invalid phone number format. Enter a valid 8 digit phone number.");
}

// Validate the email format if present
if (email.isPresent() && !isValidEmail(email.get())) {
throw new ParseException("ERROR: Invalid email format. Please provide a valid email address.");
}

return new DeleteCommand(name, phone, email);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
pe.getMessage(), pe);
}
}

/**
* Returns true if at least one of the specified prefixes is present in the
* given ArgumentMultimap.
*/
private boolean areAnyPrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
for (Prefix prefix : prefixes) {
if (argumentMultimap.getValue(prefix).isPresent()) {
return true;
}
}
return false;
}

/**
* A method to validate phone number format.
*/
private boolean isValidPhone(String phone) {
return phone.matches("\\d{8}"); // Validates an 8-digit phone number
}

/**
* A method to validate email format.
*/
private boolean isValidEmail(String email) {
return email.matches("^[\\w-\\.]+@[\\w-]+\\.[a-z]{2,3}$"); // Basic email validation
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"tag" : "High Risk"
}, {
"name" : "Carl Kurz",
"phone" : "95352563",
"phone" : "94351253",
"email" : "[email protected]",
"address" : "wall street",
"tag" : "Low Risk"
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package seedu.address.logic;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.logic.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.commands.CommandTestUtil.ADDRESS_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.EMAIL_DESC_AMY;
Expand Down Expand Up @@ -59,12 +58,6 @@ public void execute_invalidCommandFormat_throwsParseException() {
assertParseException(invalidCommand, MESSAGE_UNKNOWN_COMMAND);
}

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

@Test
public void execute_validCommand_success() throws Exception {
String listCommand = ViewCommand.COMMAND_WORD;
Expand Down
Loading

0 comments on commit 5113c85

Please sign in to comment.