forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add Remark command #129
Merged
brendanneojw
merged 11 commits into
AY2324S1-CS2103T-W16-3:master
from
cyaoxuan:86-add-remarks
Nov 1, 2023
Merged
Add Remark command #129
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ae38a90
Add Remark command
cyaoxuan cb25a9a
Add new line at EOF
cyaoxuan a7501c7
Add Javadocs
cyaoxuan e3bd19b
Add Javadocs
cyaoxuan a4c9df9
Fix style in tests
cyaoxuan 43d2b7f
Update README.md
cyaoxuan 52d3d84
Add Remark command
cyaoxuan 1ba894f
Update tests to include Remark
cyaoxuan a4b47b1
Merge branch 'master' into 86-add-remarks
cyaoxuan 962bfa2
Fix checkstyle
cyaoxuan 8f2ccca
Merge branch '86-add-remarks' of https://github.com/cyaoxuan/tp into …
cyaoxuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
91 changes: 91 additions & 0 deletions
91
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,91 @@ | ||
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"; | ||
Comment on lines
+29
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice way to show that the command succeeded |
||
|
||
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(), personToEdit.getNric(), | ||
personToEdit.getLicencePlate(), remark, personToEdit.getPolicy()); | ||
|
||
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, 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
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 { | ||
/** | ||
* 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,38 @@ | ||
package seedu.address.model.person; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Represents a Person's remark in the address book. | ||
* Guarantees: immutable; is always valid | ||
*/ | ||
public class Remark { | ||
public final String value; | ||
|
||
/** | ||
Comment on lines
+10
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like there is no regex for this field, which makes sense since is just a remark of a person |
||
* Constructs a {@code Remark}. | ||
* | ||
* @param remark A remark of any format. | ||
*/ | ||
public Remark(String remark) { | ||
requireNonNull(remark); | ||
value = remark; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof Remark // instanceof handles nulls | ||
&& value.equals(((Remark) other).value)); // state check | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might make more sense to put this in the MESSAGES class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Putting
COMMAND_WORD
in the class itself seems to be the pattern for other commands too. Can you clarify why it should be shifted to theMESSAGES
class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Rohan was confused by the use of
COMMAND_WORD
being able to be publicly accessed by other classes, I don't see an issue here as this is the command that we will use for the feature.