Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update delete contact functionality #32

Merged
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
53ab114
Add factory method for DeleteCommand
CJ-Lee01 Oct 10, 2023
ead20a1
Add subclass for DeleteCommand
CJ-Lee01 Oct 10, 2023
5f132cf
Add tasks in comments
CJ-Lee01 Oct 10, 2023
ed10bab
Add getChildren method for Contact
CJ-Lee01 Oct 10, 2023
e9f6f02
Add addChild method for Contact
CJ-Lee01 Oct 10, 2023
b077f23
Add new exception for illegal operations
CJ-Lee01 Oct 10, 2023
ff7ae08
Add exception thrown when calling addChild
CJ-Lee01 Oct 10, 2023
2805ec6
Add javadocs to delete commands
CJ-Lee01 Oct 12, 2023
1ad1b59
Add javadocs for Contact methods
CJ-Lee01 Oct 12, 2023
5722054
Fix style issues in DeleteCommand
CJ-Lee01 Oct 12, 2023
8ef6769
Add ContactId
CJ-Lee01 Oct 12, 2023
5033786
Add DeleteByIdCommand
CJ-Lee01 Oct 12, 2023
26f4670
Add DeleteByIdWithChildrenCommand
CJ-Lee01 Oct 12, 2023
538c16d
Update javadocs of delete commands
CJ-Lee01 Oct 12, 2023
f87453f
Add select ID for delete command
CJ-Lee01 Oct 12, 2023
4dfabe6
Add functionality to delete child contacts
CJ-Lee01 Oct 12, 2023
5a14f7b
Merge branch 'master' into branch-delete-contacts
CJ-Lee01 Oct 12, 2023
8a35390
Add hashcode function to ContactId
CJ-Lee01 Oct 12, 2023
f66a7ff
Add regex to ContactId
CJ-Lee01 Oct 12, 2023
79db31c
Add functionality to get contact by id
CJ-Lee01 Oct 16, 2023
93add45
Add javadocs to AddressBook
CJ-Lee01 Oct 16, 2023
6e99018
Add functionality to get contact by id in Model
CJ-Lee01 Oct 16, 2023
ee662e9
Implement delete by id commands
CJ-Lee01 Oct 16, 2023
ea0c0d3
Fix formatting and spelling errors
CJ-Lee01 Oct 16, 2023
a9b73c1
Fix checkstyle issues
CJ-Lee01 Oct 16, 2023
83126be
Implement new model method in test
CJ-Lee01 Oct 16, 2023
ad8194f
Nuke delete by id commands
CJ-Lee01 Oct 17, 2023
6b30a6e
Add no such contact exception message
CJ-Lee01 Oct 17, 2023
74baba9
Implement delete by id in DeleteCommand classes
CJ-Lee01 Oct 17, 2023
59cc772
Update javadocs for DeleteCommand classes
CJ-Lee01 Oct 17, 2023
70d7ca0
Add parseContactId method
CJ-Lee01 Oct 17, 2023
7fbe9a7
Merge branch 'master' into branch-delete-contacts
CJ-Lee01 Oct 17, 2023
fc915f9
Change ContactId to Id
CJ-Lee01 Oct 17, 2023
667451d
Add recursive flag to CliSyntax
CJ-Lee01 Oct 17, 2023
91d7f6f
Update DeleteCommandParser to delete id
CJ-Lee01 Oct 17, 2023
df0eeea
Fix wrong recursive delete message
CJ-Lee01 Oct 17, 2023
39aa1a6
Merge branch 'master' into branch-delete-contacts
CJ-Lee01 Oct 17, 2023
8dc9d58
Remove unncessary comment
CJ-Lee01 Oct 17, 2023
2e72dc4
Fix name for static field in Contact
CJ-Lee01 Oct 17, 2023
91974fe
Add todo for Organization
CJ-Lee01 Oct 17, 2023
3503f6e
Merge branch 'master' into branch-delete-contacts
CJ-Lee01 Oct 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
34 changes: 34 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteByIdCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package seedu.address.logic.commands;

import seedu.address.model.Model;
import seedu.address.model.person.Contact;
import seedu.address.model.person.ContactId;

