Skip to content

Commit

Permalink
Merge pull request #244 from shuckycheese/noteR
Browse files Browse the repository at this point in the history
FIx addnotescommand
  • Loading branch information
choonzies authored Nov 7, 2024
2 parents 3b12db7 + 4e8b5c5 commit c1f1b68
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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());
}
}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/seedu/address/model/person/Notes.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit c1f1b68

Please sign in to comment.