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

Change edit command to update command #34

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ 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_DISPLAYED_NAME = "The person name provided is invalid";
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): ";
public static final String MESSAGE_DUPLICATE_NAMES = "There are people with the same names in the list";

/**
* Returns an error message indicating the duplicate prefixes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand All @@ -33,17 +33,18 @@
import seedu.address.model.tag.Tag;

/**
* Edits the details of an existing person in the address book.
* Updates the details of an existing person in the address book.
*/
public class EditCommand extends Command {
public class UpdateCommand extends Command {

public static final String COMMAND_WORD = "edit";
public static final String COMMAND_WORD = "update";

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. "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Updates the details of the person identified "
+ "by their full name used in the displayed person list. "
+ "The full name provided is not required to be case sensitive."
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_NAME + "NAME] "
+ "Parameters: u/TARGET_NAME "
+ "[" + PREFIX_NAME + "NEW_NAME] "
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
Expand All @@ -54,60 +55,71 @@
+ 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_UPDATE_PERSON_SUCCESS = "Updated Person: %1$s";
public static final String MESSAGE_NOT_UPDATED = "At least one field to update must be provided.";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";

private final Index index;
private final EditPersonDescriptor editPersonDescriptor;
private final Name name;
private final UpdatePersonDescriptor updatePersonDescriptor;

/**
* @param index of the person in the filtered person list to edit
* @param editPersonDescriptor details to edit the person with
* @param name of the person in the filtered person list to update
* @param updatePersonDescriptor details to update the person with
*/
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(index);
requireNonNull(editPersonDescriptor);
public UpdateCommand(Name name, UpdatePersonDescriptor updatePersonDescriptor) {
requireNonNull(name);
requireNonNull(updatePersonDescriptor);

this.index = index;
this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor);
this.name = name;
this.updatePersonDescriptor = new UpdatePersonDescriptor(updatePersonDescriptor);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();
List<Person> lastShownList = model.getAddressBook().getPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
Name lowerCaseName = new Name(name.fullName.toLowerCase());
List<Person> sameNamePeople = lastShownList.stream()
.filter(person -> person.getLowerCaseName().equals(lowerCaseName))
Jolonauh marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toList());

if (sameNamePeople.isEmpty()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_NAME);
Jolonauh marked this conversation as resolved.
Show resolved Hide resolved
}

if (sameNamePeople.size() > 1) {
throw new CommandException(Messages.MESSAGE_DUPLICATE_NAMES);

Check warning on line 92 in src/main/java/seedu/address/logic/commands/UpdateCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/UpdateCommand.java#L92

Added line #L92 was not covered by tests
}

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);
Person personToUpdate = sameNamePeople.get(0);

if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
Person updatedPerson = createUpdatedPerson(personToUpdate, updatePersonDescriptor);