/**
* Deletes a contact identified using its ID from the addressbook.
*/
public class DeleteByIdCommand extends DeleteCommand {

public static final String NO_SUCH_CONTACT_WITH_ID = "No such contact with id: %s";
private final ContactId contactId;

/**
* @param id of the contact to be deleted.
*/
public DeleteByIdCommand(ContactId id) {
// TODO: Might want to change this since null is dangerous
super(null);
this.contactId = id;
}

@Override
public CommandResult execute(Model model) {
// TODO: Implement the get function based on id for the model
Contact c = model.getContactById(contactId);
if (c == null) {
return new CommandResult(String.format(NO_SUCH_CONTACT_WITH_ID, contactId));
}
model.deleteContact(c);
return new CommandResult(String.format(MESSAGE_DELETE_CONTACT_SUCCESS, c));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.DeleteByIdCommand.NO_SUCH_CONTACT_WITH_ID;

import java.util.Arrays;

import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.Contact;
import seedu.address.model.person.ContactId;

/**
* Deletes a contact identified with its id and also deletes its child contacts.
*/
public class DeleteByIdWithChildrenCommand extends DeleteCommand {

private final ContactId contactId;
/**
* @param id of the contact to be deleted along with its child contacts.
*/
public DeleteByIdWithChildrenCommand(ContactId id) {
super(null);
this.contactId = id;
}

@Override
public CommandResult execute(Model model) {
Contact contactToDelete = model.getContactById(contactId);
if (contactToDelete == null) {
return new CommandResult(String.format(NO_SUCH_CONTACT_WITH_ID, contactId));
}
Contact[] childContacts = contactToDelete.getChildren();
model.deleteContact(contactToDelete);
Arrays.stream(childContacts).forEach(contact -> {
model.deleteContact(contactToDelete);
});
return new CommandResult(String.format(
DeleteWithChildrenCommand.MESSAGE_DELETE_CONTACT_SUCCESS,
Messages.format(contactToDelete),
Arrays.stream(childContacts)
.map(c -> Messages.format(c) + "\n")
.reduce((c1, c2) -> c1 + c2)
));
}
}
31 changes: 30 additions & 1 deletion src/main/java/seedu/address/logic/commands/DeleteCommand.java
CJ-Lee01 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Contact;
import seedu.address.model.person.ContactId;

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

Expand All @@ -31,6 +32,34 @@ public DeleteCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

/**
* Creates an executable DeleteCommand based on whether to delete recursively.
*
* @param targetIndex of the contact to delete in the current list
* @param shouldDeleteChildren specifies if child contacts should be deleted
*/
public static DeleteCommand selectIndex(Index targetIndex, boolean shouldDeleteChildren) {
// TODO: Add documentation to DG
if (shouldDeleteChildren) {
return new DeleteWithChildrenCommand(targetIndex);
}
return new DeleteCommand(targetIndex);
}

/**
* Creates an executable DeleteCommand based on whether to delete recursively.
*
* @param contactId of the contact to delete in the current list
* @param shouldDeleteChildren specifies if child contacts should be deleted
*/
public static DeleteCommand selectId(ContactId contactId, boolean shouldDeleteChildren) {
// TODO: Add documentation to DG
if (shouldDeleteChildren) {
return new DeleteByIdWithChildrenCommand(contactId);
}
return new DeleteByIdCommand(contactId);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Expand Down
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 java.util.List;

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;

/**
* Deletes a contact identified with its displayed index 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%s";

private final Index targetIndex;

/**
* @param targetIndex of the contact to be deleted in the current contact list
*/
public DeleteWithChildrenCommand(Index targetIndex) {
// TODO add documentation in DG
super(targetIndex);
this.targetIndex = targetIndex;
}

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

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_CONTACT_DISPLAYED_INDEX);
}

Contact contactToDelete = lastShownList.get(targetIndex.getZeroBased());
Contact[] childContacts = contactToDelete.getChildren();
model.deleteContact(contactToDelete);
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)
));
}
}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javafx.collections.ObservableList;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.person.Contact;
import seedu.address.model.person.ContactId;
import seedu.address.model.person.UniqueContactList;

