forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from CJ-Lee01/branch-delete-contacts
Update delete contact functionality
- Loading branch information
Showing
12 changed files
with
250 additions
and
15 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/java/seedu/address/commons/exceptions/IllegalOperationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package seedu.address.commons.exceptions; | ||
|
||
/** | ||
* Exception thrown when attempting to make illegal. | ||
*/ | ||
public class IllegalOperationException extends Exception { | ||
/** | ||
* @param message that informs the user that it has attempted an illegal operation. | ||
*/ | ||
public IllegalOperationException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* @param message that informs the user that it has attempted an illegal operation. | ||
* @param cause of the main exception. | ||
*/ | ||
public IllegalOperationException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/seedu/address/logic/commands/DeleteWithChildrenCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.Arrays; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Contact; | ||
import seedu.address.model.person.Id; | ||
|
||
/** | ||
* Deletes a contact and also deletes its child contacts. | ||
*/ | ||
public class DeleteWithChildrenCommand extends DeleteCommand { | ||
|
||
public static final String MESSAGE_DELETE_CONTACT_SUCCESS = DeleteCommand.MESSAGE_DELETE_CONTACT_SUCCESS + " with" | ||
+ ":\n%2$s"; | ||
|
||
|
||
/** | ||
* @param targetIndex of the contact to be deleted in the current contact list | ||
*/ | ||
public DeleteWithChildrenCommand(Index targetIndex) { | ||
// TODO add documentation in DG | ||
super(targetIndex); | ||
} | ||
|
||
public DeleteWithChildrenCommand(Id targetId) { | ||
super(targetId); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
Contact contactToDelete = super.getContact(model); | ||
super.execute(model); | ||
// At this point if the contact is null, the superclass would have thrown exception. | ||
// Superclass would have also deleted the contact from the list. | ||
assert contactToDelete != null; | ||
Contact[] childContacts = contactToDelete.getChildren(); | ||
Arrays.stream(childContacts).forEach(contact -> { | ||
model.deleteContact(contactToDelete); | ||
}); | ||
return new CommandResult(String.format( | ||
MESSAGE_DELETE_CONTACT_SUCCESS, | ||
Messages.format(contactToDelete), | ||
Arrays.stream(childContacts) | ||
.map(c -> Messages.format(c) + "\n") | ||
.reduce((c1, c2) -> c1 + c2) | ||
.orElse("No other contacts found") // I can't find a better method. | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.