if (!personToUpdate.isSamePerson(updatedPerson) && model.hasPerson(updatedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.setPerson(personToEdit, editedPerson);
model.setPerson(personToUpdate, updatedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
return new CommandResult(String.format(MESSAGE_UPDATE_PERSON_SUCCESS, Messages.format(updatedPerson)));
}

/**
* Creates and returns a {@code Person} with the details of {@code personToEdit}
* edited with {@code editPersonDescriptor}.
* Creates and returns a {@code Person} with the details of {@code personToUpdate}
* updated with {@code updatePersonDescriptor}.
*/
private static Person createEditedPerson(Person personToEdit, EditPersonDescriptor editPersonDescriptor) {
assert personToEdit != null;

Name updatedName = editPersonDescriptor.getName().orElse(personToEdit.getName());
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Description updatedDescription = editPersonDescriptor.getDescription().orElse(personToEdit.getDescription());
NextOfKin updatedNextOfKin = editPersonDescriptor.getNextOfKin().orElse(personToEdit.getNextOfKin());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
private static Person createUpdatedPerson(Person personToUpdate, UpdatePersonDescriptor updatePersonDescriptor) {
assert personToUpdate != null;

Name updatedName = updatePersonDescriptor.getName().orElse(personToUpdate.getName());
Phone updatedPhone = updatePersonDescriptor.getPhone().orElse(personToUpdate.getPhone());
Email updatedEmail = updatePersonDescriptor.getEmail().orElse(personToUpdate.getEmail());
Address updatedAddress = updatePersonDescriptor.getAddress().orElse(personToUpdate.getAddress());
Description updatedDescription = updatePersonDescriptor.getDescription().orElse(personToUpdate
.getDescription());
NextOfKin updatedNextOfKin = updatePersonDescriptor.getNextOfKin().orElse(personToUpdate.getNextOfKin());
Set<Tag> updatedTags = updatePersonDescriptor.getTags().orElse(personToUpdate.getTags());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress,
updatedDescription, updatedNextOfKin, updatedTags
Expand All @@ -121,28 +133,28 @@
}

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

EditCommand otherEditCommand = (EditCommand) other;
return index.equals(otherEditCommand.index)
&& editPersonDescriptor.equals(otherEditCommand.editPersonDescriptor);
UpdateCommand otherUpdateCommand = (UpdateCommand) other;
return name.equals(otherUpdateCommand.name)
&& updatePersonDescriptor.equals(otherUpdateCommand.updatePersonDescriptor);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("index", index)
.add("editPersonDescriptor", editPersonDescriptor)
.add("name", name)
.add("updatePersonDescriptor", updatePersonDescriptor)
.toString();
}

/**
* Stores the details to edit the person with. Each non-empty field value will replace the
* Stores the details to update the person with. Each non-empty field value will replace the
* corresponding field value of the person.
*/
public static class EditPersonDescriptor {
public static class UpdatePersonDescriptor {
private Name name;
private Phone phone;
private Email email;
Expand All @@ -151,13 +163,13 @@
private NextOfKin nextOfKin;
private Set<Tag> tags;

public EditPersonDescriptor() {}
public UpdatePersonDescriptor() {}

/**
* Copy constructor.
* A defensive copy of {@code tags} is used internally.
*/
public EditPersonDescriptor(EditPersonDescriptor toCopy) {
public UpdatePersonDescriptor(UpdatePersonDescriptor toCopy) {
setName(toCopy.name);
setPhone(toCopy.phone);
setEmail(toCopy.email);
Expand All @@ -168,9 +180,9 @@
}

/**
* Returns true if at least one field is edited.
* Returns true if at least one field is updated.
*/
public boolean isAnyFieldEdited() {
public boolean isAnyFieldUpdated() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags);
}

Expand Down Expand Up @@ -246,18 +258,18 @@
}

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

EditPersonDescriptor otherEditPersonDescriptor = (EditPersonDescriptor) other;
return Objects.equals(name, otherEditPersonDescriptor.name)
&& Objects.equals(phone, otherEditPersonDescriptor.phone)
&& Objects.equals(email, otherEditPersonDescriptor.email)
&& Objects.equals(address, otherEditPersonDescriptor.address)
&& Objects.equals(description, otherEditPersonDescriptor.description)
&& Objects.equals(nextOfKin, otherEditPersonDescriptor.nextOfKin)
&& Objects.equals(tags, otherEditPersonDescriptor.tags);
UpdatePersonDescriptor otherUpdatePersonDescriptor = (UpdatePersonDescriptor) other;
return Objects.equals(name, otherUpdatePersonDescriptor.name)
&& Objects.equals(phone, otherUpdatePersonDescriptor.phone)
&& Objects.equals(email, otherUpdatePersonDescriptor.email)
&& Objects.equals(address, otherUpdatePersonDescriptor.address)
&& Objects.equals(description, otherUpdatePersonDescriptor.description)
&& Objects.equals(nextOfKin, otherUpdatePersonDescriptor.nextOfKin)
&& Objects.equals(tags, otherUpdatePersonDescriptor.tags);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.UpdateCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -56,8 +56,8 @@ public Command parseCommand(String userInput) throws ParseException {
case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(arguments);

case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);
case UpdateCommand.COMMAND_WORD:
return new UpdateCommandParser().parse(arguments);

case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(arguments);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class CliSyntax {
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_DESCRIPTION = new Prefix("d/");
public static final Prefix PREFIX_NOK = new Prefix("k/");
public static final Prefix PREFIX_UPDATE = new Prefix("u/");
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,80 +9,82 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOK;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_UPDATE;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.UpdateCommand;
import seedu.address.logic.commands.UpdateCommand.UpdatePersonDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Name;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new EditCommand object
* Parses input arguments and creates a new UpdateCommand object
*/
public class EditCommandParser implements Parser<EditCommand> {
public class UpdateCommandParser implements Parser<UpdateCommand> {

/**
* Parses the given {@code String} of arguments in the context of the EditCommand
* and returns an EditCommand object for execution.
* Parses the given {@code String} of arguments in the context of the UpdateCommand
* and returns an UpdateCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public EditCommand parse(String args) throws ParseException {
public UpdateCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE,
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_UPDATE, PREFIX_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_DESCRIPTION, PREFIX_NOK, PREFIX_TAG);

Index index;
Name name;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
name = ParserUtil.parseName(argMultimap.getValue(PREFIX_UPDATE)
.orElseThrow(() -> new ParseException(UpdateCommand.MESSAGE_USAGE)));
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UpdateCommand.MESSAGE_USAGE), pe);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_DESCRIPTION, PREFIX_NOK);

EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();
UpdateCommand.UpdatePersonDescriptor updatePersonDescriptor = new UpdatePersonDescriptor();

if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
editPersonDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()));
updatePersonDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()));
}
if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
editPersonDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()));
updatePersonDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()));
}
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
editPersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()));
updatePersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()));
}
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
updatePersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}
if (argMultimap.getValue(PREFIX_DESCRIPTION).isPresent()) {
editPersonDescriptor.setDescription(
updatePersonDescriptor.setDescription(
ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get()));
}
if (argMultimap.getValue(PREFIX_NOK).isPresent()) {
editPersonDescriptor.setNextOfKin(ParserUtil.parseNextOfKin(argMultimap.getValue(PREFIX_NOK).get()));
updatePersonDescriptor.setNextOfKin(ParserUtil.parseNextOfKin(argMultimap.getValue(PREFIX_NOK).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);
parseTagsForUpdate(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(updatePersonDescriptor::setTags);

if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
if (!updatePersonDescriptor.isAnyFieldUpdated()) {
throw new ParseException(UpdateCommand.MESSAGE_NOT_UPDATED);
}

return new EditCommand(index, editPersonDescriptor);
return new UpdateCommand(name, updatePersonDescriptor);
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty.
* If {@code tags} contain only one element which is an empty string, it will be parsed into a
* {@code Set<Tag>} containing zero tags.
*/
private Optional<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws ParseException {
private Optional<Set<Tag>> parseTagsForUpdate(Collection<String> tags) throws ParseException {
assert tags != null;

if (tags.isEmpty()) {
Expand Down
Loading
Loading