forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from tengcharmaine/addnote
Add addnote command to add a note to a Person's entry in the addressbook
- Loading branch information
Showing
12 changed files
with
319 additions
and
1 deletion.
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
105 changes: 105 additions & 0 deletions
105
src/main/java/seedu/address/logic/commands/AddNoteCommand.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,105 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_IC; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.IdentityCardNumberMatchesPredicate; | ||
import seedu.address.model.person.Note; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Changes the note of an existing person in the address book. | ||
*/ | ||
public class AddNoteCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "addnote"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the note of the person whose profile matches " | ||
+ "the specified IC (case-insensitive). " | ||
+ "Existing remark will be appended by default. To replace the original note, add -replace at " | ||
+ "the end of your command. E.g. addnote i/S0123456Q n/Diabetes -replace\n" | ||
+ "Parameters: " | ||
+ PREFIX_IC + "IC " | ||
+ PREFIX_NOTE + "NOTE \n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_IC + " S0123456Q " | ||
+ PREFIX_NOTE + "Healthy."; | ||
|
||
|
||
public static final String MESSAGE_MODIFY_NOTE_SUCCESS = "%1$s's note modified successfully!"; | ||
private final IdentityCardNumberMatchesPredicate icPredicate; | ||
private final Note note; | ||
private final boolean isReplace; | ||
|
||
/** | ||
* @param icPredicate of the person in the filtered person list to edit the note | ||
* @param note of the person to be updated to | ||
*/ | ||
public AddNoteCommand(IdentityCardNumberMatchesPredicate icPredicate, Note note, boolean isReplace) { | ||
requireAllNonNull(icPredicate, note); | ||
|
||
this.icPredicate = icPredicate; | ||
this.note = note; | ||
this.isReplace = isReplace; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
model.updateFilteredPersonList(icPredicate); | ||
List<Person> lastShownList = model.getFilteredPersonList(); | ||
|
||
if (lastShownList.isEmpty()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON); | ||
} | ||
|
||
Person personToEdit = lastShownList.get(0); | ||
Person editedPerson; | ||
|
||
if (isReplace) { | ||
editedPerson = new Person(personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), | ||
personToEdit.getIdentityCardNumber(), personToEdit.getAge(), personToEdit.getSex(), | ||
personToEdit.getAddress(), note, personToEdit.getTags()); | ||
} else { | ||
Note updatedNote = personToEdit.getNote().append("\n" + note.toString()); | ||
editedPerson = new Person(personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), | ||
personToEdit.getIdentityCardNumber(), personToEdit.getAge(), personToEdit.getSex(), | ||
personToEdit.getAddress(), updatedNote, personToEdit.getTags()); | ||
} | ||
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) { | ||
return String.format(MESSAGE_MODIFY_NOTE_SUCCESS, personToEdit); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AddNoteCommand)) { | ||
return false; | ||
} | ||
|
||
AddNoteCommand e = (AddNoteCommand) other; | ||
return icPredicate.equals(e.icPredicate) | ||
&& note.equals(e.note) | ||
&& isReplace == e.isReplace; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/seedu/address/logic/parser/AddNoteCommandParser.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,64 @@ | ||
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_FLAG; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_IC; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.logic.commands.AddNoteCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.IdentityCardNumber; | ||
import seedu.address.model.person.IdentityCardNumberMatchesPredicate; | ||
import seedu.address.model.person.Note; | ||
|
||
/** | ||
* Parses input arguments and creates a new {@code AddNoteCommand} object | ||
*/ | ||
public class AddNoteCommandParser implements Parser<AddNoteCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the {@code AddNoteCommand} | ||
* and returns a {@code AddNoteCommand} object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public AddNoteCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_IC, PREFIX_NOTE, PREFIX_FLAG); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_IC, PREFIX_NOTE) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddNoteCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
IdentityCardNumber ic; | ||
try { | ||
String note = ""; | ||
boolean isReplace = false; | ||
|
||
if (argMultimap.getValue(PREFIX_FLAG).isPresent()) { | ||
int startIndex = args.indexOf(PREFIX_NOTE.getPrefix()) + PREFIX_NOTE.getPrefix().length(); | ||
int endIndex = args.indexOf(PREFIX_FLAG.getPrefix()); | ||
note = args.substring(startIndex, endIndex).trim(); | ||
isReplace = true; | ||
} else { | ||
note = argMultimap.getValue(PREFIX_NOTE).orElse(""); | ||
} | ||
|
||
ic = ParserUtil.parseIC(argMultimap.getValue(PREFIX_IC).get()); | ||
return new AddNoteCommand(new IdentityCardNumberMatchesPredicate(ic), new Note(note), isReplace); | ||
} catch (IllegalValueException ive) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddNoteCommand.MESSAGE_USAGE), ive); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
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
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
50 changes: 50 additions & 0 deletions
50
src/test/java/seedu/address/logic/commands/AddNoteCommandTest.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,50 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE_AMY; | ||
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE_BOB; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.model.person.IdentityCardNumber; | ||
import seedu.address.model.person.IdentityCardNumberMatchesPredicate; | ||
import seedu.address.model.person.Note; | ||
|
||
/** | ||
* Contains integration tests (interaction with the Model) and unit tests for AddNoteCommand. | ||
*/ | ||
public class AddNoteCommandTest { | ||
@Test | ||
public void equals() { | ||
IdentityCardNumberMatchesPredicate firstPredicate = | ||
new IdentityCardNumberMatchesPredicate(new IdentityCardNumber("S1234567A")); | ||
IdentityCardNumberMatchesPredicate secondPredicate = | ||
new IdentityCardNumberMatchesPredicate(new IdentityCardNumber("S9876543B")); | ||
|
||
final AddNoteCommand standardCommand = new AddNoteCommand(firstPredicate, | ||
new Note(VALID_NOTE_AMY), false); | ||
|
||
// same values -> returns true | ||
AddNoteCommand commandWithSameValues = new AddNoteCommand(firstPredicate, | ||
new Note(VALID_NOTE_AMY), false); | ||
assertTrue(standardCommand.equals(commandWithSameValues)); | ||
|
||
// same object -> returns true | ||
assertTrue(standardCommand.equals(standardCommand)); | ||
|
||
// null -> returns false | ||
assertFalse(standardCommand.equals(null)); | ||
|
||
// different types -> returns false | ||
assertFalse(standardCommand.equals(new ClearCommand())); | ||
|
||
// different index -> returns false | ||
assertFalse(standardCommand.equals(new AddNoteCommand(secondPredicate, | ||
new Note(VALID_NOTE_AMY), false))); | ||
|
||
// different remark -> returns false | ||
assertFalse(standardCommand.equals(new AddNoteCommand(firstPredicate, | ||
new Note(VALID_NOTE_BOB), false))); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/seedu/address/logic/parser/AddNoteCommandParserTest.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,24 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.AddNoteCommand; | ||
|
||
public class AddNoteCommandParserTest { | ||
private AddNoteCommandParser parser = new AddNoteCommandParser(); | ||
private final String nonEmptyNote = "Some note."; | ||
|
||
@Test | ||
public void parse_missingCompulsoryField_failure() { | ||
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddNoteCommand.MESSAGE_USAGE); | ||
|
||
// no parameters | ||
assertParseFailure(parser, AddNoteCommand.COMMAND_WORD, expectedMessage); | ||
|
||
// no index | ||
assertParseFailure(parser, AddNoteCommand.COMMAND_WORD + " " + nonEmptyNote, expectedMessage); | ||
} | ||
} |
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
Oops, something went wrong.