From 5c141b6c7756f73f9d80a9dd7cef30fa48a371df Mon Sep 17 00:00:00 2001 From: samriddh2145 <156680067+samriddh2145@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:49:19 +0800 Subject: [PATCH 1/2] Implement modified delete command --- .../java/seedu/address/logic/Messages.java | 2 +- .../address/logic/commands/DeleteCommand.java | 92 ++++++++++++--- .../address/logic/commands/EditCommand.java | 2 +- .../logic/parser/DeleteCommandParser.java | 56 +++++++++- .../seedu/address/logic/LogicManagerTest.java | 7 -- .../logic/commands/DeleteCommandTest.java | 105 +++++++++++------- .../logic/commands/EditCommandTest.java | 4 +- .../logic/parser/AddressBookParserTest.java | 6 +- .../logic/parser/DeleteCommandParserTest.java | 10 +- 9 files changed, 210 insertions(+), 74 deletions(-) diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index c6cc92930a0..f5802cf7411 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -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): "; diff --git a/src/main/java/seedu/address/logic/commands/DeleteCommand.java b/src/main/java/seedu/address/logic/commands/DeleteCommand.java index 1135ac19b74..1e880aec91f 100644 --- a/src/main/java/seedu/address/logic/commands/DeleteCommand.java +++ b/src/main/java/seedu/address/logic/commands/DeleteCommand.java @@ -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; @@ -19,16 +23,36 @@ 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 name; + private final Optional phone; + private final Optional 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 name, Optional phone, Optional email) { + this.name = name; + this.phone = phone; + this.email = email; } @Override @@ -36,15 +60,50 @@ public CommandResult execute(Model model) throws CommandException { requireNonNull(model); List lastShownList = model.getFilteredPersonList(); - if (targetIndex.getZeroBased() >= lastShownList.size()) { - throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + // Filter persons based on provided criteria + List 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) { @@ -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(); } } diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 20419e7a15e..20905f3eca1 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -70,7 +70,7 @@ public CommandResult execute(Model model) throws CommandException { List 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()); diff --git a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java index 3527fe76a3e..9dec49798ed 100644 --- a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java @@ -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; @@ -18,12 +22,58 @@ public class DeleteCommandParser implements Parser { */ 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 name = argMultimap.getValue(PREFIX_NAME); + Optional phone = argMultimap.getValue(PREFIX_PHONE); + Optional 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); } } + /** + * 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 + } + } diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index 3565d6f4db4..dc3e9295901 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -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; @@ -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; diff --git a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java index b6f332eabca..394757b1445 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java @@ -1,24 +1,24 @@ package seedu.address.logic.commands; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; -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.ALICE; +import static seedu.address.testutil.TypicalPersons.BENSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import java.util.Optional; + import org.junit.jupiter.api.Test; -import seedu.address.commons.core.index.Index; import seedu.address.logic.Messages; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; import seedu.address.model.person.Person; + /** * Contains integration tests (interaction with the Model) and unit tests for * {@code DeleteCommand}. @@ -28,9 +28,10 @@ public class DeleteCommandTest { 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); + public void execute_validNameUnfilteredList_success() { + Person personToDelete = ALICE; + DeleteCommand deleteCommand = new DeleteCommand(Optional.of(ALICE.getName().toString()), + Optional.empty(), Optional.empty()); String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)); @@ -42,72 +43,94 @@ public void execute_validIndexUnfilteredList_success() { } @Test - public void execute_invalidIndexUnfilteredList_throwsCommandException() { - Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); - DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex); + public void execute_invalidNameUnfilteredList_throwsCommandException() { + DeleteCommand deleteCommand = new DeleteCommand(Optional.of("Invalid Name"), + Optional.empty(), Optional.empty()); - assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DELETED); } @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); + public void execute_validPhoneUnfilteredList_success() { + // Delete person by valid phone number (BENSON) + Person personToDelete = BENSON; + DeleteCommand deleteCommand = new DeleteCommand(Optional.empty(), Optional.of(BENSON.getPhone().toString()), + Optional.empty()); String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)); - Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); expectedModel.deletePerson(personToDelete); - showNoPerson(expectedModel); assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); } @Test - public void execute_invalidIndexFilteredList_throwsCommandException() { - showPersonAtIndex(model, INDEX_FIRST_PERSON); + public void execute_invalidPhoneUnfilteredList_throwsCommandException() { + // Try deleting a person by a phone number that doesn't exist + DeleteCommand deleteCommand = new DeleteCommand(Optional.empty(), Optional.of("99999999"), Optional.empty()); + + assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DELETED); + } - Index outOfBoundIndex = INDEX_SECOND_PERSON; - // ensures that outOfBoundIndex is still in bounds of address book list - assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size()); + @Test + public void execute_validEmailUnfilteredList_success() { + // Delete person by valid email (ALICE) + Person personToDelete = ALICE; + DeleteCommand deleteCommand = new DeleteCommand(Optional.empty(), Optional.empty(), + Optional.of(ALICE.getEmail().toString())); - DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex); + String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, + Messages.format(personToDelete)); - assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedModel.deletePerson(personToDelete); + + assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); + } + + @Test + public void execute_invalidEmailUnfilteredList_throwsCommandException() { + // Try deleting a person by an email that doesn't exist + DeleteCommand deleteCommand = new DeleteCommand(Optional.empty(), Optional.empty(), + Optional.of("nonexistent@example.com")); + + assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DELETED); } @Test public void equals() { - DeleteCommand deleteFirstCommand = new DeleteCommand(INDEX_FIRST_PERSON); - DeleteCommand deleteSecondCommand = new DeleteCommand(INDEX_SECOND_PERSON); + DeleteCommand deleteByNameCommand = new DeleteCommand(Optional.of(ALICE.getName().toString()), + Optional.empty(), Optional.empty()); + DeleteCommand deleteByPhoneCommand = new DeleteCommand(Optional.empty(), + Optional.of(BENSON.getPhone().toString()), Optional.empty()); // same object -> returns true - assertTrue(deleteFirstCommand.equals(deleteFirstCommand)); + assertTrue(deleteByNameCommand.equals(deleteByNameCommand)); // same values -> returns true - DeleteCommand deleteFirstCommandCopy = new DeleteCommand(INDEX_FIRST_PERSON); - assertTrue(deleteFirstCommand.equals(deleteFirstCommandCopy)); + DeleteCommand deleteByNameCommandCopy = new DeleteCommand(Optional.of(ALICE.getName().toString()), + Optional.empty(), Optional.empty()); + assertTrue(deleteByNameCommand.equals(deleteByNameCommandCopy)); // different types -> returns false - assertFalse(deleteFirstCommand.equals(1)); + assertFalse(deleteByNameCommand.equals(1)); // null -> returns false - assertFalse(deleteFirstCommand.equals(null)); + assertFalse(deleteByNameCommand.equals(null)); // different person -> returns false - assertFalse(deleteFirstCommand.equals(deleteSecondCommand)); + assertFalse(deleteByNameCommand.equals(deleteByPhoneCommand)); } - @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()); - } + //@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()); + //} /** * Updates {@code model}'s filtered list to show no one. diff --git a/src/test/java/seedu/address/logic/commands/EditCommandTest.java b/src/test/java/seedu/address/logic/commands/EditCommandTest.java index ed2e05e68e2..6e5621b8a95 100644 --- a/src/test/java/seedu/address/logic/commands/EditCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditCommandTest.java @@ -126,7 +126,7 @@ public void execute_invalidPersonIndexUnfilteredList_failure() { EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build(); EditCommand editCommand = new EditCommand(outOfBoundIndex, descriptor); - assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DELETED); } /** @@ -143,7 +143,7 @@ public void execute_invalidPersonIndexFilteredList_failure() { EditCommand editCommand = new EditCommand(outOfBoundIndex, new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); - assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DELETED); } @Test diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index 606c7cfeab3..5c1831405d7 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -6,9 +6,11 @@ import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND; import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalPersons.ALICE; import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import org.junit.jupiter.api.Test; @@ -49,8 +51,8 @@ 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()); - assertEquals(new DeleteCommand(INDEX_FIRST_PERSON), command); + DeleteCommand.COMMAND_WORD + " n/" + ALICE.getName().toString()); + assertEquals(new DeleteCommand(Optional.of("Alice Pauline"), Optional.empty(), Optional.empty()), command); } @Test diff --git a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java index 6a40e14a649..377d7558adc 100644 --- a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java @@ -3,7 +3,9 @@ import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; 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 java.util.Optional; import org.junit.jupiter.api.Test; @@ -22,7 +24,11 @@ public class DeleteCommandParserTest { @Test public void parse_validArgs_returnsDeleteCommand() { - assertParseSuccess(parser, "1", new DeleteCommand(INDEX_FIRST_PERSON)); + assertParseSuccess(parser, "delete n/Alice Pauline p/94351253 e/alice@example.com", + new DeleteCommand(Optional.of(ALICE.getName().toString()), + Optional.of(ALICE.getPhone().toString()), Optional.of(ALICE.getEmail().toString()))); + assertParseSuccess(parser, "delete n/Alice Pauline", + new DeleteCommand(Optional.of(ALICE.getName().toString()), Optional.empty(), Optional.empty())); } @Test From 155699bbc017826b1f0a44b373515130691ab327 Mon Sep 17 00:00:00 2001 From: samriddh2145 <156680067+samriddh2145@users.noreply.github.com> Date: Thu, 17 Oct 2024 02:17:21 +0800 Subject: [PATCH 2/2] Add additional test cases --- .../logic/parser/DeleteCommandParser.java | 2 +- .../typicalPersonsAddressBook.json | 2 +- .../logic/commands/DeleteCommandTest.java | 33 +++++++++++++++---- .../logic/parser/DeleteCommandParserTest.java | 24 ++++++++++++++ .../address/model/person/PersonTest.java | 1 + .../address/testutil/TypicalPersons.java | 2 +- 6 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java index 9dec49798ed..81d7eefdab6 100644 --- a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java @@ -45,7 +45,7 @@ public DeleteCommand parse(String args) throws ParseException { 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); } } diff --git a/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json index bfd5a34c170..ca841780961 100644 --- a/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json @@ -14,7 +14,7 @@ "tag" : "High Risk" }, { "name" : "Carl Kurz", - "phone" : "95352563", + "phone" : "94351253", "email" : "heinz@example.com", "address" : "wall street", "tag" : "Low Risk" diff --git a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java index 394757b1445..d6b585e2a39 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java @@ -1,5 +1,6 @@ package seedu.address.logic.commands; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; @@ -99,6 +100,15 @@ public void execute_invalidEmailUnfilteredList_throwsCommandException() { assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DELETED); } + @Test + public void execute_multipleMatchingPersons_throwsCommandException() { + // Both ALICE and BENSON have names that match "Alice" (assuming case-insensitive match) + DeleteCommand deleteCommand = new DeleteCommand(Optional.empty(), Optional.of("94351253"), Optional.empty()); + + // Expect CommandException with MESSAGE_MULTIPLE_PERSONS_FOUND + assertCommandFailure(deleteCommand, model, DeleteCommand.MESSAGE_MULTIPLE_PERSONS_FOUND); + } + @Test public void equals() { DeleteCommand deleteByNameCommand = new DeleteCommand(Optional.of(ALICE.getName().toString()), @@ -124,13 +134,22 @@ public void equals() { assertFalse(deleteByNameCommand.equals(deleteByPhoneCommand)); } - //@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()); - //} + @Test + public void toString_validCommand() { + DeleteCommand deleteCommand = new DeleteCommand( + Optional.of("Alice Pauline"), + Optional.of("94351253"), + Optional.of("alice@example.com") + ); + + // Expected output + String expectedString = "seedu.address.logic.commands.DeleteCommand{name=Optional[Alice Pauline]," + + " phone=Optional[94351253], email=Optional[alice@example.com]}"; + + // Assert that the toString output matches the expected string + assertEquals(expectedString, deleteCommand.toString()); + } + /** * Updates {@code model}'s filtered list to show no one. diff --git a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java index 377d7558adc..beb0b121831 100644 --- a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java @@ -31,6 +31,30 @@ public void parse_validArgs_returnsDeleteCommand() { new DeleteCommand(Optional.of(ALICE.getName().toString()), Optional.empty(), Optional.empty())); } + @Test + public void parse_invalidEmail_throwsParseException() { + // Invalid email input + String userInput = "delete n/Alice Pauline p/12345678 e/ @gmail.com"; + + // Expected error message + String expectedMessage = "ERROR: Invalid email format. Please provide a valid email address."; + + assertParseFailure(parser, userInput, expectedMessage); + + } + + @Test + public void parse_invalidPhone_throwsParseException() { + // Simulate an input with an invalid phone number (e.g., less than 8 digits) + String userInput = "delete n/Alice Pauline p/1234"; // Invalid phone number + + // Expected error message when the phone number is invalid + String expectedMessage = "ERROR: Invalid phone number format. Enter a valid 8 digit phone number."; + + // Call the parser and expect a ParseException with the specific message + assertParseFailure(parser, userInput, expectedMessage); + } + @Test public void parse_invalidArgs_throwsParseException() { assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE)); diff --git a/src/test/java/seedu/address/model/person/PersonTest.java b/src/test/java/seedu/address/model/person/PersonTest.java index 19f2babcc87..3af32c5b8f9 100644 --- a/src/test/java/seedu/address/model/person/PersonTest.java +++ b/src/test/java/seedu/address/model/person/PersonTest.java @@ -45,6 +45,7 @@ public void isSamePerson() { assertFalse(BOB.isSamePerson(editedBob)); } + @Test public void equals() { // same values -> returns true diff --git a/src/test/java/seedu/address/testutil/TypicalPersons.java b/src/test/java/seedu/address/testutil/TypicalPersons.java index 91122bf589c..ce083f806c4 100644 --- a/src/test/java/seedu/address/testutil/TypicalPersons.java +++ b/src/test/java/seedu/address/testutil/TypicalPersons.java @@ -31,7 +31,7 @@ public class TypicalPersons { .withAddress("311, Clementi Ave 2, #02-25") .withEmail("johnd@example.com").withPhone("98765432") .withTags("High Risk").build(); - public static final Person CARL = new PersonBuilder().withName("Carl Kurz").withPhone("95352563") + public static final Person CARL = new PersonBuilder().withName("Carl Kurz").withPhone("94351253") .withEmail("heinz@example.com").withAddress("wall street").build(); public static final Person DANIEL = new PersonBuilder().withName("Daniel Meier").withPhone("87652533") .withEmail("cornelia@example.com").withAddress("10th street").withTags("Low Risk").build();