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 #59 from tanshiyu1999/branch-Edit
Update Edit Function
- Loading branch information
Showing
12 changed files
with
194 additions
and
29 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -33,7 +33,12 @@ | |
import seedu.address.model.contact.Email; | ||
import seedu.address.model.contact.Id; | ||
import seedu.address.model.contact.Name; | ||
import seedu.address.model.contact.Organization; | ||
import seedu.address.model.contact.Phone; | ||
import seedu.address.model.contact.Position; | ||
import seedu.address.model.contact.Recruiter; | ||
import seedu.address.model.contact.Status; | ||
import seedu.address.model.contact.Type; | ||
import seedu.address.model.contact.Url; | ||
import seedu.address.model.tag.Tag; | ||
|
||
|
@@ -54,25 +59,53 @@ public class EditCommand extends Command { | |
AutocompleteDataSet.anyNumberOf(FLAG_TAG) | ||
); | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the contact identified " | ||
+ "by the index number used in the displayed contact list. " | ||
+ "Existing values will be overwritten by the input values.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
public static final String MESSAGE_ORGANIZATION_USAGE = "Edits an organization.\n" | ||
+ "Parameters: INDEX/ID " | ||
+ "[" + FLAG_NAME + " NAME] " | ||
+ "[" + FLAG_ID + " ID] " | ||
+ "[" + FLAG_PHONE + " PHONE] " | ||
+ "[" + FLAG_EMAIL + " EMAIL] " | ||
+ "[" + FLAG_URL + " URL] " | ||
+ "[" + FLAG_ADDRESS + " ADDRESS] " | ||
+ "[" + FLAG_STATUS + " STATUS] " | ||
+ "[" + FLAG_POSITION + " POSITION] " | ||
+ "[" + FLAG_TAG + " TAG]...\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ FLAG_PHONE + " 91234567 " | ||
+ FLAG_EMAIL + " [email protected]"; | ||
+ FLAG_STATUS + " Applied"; | ||
|
||
public static final String MESSAGE_RECRUITER_USAGE = "Edits a recruiter.\n" | ||
+ "Parameters: INDEX/ID " | ||
+ "[" + FLAG_NAME + " NAME] " | ||
+ "[" + FLAG_ID + " ID] " | ||
+ "[" + FLAG_PHONE + " PHONE] " | ||
+ "[" + FLAG_EMAIL + " EMAIL] " | ||
+ "[" + FLAG_URL + " URL] " | ||
+ "[" + FLAG_ADDRESS + " ADDRESS] " | ||
+ "[" + FLAG_ORGANIZATION_ID + " OID] " | ||
+ "[" + FLAG_TAG + " TAG]...\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ FLAG_PHONE + " 91234567 " | ||
+ FLAG_EMAIL + " [email protected]"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Edits the details of the contact of the class type Organization or Recruiter," | ||
+ " identified by its index in the displayed contact list or its id." | ||
+ " Note that existing values will be overwritten by the input values." | ||
+ " The input format varies depending on the class:\n\n" | ||
+ MESSAGE_ORGANIZATION_USAGE + "\n\n" | ||
+ MESSAGE_RECRUITER_USAGE; | ||
|
||
public static final String MESSAGE_EDIT_CONTACT_SUCCESS = "Edited Contact: %1$s"; | ||
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; | ||
public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists in the address book."; | ||
public static final String MESSAGE_INVALID_ORGANIZATION = | ||
"The organization id you supplied does not match any organization in the address book."; | ||
|
||
private final Index index; | ||
private final EditContactDescriptor editContactDescriptor; | ||
private Index index; | ||
|
||
private Id targetId = null; | ||
private EditContactDescriptor editContactDescriptor; | ||
|
||
/** | ||
* @param index of the contact in the filtered contact list to edit | ||
|
@@ -86,32 +119,54 @@ public EditCommand(Index index, EditContactDescriptor editContactDescriptor) { | |
this.editContactDescriptor = new EditContactDescriptor(editContactDescriptor); | ||
} | ||
|
||
/** | ||
* @param targetId of the contact to be editted | ||
*/ | ||
public EditCommand(Id targetId, EditContactDescriptor editContactDescriptor) { | ||
this.targetId = targetId; | ||
this.editContactDescriptor = new EditContactDescriptor(editContactDescriptor); | ||
} | ||
|
||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
if (this.targetId != null) { | ||
Contact contactToEdit = model.getContactById(targetId); | ||
return getCommandResult(model, contactToEdit); | ||
} | ||
|
||
List<Contact> lastShownList = model.getFilteredContactList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_CONTACT_DISPLAYED_INDEX); | ||
} | ||
|
||
Contact contactToEdit = lastShownList.get(index.getZeroBased()); | ||
Contact editedContact = createEditedContact(contactToEdit, editContactDescriptor); | ||
return getCommandResult(model, contactToEdit); | ||
} | ||
|
||
|
||
public CommandResult getCommandResult(Model model, Contact contactToEdit) throws CommandException { | ||
Contact editedContact = createEditedContact(model, contactToEdit, editContactDescriptor); | ||
if (!contactToEdit.isSameContact(editedContact) && model.hasContact(editedContact)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_CONTACT); | ||
} | ||
|
||
model.setContact(contactToEdit, editedContact); | ||
model.updateFilteredContactList(PREDICATE_SHOW_ALL_CONTACTS); | ||
return new CommandResult(String.format(MESSAGE_EDIT_CONTACT_SUCCESS, Messages.format(editedContact))); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
/** | ||
* Creates and returns a {@code Contact} with the details of {@code contactToEdit} | ||
* edited with {@code editContactDescriptor}. | ||
*/ | ||
private static Contact createEditedContact(Contact contactToEdit, EditContactDescriptor editContactDescriptor) { | ||
private static Contact createEditedContact(Model model, Contact contactToEdit, | ||
EditContactDescriptor editContactDescriptor) throws CommandException { | ||
assert contactToEdit != null; | ||
|
||
Name updatedName = editContactDescriptor.getName() | ||
|
@@ -129,6 +184,32 @@ private static Contact createEditedContact(Contact contactToEdit, EditContactDes | |
Set<Tag> updatedTags = editContactDescriptor.getTags() | ||
.orElse(contactToEdit.getTags()); | ||
|
||
|
||
if (contactToEdit.getType() == Type.ORGANIZATION) { | ||
Status updatedStatus = editContactDescriptor.getStatus() | ||
.orElse(((Organization) contactToEdit).getStatus().orElse(null)); | ||
|
||
Position updatedPosition = editContactDescriptor.getPosition() | ||
.orElse(((Organization) contactToEdit).getPosition().orElse(null)); | ||
return new Organization(updatedName, updatedId, updatedPhone, updatedEmail, updatedUrl, | ||
updatedAddress, updatedTags, updatedStatus, updatedPosition, null); | ||
} else if (contactToEdit.getType() == Type.RECRUITER) { | ||
Optional<Id> updatedOid = editContactDescriptor | ||
.getOrganizationId() | ||
.or(() -> ((Recruiter) contactToEdit).getOrganizationId()); | ||
|
||
Organization updatedOrganization = (Organization) updatedOid.map(model::getContactById) | ||
.filter(c -> c.getType() == Type.ORGANIZATION) | ||
.orElse(null); | ||
|
||
if (updatedOid.isPresent() && updatedOrganization == null) { | ||
throw new CommandException(MESSAGE_INVALID_ORGANIZATION); | ||
} | ||
|
||
return new Recruiter(updatedName, updatedId, updatedPhone, updatedEmail, updatedUrl, | ||
updatedAddress, updatedTags, updatedOrganization); | ||
} | ||
|
||
return new Contact(updatedName, updatedId, updatedPhone, updatedEmail, updatedUrl, updatedAddress, updatedTags); | ||
} | ||
|
||
|
@@ -168,6 +249,10 @@ public static class EditContactDescriptor { | |
private Url url; | ||
private Address address; | ||
private Set<Tag> tags; | ||
private Status status; | ||
private Position position; | ||
|
||
private Id oid; | ||
|
||
public EditContactDescriptor() {} | ||
|
||
|
@@ -183,19 +268,30 @@ public EditContactDescriptor(EditContactDescriptor toCopy) { | |
setUrl(toCopy.url); | ||
setAddress(toCopy.address); | ||
setTags(toCopy.tags); | ||
setStatus(toCopy.status); | ||
setPosition(toCopy.position); | ||
setOid(toCopy.oid); | ||
} | ||
|
||
/** | ||
* Returns true if at least one field is edited. | ||
*/ | ||
public boolean isAnyFieldEdited() { | ||
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags); | ||
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags, id, url, status, position, oid); | ||
} | ||
|
||
public void setName(Name name) { | ||
this.name = name; | ||
} | ||
|
||
public Optional<Id> getOrganizationId() { | ||
return Optional.ofNullable(oid); | ||
} | ||
|
||
public void setOid(Id id) { | ||
this.oid = id; | ||
} | ||
|
||
public Optional<Name> getName() { | ||
return Optional.ofNullable(name); | ||
} | ||
|
@@ -236,6 +332,22 @@ public void setAddress(Address address) { | |
this.address = address; | ||
} | ||
|
||
public void setStatus(Status status) { | ||
this.status = status; | ||
} | ||
|
||
public void setPosition(Position position) { | ||
this.position = position; | ||
} | ||
|
||
public Optional<Status> getStatus() { | ||
return Optional.ofNullable(status); | ||
} | ||
|
||
public Optional<Position> getPosition() { | ||
return Optional.ofNullable(position); | ||
} | ||
|
||
public Optional<Address> getAddress() { | ||
return Optional.ofNullable(address); | ||
} | ||
|
@@ -284,6 +396,9 @@ public String toString() { | |
.add("email", email) | ||
.add("address", address) | ||
.add("tags", tags) | ||
.add("url", url) | ||
.add("status", status) | ||
.add("position", position) | ||
.toString(); | ||
} | ||
} | ||
|
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
4 changes: 2 additions & 2 deletions
4
src/test/data/JsonAddressBookStorageTest/invalidAndValidPersonAddressBook.json
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
{ | ||
"persons": [ { | ||
"name": "Valid Person", | ||
"id": "18dfa74c-2229-4f4f-99a5-336c25e6783e", | ||
"id": "a8dfa74c-2229-4f4f-99a5-336c25e6783e", | ||
"phone": "9482424", | ||
"email": "[email protected]", | ||
"address": "4th street" | ||
}, { | ||
"name": "Person With Invalid Phone Field", | ||
"id" : "3a6e0af8-5092-47c0-baf7-18d6dc535823", | ||
"id" : "ba6e0af8-5092-47c0-baf7-18d6dc535823", | ||
"phone": "948asdf2424", | ||
"email": "[email protected]", | ||
"address": "4th street" | ||
|
2 changes: 1 addition & 1 deletion
2
src/test/data/JsonAddressBookStorageTest/invalidPersonAddressBook.json
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"persons": [ { | ||
"name": "Person with invalid name field: Ha!ns Mu@ster", | ||
"id" : "3a6e0af8-5092-47c0-baf7-18d6dc535823", | ||
"id" : "ca6e0af8-5092-47c0-baf7-18d6dc535823", | ||
"phone": "9482424", | ||
"email": "[email protected]", | ||
"address": "4th street" | ||
|
Oops, something went wrong.