/**
Expand Down Expand Up @@ -94,6 +95,21 @@ public void removeContact(Contact key) {
contacts.remove(key);
}

/**
* Gives a contact which id matches the given id.
* Gives null if a contact with such id does not exist.
* Given id must not be null.
*/
public Contact getContactById(ContactId id) {
requireNonNull(id);
for (Contact c: contacts) {
if (id.equals(c.getContactId())) {
return c;
}
}
return null;
}

//// util methods

@Override
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.model.person.Contact;
import seedu.address.model.person.ContactId;

/**
* The API of the Model component.
Expand Down Expand Up @@ -76,6 +77,13 @@ public interface Model {
*/
void setContact(Contact target, Contact editedContact);

/**
* Gives a contact which matches the given id.
* Gives null if no such contact is found.
* Given id must not be null.
*/
Contact getContactById(ContactId id);

/** Returns an unmodifiable view of the filtered contact list */
ObservableList<Contact> getFilteredContactList();

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.person.Contact;
import seedu.address.model.person.ContactId;

/**
* Represents the in-memory model of the address book data.
Expand Down Expand Up @@ -111,6 +112,11 @@ public void setContact(Contact target, Contact editedContact) {
addressBook.setContact(target, editedContact);
}

@Override
public Contact getContactById(ContactId id) {
return addressBook.getContactById(id);
}

//=========== Filtered Contact List Accessors =============================================================

/**
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/seedu/address/model/person/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Objects;
import java.util.Set;

import seedu.address.commons.exceptions.IllegalOperationException;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.tag.Tag;

Expand All @@ -16,7 +17,10 @@
*/
public class Contact {

private static String illegalOperationMessage = "Contact cannot have child contacts";
CJ-Lee01 marked this conversation as resolved.
Show resolved Hide resolved

// Identity fields
private final ContactId contactId = null;
private final Name name;
private final Phone phone;
private final Email email;
Expand All @@ -37,6 +41,10 @@ public Contact(Name name, Phone phone, Email email, Address address, Set<Tag> ta
this.tags.addAll(tags);
}

public ContactId getContactId() {
return contactId;
}

public Name getName() {
return name;
}
Expand Down Expand Up @@ -74,6 +82,25 @@ public boolean isSameContact(Contact otherContact) {
&& otherContact.getName().equals(getName());
}

/**
* Gives the array of contacts that are linked under this contact.
*/
public Contact[] getChildren() {
// default return value
// TODO add to DG
return new Contact[]{};
}
CJ-Lee01 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Adds a child contact under this contact.
* @throws IllegalOperationException if this contact cannot accept child contacts
*/
public void addChild(Contact childContact) throws IllegalOperationException {
// Should throw exception if the type of contact cannot have child contacts.
// TODO add to DG, do JavaDocs
throw new IllegalOperationException(illegalOperationMessage);
}

/**
* Returns true if both contacts have the same identity and data fields.
* This defines a stronger notion of equality between two contacts.
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/seedu/address/model/person/ContactId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package seedu.address.model.person;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
* Represents a Contact's ID in the address book.
* Guarantees: immutable, is valid as declared in {@link #isValidId(String)}
*/
public class ContactId {

public static final String MESSAGE_CONSTRAINTS =
"Contact ID should not be blank";

// TODO: I need help with this part
public static final String VALIDATION_REGEX = "^[a-zA-Z0-9\\-_]+$";

public final String contactId;

/**
* Constructs a {@code ContactId}.
*
* @param contactId A valid contact id.
*/
public ContactId(String contactId) {
requireNonNull(contactId);
checkArgument(isValidId(contactId), MESSAGE_CONSTRAINTS);
this.contactId = contactId;
}

/**
* Returns true if a given string is a valid contact ID.
*/
public static boolean isValidId(String test) {
return test.matches(VALIDATION_REGEX);
}

@Override
public String toString() {
return contactId;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

//instanceof handles nulls
if (!(other instanceof ContactId)) {
return false;
}

ContactId otherId = (ContactId) other;
return contactId.equals(otherId.contactId);
}

@Override
public int hashCode() {
return contactId.hashCode();
}
}
Loading