Skip to content

Commit

Permalink
Implement Remark Command according to AB3 Tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
MAOXIONGKAI committed Sep 26, 2024
1 parent 9bffd27 commit 804a927
Show file tree
Hide file tree
Showing 24 changed files with 475 additions and 48 deletions.
13 changes: 13 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
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 {

Check warning on line 6 in src/main/java/seedu/address/commons/core/Messages.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/commons/core/Messages.java#L6

Added line #L6 was not covered by tests

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!";

}
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 @@ -45,6 +45,8 @@ public static String format(Person person) {
.append(person.getAddress())
.append("; Tags: ");
person.getTags().forEach(builder::append);
builder.append("; Remark: ")
.append(person.getRemark());
return builder.toString();
}

Expand Down
15 changes: 14 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

/**
Expand All @@ -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]";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -138,6 +142,7 @@ public static class EditPersonDescriptor {
private Email email;
private Address address;
private Set<Tag> tags;
private Remark remark;

public EditPersonDescriptor() {}

Expand Down Expand Up @@ -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;
}

Check warning on line 210 in src/main/java/seedu/address/logic/commands/EditCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/EditCommand.java#L209-L210

Added lines #L209 - L210 were not covered by tests

public Optional<Remark> getRemarks() {
return Optional.ofNullable(remark);

Check warning on line 213 in src/main/java/seedu/address/logic/commands/EditCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/EditCommand.java#L213

Added line #L213 was not covered by tests
}

/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
Expand Down
94 changes: 94 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,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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;

/**
Expand Down Expand Up @@ -44,8 +45,9 @@ public AddCommand parse(String args) throws ParseException {
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Remark remark = new Remark("");

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

return new AddCommand(person);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -77,6 +78,9 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

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

Check warning on line 82 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#L82

Added line #L82 was not covered by tests

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public class CliSyntax {
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");

public static final Prefix PREFIX_REMARK = new Prefix("r/");
}
12 changes: 11 additions & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Remark;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -111,7 +112,7 @@ public static Tag parseTag(String tag) throws ParseException {
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>}.
* Parses a {@code String tags} into a {@code Set<Tag>}.
*/
public static Set<Tag> parseTags(Collection<String> tags) throws ParseException {
requireNonNull(tags);
Expand All @@ -121,4 +122,13 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
}
return tagSet;
}

/**
* Parses {@code String remark} into a {@code Remark}.
*/
public static Remark parseRemark(String value) {
requireNonNull(value);
final Remark remark = new Remark(value);
return remark;

Check warning on line 132 in src/main/java/seedu/address/logic/parser/ParserUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ParserUtil.java#L130-L132

Added lines #L130 - L132 were not covered by tests
}
}
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 implements Parser<RemarkCommand> {

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
}
}
10 changes: 8 additions & 2 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ public class Person {
// Data fields
private final Address address;
private final Set<Tag> tags = new HashSet<>();
private final Remark remark;

/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags, Remark remark) {
requireAllNonNull(name, phone, email, address, tags);
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.tags.addAll(tags);
this.remark = remark;
}

public Name getName() {
Expand All @@ -61,6 +63,10 @@ public Set<Tag> getTags() {
return Collections.unmodifiableSet(tags);
}

public Remark getRemark() {
return remark;
}

/**
* Returns true if both persons have the same name.
* This defines a weaker notion of equality between two persons.
Expand Down Expand Up @@ -111,7 +117,7 @@ public String toString() {
.add("email", email)
.add("address", address)
.add("tags", tags)
.add("remark", remark)
.toString();
}

}
48 changes: 48 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,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;

Check warning on line 31 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#L31

Added line #L31 was not covered by tests
}

// instanceof handles nulls
if (!(other instanceof Remark)) {
return false;

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
}

Remark otherRemark = (Remark) other;
return value.equals(otherRemark.value);
}

@Override
public int hashCode() {
return value.hashCode();

Check warning on line 45 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#L45

Added line #L45 was not covered by tests
}

}
Loading

0 comments on commit 804a927

Please sign in to comment.