diff --git a/src/main/java/seedu/address/logic/commands/AddNotesCommand.java b/src/main/java/seedu/address/logic/commands/AddNotesCommand.java index 2d5984ae372..8fdf66958ed 100644 --- a/src/main/java/seedu/address/logic/commands/AddNotesCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddNotesCommand.java @@ -25,11 +25,13 @@ public class AddNotesCommand extends Command { + "by the index number used in the last patient listing. " + "Existing notes will be overwritten by the input.\n" + "Parameters: INDEX (must be a positive integer) " - + PREFIX_NOTES + "[NOTES]\n" + + PREFIX_NOTES + "NOTES\n" + "Example: " + COMMAND_WORD + " 1 " + PREFIX_NOTES + "Patient is prone to falling."; public static final String MESSAGE_ADD_NOTES_SUCCESS = "Added notes to Patient: \n\n%1$s"; public static final String MESSAGE_DELETE_NOTES_SUCCESS = "Removed notes from Patient: %1$s"; + public static final String MESSAGE_EMPTY_NOTE = "Unable to add empty notes.\n" + + "If you intended to delete a current patient note, please use the delnotes command"; private final Index index; private final Notes notes; diff --git a/src/main/java/seedu/address/logic/parser/AddNotesCommandParser.java b/src/main/java/seedu/address/logic/parser/AddNotesCommandParser.java index 89e7bd43f1d..2e4c283768c 100644 --- a/src/main/java/seedu/address/logic/parser/AddNotesCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/AddNotesCommandParser.java @@ -3,12 +3,16 @@ import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTES; +import java.util.stream.Stream; + import seedu.address.commons.core.index.Index; import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.logic.commands.AddNotesCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.Notes; + + /** * Parses input arguments and creates a new AddNotesCommand object. */ @@ -29,7 +33,18 @@ public AddNotesCommand parse(String args) throws ParseException { } catch (IllegalValueException ive) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddNotesCommand.MESSAGE_USAGE), ive); } - String notes = argMultimap.getValue(PREFIX_NOTES).orElse(""); - return new AddNotesCommand(index, new Notes(notes)); + if (!arePrefixesPresent(argMultimap, PREFIX_NOTES)) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddNotesCommand.MESSAGE_USAGE)); + } + + if (argMultimap.getValue(PREFIX_NOTES).get().isEmpty()) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddNotesCommand.MESSAGE_EMPTY_NOTE)); + } + Notes notes = ParserUtil.parseNotes(argMultimap.getValue(PREFIX_NOTES).get()); + return new AddNotesCommand(index, notes); + } + + private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { + return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); } } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 9cc84cc918f..e45e6e04341 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -16,6 +16,7 @@ import seedu.address.model.person.Id; import seedu.address.model.person.Medication; import seedu.address.model.person.Name; +import seedu.address.model.person.Notes; import seedu.address.model.person.Ward; /** @@ -118,6 +119,21 @@ public static Medication parseMedication(String medication) throws ParseExceptio return new Medication(trimmedMedication); } + /** + * Parses a {@code String Notes} into a {@code Notes}. + * Leading and trailing whitespaces will be trimmed. + * + * @throws ParseException if the given {@code notes} is invalid. + */ + public static Notes parseNotes(String notes) throws ParseException { + requireNonNull(notes); + String trimmedNotes = notes.trim(); + if (!Notes.isValidNote(trimmedNotes)) { + throw new ParseException(Notes.MESSAGE_CONSTRAINTS); + } + return new Notes(trimmedNotes); + } + /** * Parses a {@code String appointmentDescription} into a {@code String appointmentDescription}. * Leading and trailing whitespaces will be trimmed. diff --git a/src/main/java/seedu/address/model/person/Notes.java b/src/main/java/seedu/address/model/person/Notes.java index 939bf8ebb04..9dedf21956c 100644 --- a/src/main/java/seedu/address/model/person/Notes.java +++ b/src/main/java/seedu/address/model/person/Notes.java @@ -1,10 +1,15 @@ package seedu.address.model.person; import static java.util.Objects.requireNonNull; +import static seedu.address.commons.util.AppUtil.checkArgument; + /** * Represents a Patient's notes in WardWatch. * Guarantees: immutable; is always valid */ public class Notes { + public static final String MESSAGE_CONSTRAINTS = + "Notes field must have at least 1 alphanumeric character and has a character limit of 80."; + public static final String VALIDATION_REGEX = "^(?=.*[A-Za-z0-9]).{1,80}$"; public final String value; /** @@ -14,18 +19,26 @@ public class Notes { */ public Notes(String patientNotes) { requireNonNull(patientNotes); + checkArgument(isValidNote(patientNotes), MESSAGE_CONSTRAINTS); value = patientNotes; } + + public static boolean isValidNote(String test) { + return test.matches(VALIDATION_REGEX) || test.isEmpty(); + } + @Override public String toString() { return value; } + @Override public boolean equals(Object other) { return other == this // short circuit if same object || (other instanceof Notes // instanceof handles nulls && value.equals(((Notes) other).value)); // state check } + @Override public int hashCode() { return value.hashCode(); diff --git a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java index 26f13850ebc..dde45343723 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java @@ -109,6 +109,9 @@ public Person toModelType() throws IllegalValueException { if (notes == null) { throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Notes.class.getSimpleName())); } + if (!Notes.isValidNote(notes)) { + throw new IllegalValueException(Notes.MESSAGE_CONSTRAINTS); + } final Notes modelNotes = new Notes(notes); final Appointment modelAppointment = (appointment != null) ? appointment.toModelType() : null; diff --git a/src/test/java/seedu/address/logic/parser/AddNotesCommandParserTest.java b/src/test/java/seedu/address/logic/parser/AddNotesCommandParserTest.java index b7391c9c569..f30a99cec1a 100644 --- a/src/test/java/seedu/address/logic/parser/AddNotesCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddNotesCommandParserTest.java @@ -15,15 +15,10 @@ public class AddNotesCommandParserTest { private final String nonEmptyNotes = "Some notes."; @Test public void parse_indexSpecified_success() { - // have remark Index targetIndex = INDEX_FIRST_PERSON; String userInput = targetIndex.getOneBased() + " " + PREFIX_NOTES + nonEmptyNotes; AddNotesCommand expectedCommand = new AddNotesCommand(INDEX_FIRST_PERSON, new Notes(nonEmptyNotes)); assertParseSuccess(parser, userInput, expectedCommand); - // no remark - userInput = targetIndex.getOneBased() + " " + PREFIX_NOTES; - expectedCommand = new AddNotesCommand(INDEX_FIRST_PERSON, new Notes("")); - assertParseSuccess(parser, userInput, expectedCommand); } @Test public void parse_missingCompulsoryField_failure() {