forked from nus-cs2103-AY2021S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
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 nus-cs2103-AY2021S1#90 from gloon99/branch-Add-Mee…
…ting Branch add meeting
- Loading branch information
Showing
48 changed files
with
1,469 additions
and
158 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
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
159 changes: 159 additions & 0 deletions
159
src/main/java/seedu/address/logic/commands/AddLabelCommand.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,159 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Name; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Edits the details of an existing person in the address book. | ||
*/ | ||
public class AddLabelCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "label add"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a label to the person specified.\n" | ||
+ "Parameters: NAME (must be name of person existing in ModDuke) " | ||
+ "[" + PREFIX_TAG + "TAG]...\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ "Roy " | ||
+ PREFIX_TAG + "classmate"; | ||
|
||
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Labelled Person: %1$s"; | ||
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; | ||
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book."; | ||
|
||
private final Name targetName; | ||
private final LabelPersonDescriptor labelPersonDescriptor; | ||
|
||
/** | ||
* @param targetName of the person in the filtered person list to label | ||
*/ | ||
public AddLabelCommand(Name targetName, LabelPersonDescriptor labelPersonDescriptor) { | ||
requireNonNull(targetName); | ||
requireNonNull(labelPersonDescriptor); | ||
|
||
this.targetName = targetName; | ||
this.labelPersonDescriptor = new LabelPersonDescriptor(labelPersonDescriptor); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
boolean isValidContact = model.hasPersonName(targetName); | ||
|
||
if (!isValidContact) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED); | ||
} | ||
|
||
List<Person> filteredList = model.getFilteredPersonList().stream() | ||
.filter(person -> person.isSameName(targetName)).collect(Collectors.toList()); | ||
Person personToLabel = filteredList.get(0); | ||
|
||
Person labelledPerson = createLabelledPerson(personToLabel, labelPersonDescriptor); | ||
|
||
if (!personToLabel.isSamePerson(labelledPerson) && model.hasPerson(labelledPerson)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_PERSON); | ||
} | ||
|
||
model.setPerson(personToLabel, labelledPerson); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, labelledPerson)); | ||
} | ||
|
||
/** | ||
* Creates and returns a {@code Person} with the details of {@code personToEdit} | ||
* edited with {@code editPersonDescriptor}. | ||
*/ | ||
private static Person createLabelledPerson(Person personToLabel, LabelPersonDescriptor labelPersonDescriptor) { | ||
assert personToLabel != null; | ||
|
||
Set<Tag> updatedTags = labelPersonDescriptor.getTags().orElse(personToLabel.getTags()); | ||
|
||
return new Person(personToLabel.getName(), personToLabel.getPhone(), personToLabel.getEmail(), updatedTags); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AddLabelCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
AddLabelCommand e = (AddLabelCommand) other; | ||
return targetName.equals(e.targetName) | ||
&& labelPersonDescriptor.equals(e.labelPersonDescriptor); | ||
} | ||
|
||
/** | ||
* Stores the details to edit the person with. Each non-empty field value will replace the | ||
* corresponding field value of the person. | ||
*/ | ||
public static class LabelPersonDescriptor { | ||
private Set<Tag> tags; | ||
|
||
public LabelPersonDescriptor() {} | ||
|
||
/** | ||
* Copy constructor. | ||
* A defensive copy of {@code tags} is used internally. | ||
*/ | ||
public LabelPersonDescriptor(LabelPersonDescriptor toCopy) { | ||
setTags(toCopy.tags); | ||
} | ||
|
||
/** | ||
* Sets {@code tags} to this object's {@code tags}. | ||
* A defensive copy of {@code tags} is used internally. | ||
*/ | ||
public void setTags(Set<Tag> tags) { | ||
this.tags = (tags != null) ? new HashSet<>(tags) : null; | ||
} | ||
|
||
/** | ||
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException} | ||
* if modification is attempted. | ||
* Returns {@code Optional#empty()} if {@code tags} is null. | ||
*/ | ||
public Optional<Set<Tag>> getTags() { | ||
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof LabelPersonDescriptor)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
LabelPersonDescriptor e = (LabelPersonDescriptor) other; | ||
|
||
return getTags().equals(e.getTags()); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,9 @@ | |
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.util.CollectionUtil; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
|
@@ -29,36 +29,36 @@ | |
*/ | ||
public class EditCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "edit"; | ||
public static final String COMMAND_WORD = "contact edit"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified " | ||
+ "by the index number used in the displayed person list. " | ||
+ "by the name of the person in the displayed person list. " | ||
+ "Existing values will be overwritten by the input values.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "[" + PREFIX_NAME + "NAME] " | ||
+ "Parameters: NAME (must be name of person existing in ModDuke) " | ||
+ "[" + PREFIX_NAME + "NEW_NAME] " | ||
+ "[" + PREFIX_PHONE + "PHONE] " | ||
+ "[" + PREFIX_EMAIL + "EMAIL] " | ||
+ "[" + PREFIX_TAG + "TAG]...\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ "Example: " + COMMAND_WORD + " john doe " | ||
+ PREFIX_PHONE + "91234567 " | ||
+ PREFIX_EMAIL + "[email protected]"; | ||
|
||
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s"; | ||
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; | ||
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book."; | ||
|
||
private final Index index; | ||
private final Name name; | ||
private final EditPersonDescriptor editPersonDescriptor; | ||
|
||
/** | ||
* @param index of the person in the filtered person list to edit | ||
* @param name of the person in the filtered person list to edit | ||
* @param editPersonDescriptor details to edit the person with | ||
*/ | ||
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) { | ||
requireNonNull(index); | ||
public EditCommand(Name name, EditPersonDescriptor editPersonDescriptor) { | ||
requireNonNull(name); | ||
requireNonNull(editPersonDescriptor); | ||
|
||
this.index = index; | ||
this.name = name; | ||
this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor); | ||
} | ||
|
||
|
@@ -67,11 +67,15 @@ public CommandResult execute(Model model) throws CommandException { | |
requireNonNull(model); | ||
List<Person> lastShownList = model.getFilteredPersonList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
boolean isValidContact = model.hasPersonName(name); | ||
if (!isValidContact) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED); | ||
} | ||
|
||
Person personToEdit = lastShownList.get(index.getZeroBased()); | ||
List<Person> filteredList = lastShownList.stream() | ||
.filter(person -> person.isSameName(name)).collect(Collectors.toList()); | ||
Person personToEdit = filteredList.get(0); | ||
|
||
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor); | ||
|
||
if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) { | ||
|
@@ -112,7 +116,7 @@ public boolean equals(Object other) { | |
|
||
// state check | ||
EditCommand e = (EditCommand) other; | ||
return index.equals(e.index) | ||
return name.equals(e.name) | ||
&& editPersonDescriptor.equals(e.editPersonDescriptor); | ||
} | ||
|
||
|
Oops, something went wrong.