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

Add Remark command #129

Merged
merged 11 commits into from
Nov 1, 2023
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
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Remark;
import seedu.address.model.policy.Company;
import seedu.address.model.policy.Policy;
import seedu.address.model.policy.PolicyDate;
Expand Down Expand Up @@ -132,10 +133,11 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Nric updatedNric = editPersonDescriptor.getNric().orElse(personToEdit.getNric());
LicencePlate updatedLicencePlate =
editPersonDescriptor.getLicencePlate().orElse(personToEdit.getLicencePlate());
Remark updatedRemark = personToEdit.getRemark(); // edit command does not allow editing remarks
Policy updatedPolicy = getUpdatedPolicy(personToEdit, editPersonDescriptor);

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags, updatedNric,
updatedLicencePlate, updatedPolicy);
updatedLicencePlate, updatedRemark, updatedPolicy);
}

private static Policy getUpdatedPolicy(Person personToEdit, EditPersonDescriptor editPersonDescriptor) {
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/seedu/address/logic/commands/RemarkCommand.java
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";
Copy link
Collaborator

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

Copy link
Author

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 the MESSAGES class?

Copy link
Collaborator

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.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Remark;
import seedu.address.model.policy.Company;
import seedu.address.model.policy.Policy;
import seedu.address.model.policy.PolicyDate;
Expand Down Expand Up @@ -87,6 +88,7 @@ public AddCommand parse(String args) throws ParseException {
LicencePlate licencePlate = ParserUtil.parseLicencePlate(argMultimap.getValue(PREFIX_LICENCE_PLATE).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Remark remark = new Remark("");

if (!arePrefixesAbsent(argMultimap, PREFIX_COMPANY, PREFIX_POLICY_NUMBER, PREFIX_POLICY_ISSUE_DATE,
PREFIX_POLICY_EXPIRY_DATE)) {
Expand Down Expand Up @@ -127,7 +129,7 @@ public AddCommand parse(String args) throws ParseException {

Policy policy = new Policy(company, policyNumber, policyIssueDate, policyExpiryDate);

Person person = new Person(name, phone, email, address, tagList, nric, licencePlate, policy);
Person person = new Person(name, phone, email, address, tagList, nric, licencePlate, remark, policy);

return new AddCommand(person);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.RemarkCommand;
import seedu.address.logic.commands.RemindCommand;
import seedu.address.logic.commands.SortCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -89,6 +90,9 @@
case BatchDeleteCommand.COMMAND_WORD:
return new BatchDeleteCommandParser().parse(arguments);

case RemarkCommand.COMMAND_WORD:
return new RemarkCommandParser().parse(arguments);

Check warning on line 94 in src/main/java/seedu/address/logic/parser/AddressBookParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/AddressBookParser.java#L94

Added line #L94 was not covered by tests

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
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 @@ -18,4 +18,5 @@ public class CliSyntax {
public static final Prefix PREFIX_POLICY_ISSUE_DATE = new Prefix("pi/");
public static final Prefix PREFIX_POLICY_EXPIRY_DATE = new Prefix("pe/");
public static final Prefix PREFIX_DELETE_MONTH = new Prefix("dm/");
public static final Prefix PREFIX_REMARK = new Prefix("r/");
}
39 changes: 39 additions & 0 deletions src/main/java/seedu/address/logic/parser/RemarkCommandParser.java
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 {

Check warning on line 16 in src/main/java/seedu/address/logic/parser/RemarkCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/RemarkCommandParser.java#L16

Added line #L16 was not covered by tests
/**
* 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,

Check warning on line 24 in src/main/java/seedu/address/logic/parser/RemarkCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/RemarkCommandParser.java#L23-L24

Added lines #L23 - L24 were not covered by tests
PREFIX_REMARK);

Index index;
try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (IllegalValueException ive) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,

Check warning on line 31 in src/main/java/seedu/address/logic/parser/RemarkCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/RemarkCommandParser.java#L29-L31

Added lines #L29 - L31 were not covered by tests
RemarkCommand.MESSAGE_USAGE), ive);
}

Check warning on line 33 in src/main/java/seedu/address/logic/parser/RemarkCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/RemarkCommandParser.java#L33

Added line #L33 was not covered by tests

String remark = argMultimap.getValue(PREFIX_REMARK).orElse("");

Check warning on line 35 in src/main/java/seedu/address/logic/parser/RemarkCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/RemarkCommandParser.java#L35

Added line #L35 was not covered by tests

return new RemarkCommand(index, new Remark(remark));

Check warning on line 37 in src/main/java/seedu/address/logic/parser/RemarkCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/RemarkCommandParser.java#L37

Added line #L37 was not covered by tests
}
}
17 changes: 12 additions & 5 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
private final Nric nric;
private final LicencePlate licencePlate;
private final Policy policy;

private final Remark remark;
/**
* Every field must be present and not null.
* In the case of leads with null policy fields, default values will be put in place by the respective classes.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags, Nric nric,
LicencePlate licencePlate, Policy policy) {
requireAllNonNull(name, phone, email, nric, licencePlate, address, tags, policy);
LicencePlate licencePlate, Remark remark, Policy policy) {
requireAllNonNull(name, phone, email, nric, licencePlate, address, tags, remark, policy);
this.name = name;
this.phone = phone;
this.email = email;
Expand All @@ -47,6 +47,7 @@
this.nric = nric;
this.licencePlate = licencePlate;
this.policy = policy;
this.remark = remark;
}

public Name getName() {
Expand Down Expand Up @@ -85,6 +86,10 @@
return policy;
}

public Remark getRemark() {
return remark;
}

/**
* Returns true if both persons have the same attributes.
* This defines a weaker notion of equality between two persons.
Expand Down Expand Up @@ -157,13 +162,14 @@
&& tags.equals(otherPerson.tags)
&& nric.equals(otherPerson.nric)
&& licencePlate.equals(otherPerson.licencePlate)
&& policy.equals(otherPerson.policy);
&& policy.equals(otherPerson.policy)
&& remark.equals(otherPerson.remark);
}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(name, phone, email, address, tags, nric, licencePlate, policy);
return Objects.hash(name, phone, email, address, tags, nric, licencePlate, remark, policy);

Check warning on line 172 in src/main/java/seedu/address/model/person/Person.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Person.java#L172

Added line #L172 was not covered by tests
}

@Override
Expand All @@ -176,6 +182,7 @@
.add("tags", tags)
.add("nric", nric)
.add("licence plate", licencePlate)
.add("remark", remark)
.add("policy", policy)
.toString();
}
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/seedu/address/model/person/Remark.java
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();

Check warning on line 36 in src/main/java/seedu/address/model/person/Remark.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Remark.java#L36

Added line #L36 was not covered by tests
}
}
Loading
Loading