forked from nus-cs2103-AY2425S1/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.
Implement Remark Command according to AB3 Tutorial
- Loading branch information
1 parent
9bffd27
commit 804a927
Showing
24 changed files
with
475 additions
and
48 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package seedu.address.commons.core; | ||
|
||
/** | ||
* Container for user visible messages. | ||
*/ | ||
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_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; | ||
|
||
} |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
|
@@ -26,6 +27,7 @@ | |
import seedu.address.model.person.Name; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.Phone; | ||
import seedu.address.model.person.Remark; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
|
@@ -44,6 +46,7 @@ public class EditCommand extends Command { | |
+ "[" + PREFIX_EMAIL + "EMAIL] " | ||
+ "[" + PREFIX_ADDRESS + "ADDRESS] " | ||
+ "[" + PREFIX_TAG + "TAG]...\n" | ||
+ "[" + PREFIX_REMARK + "REMARK]...\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_PHONE + "91234567 " | ||
+ PREFIX_EMAIL + "[email protected]"; | ||
|
@@ -100,8 +103,9 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript | |
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail()); | ||
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress()); | ||
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags()); | ||
Remark updatedRemark = personToEdit.getRemark(); // edit command does not allow editing remarks | ||
|
||
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags); | ||
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags, updatedRemark); | ||
} | ||
|
||
@Override | ||
|
@@ -138,6 +142,7 @@ public static class EditPersonDescriptor { | |
private Email email; | ||
private Address address; | ||
private Set<Tag> tags; | ||
private Remark remark; | ||
|
||
public EditPersonDescriptor() {} | ||
|
||
|
@@ -200,6 +205,14 @@ public void setTags(Set<Tag> tags) { | |
this.tags = (tags != null) ? new HashSet<>(tags) : null; | ||
} | ||
|
||
public void setRemark(Remark remark) { | ||
this.remark = remark; | ||
} | ||
|
||
public Optional<Remark> getRemarks() { | ||
return Optional.ofNullable(remark); | ||
} | ||
|
||
/** | ||
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException} | ||
* if modification is attempted. | ||
|
94 changes: 94 additions & 0 deletions
94
src/main/java/seedu/address/logic/commands/RemarkCommand.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,94 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
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.Person; | ||
import seedu.address.model.person.Remark; | ||
|
||
/** | ||
* Changes the remark of an existing person in the address book. | ||
*/ | ||
public class RemarkCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "remark"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Edits the remark of the person identified " | ||
+ "by the index number used in the last person listing. " | ||
+ "Existing remark will be overwritten by the input.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "r/ [REMARK]\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ "r/ Likes to swim."; | ||
|
||
public static final String MESSAGE_ARGUMENTS = "Index: %1$d, Remark: %2$s"; | ||
public static final String MESSAGE_ADD_REMARK_SUCCESS = | ||
"Added remark to Person:%1$s"; | ||
public static final String MESSAGE_DELETE_REMARK_SUCCESS = | ||
"Removed remark from Person:%1$s"; | ||
private final Index index; | ||
private final Remark remark; | ||
|
||
/** | ||
* @param index of the person in the filtered person list to edit the remark | ||
* @param remark of the person to be updated to | ||
*/ | ||
public RemarkCommand(Index index, Remark remark) { | ||
requireAllNonNull(index, remark); | ||
|
||
this.index = index; | ||
this.remark = remark; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
List<Person> lastShownList = model.getFilteredPersonList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
Person personToEdit = lastShownList.get(index.getZeroBased()); | ||
Person editedPerson = new Person( | ||
personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), | ||
personToEdit.getAddress(), personToEdit.getTags(), remark); | ||
|
||
model.setPerson(personToEdit, editedPerson); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
|
||
return new CommandResult(generateSuccessMessage(editedPerson)); | ||
} | ||
|
||
/** | ||
* Generates a command execution success message based on whether | ||
* the remark is added to or removed from | ||
* {@code personToEdit}. | ||
*/ | ||
private String generateSuccessMessage(Person personToEdit) { | ||
String message = !remark.value.isEmpty() ? MESSAGE_ADD_REMARK_SUCCESS : MESSAGE_DELETE_REMARK_SUCCESS; | ||
return String.format(message, Messages.format(personToEdit)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof RemarkCommand)) { | ||
return false; | ||
} | ||
|
||
RemarkCommand e = (RemarkCommand) other; | ||
return index.equals(e.index) | ||
&& remark.equals(e.remark); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
src/main/java/seedu/address/logic/parser/RemarkCommandParser.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,39 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.logic.commands.RemarkCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.Remark; | ||
|
||
/** | ||
* Parses input arguments and creates a new RemarkCommand object | ||
*/ | ||
public class RemarkCommandParser implements Parser<RemarkCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the RemarkCommand | ||
* and returns a RemarkCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public RemarkCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, | ||
PREFIX_REMARK); | ||
|
||
Index index; | ||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
} catch (IllegalValueException ive) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
RemarkCommand.MESSAGE_USAGE), ive); | ||
} | ||
|
||
String remark = argMultimap.getValue(PREFIX_REMARK).orElse(""); | ||
|
||
return new RemarkCommand(index, new Remark(remark)); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package seedu.address.model.person; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Represents a Person's remark in the address book. | ||
* Guarantees: immutable; | ||
*/ | ||
public class Remark { | ||
|
||
public final String value; | ||
|
||
/** | ||
* Constructs an {@code Remark}. | ||
* | ||
* @param remark A valid remark. | ||
*/ | ||
public Remark(String remark) { | ||
requireNonNull(remark); | ||
value = remark; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof Remark)) { | ||
return false; | ||
} | ||
|
||
Remark otherRemark = (Remark) other; | ||
return value.equals(otherRemark.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
|
||
} |
Oops, something went wrong.