diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 59076a2020e..277d6a84948 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -120,6 +120,21 @@ Examples: * `teacher n/Lebron p/91234567 e/lbj@example.com a/George street, block 123, #01-01 i/Math HOD t/relief` +### Adding a medical history to a student: `medical` + +Adds a medical history to an existing student in NewAddressBook. + +Format: `medical INDEX m/MEDICAL_HISTORY` + +
:bulb: **Tip:** +Edits the student at the specified index. The index must be a positive integer. +To edit an existing medical history, simply use the same command, which will overwrite +the current medical history. +
+ +Examples: +* `medical 1 m/ADHD` + ### Listing all persons : `list` Shows a list of all persons stored in NewAddressBook. diff --git a/src/main/java/seedu/address/logic/commands/student/EditStudentCommand.java b/src/main/java/seedu/address/logic/commands/student/EditStudentCommand.java index c6c579bfae3..34f1f7dc744 100644 --- a/src/main/java/seedu/address/logic/commands/student/EditStudentCommand.java +++ b/src/main/java/seedu/address/logic/commands/student/EditStudentCommand.java @@ -18,10 +18,14 @@ import seedu.address.model.Model; import seedu.address.model.person.Address; import seedu.address.model.person.FormClass; +import seedu.address.model.person.MedicalHistory; import seedu.address.model.person.Person; import seedu.address.model.person.Phone; import seedu.address.model.person.Student; +/** + * Edits the fields of an existing student in the New Address Book. + */ public class EditStudentCommand extends EditCommand { public static final String TARGET = "student"; public static final String COMMAND_WORD = "editStudent"; @@ -60,7 +64,8 @@ private static Student createdEditedStudent(Student studentToEdit, EditStudentDe editStudentDescriptor.getEmergencyContact().orElse(studentToEdit.getEmergencyContact()); FormClass updatedFormClass = editStudentDescriptor.getFormClass().orElse(studentToEdit.getFormClass()); Address updatedAddress = editStudentDescriptor.getAddress().orElse(studentToEdit.getAddress()); - return new Student(person, updatedEmergencyContact, updatedFormClass, updatedAddress); + MedicalHistory updatedMedicalHistory = studentToEdit.getMedicalHistory(); //edit command does not edit medical + return new Student(person, updatedEmergencyContact, updatedFormClass, updatedAddress, updatedMedicalHistory); } @Override diff --git a/src/main/java/seedu/address/logic/commands/student/MedicalHistoryCommand.java b/src/main/java/seedu/address/logic/commands/student/MedicalHistoryCommand.java new file mode 100644 index 00000000000..7bfbea45887 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/student/MedicalHistoryCommand.java @@ -0,0 +1,101 @@ +package seedu.address.logic.commands.student; + +import static java.util.Objects.requireNonNull; +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.Messages; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.Command; +import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.person.MedicalHistory; +import seedu.address.model.person.Person; +import seedu.address.model.person.Student; + +/** + * Changes the medical history of an existing student inside the New Address Book. + */ +public class MedicalHistoryCommand extends Command { + public static final String COMMAND_WORD = "medical"; + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Edits the medical history of the person identified " + + "by the index number used in the last person listing. " + + "Existing medical history will be overwritten by the input.\n" + + "Parameters: INDEX (must be a positive integer) " + + "m/ [MEDICAL_HISTORY]\n" + + "Example: " + COMMAND_WORD + " 1 " + + "m/ ADHD"; + + public static final String MESSAGE_ARGUMENTS = "Index: %1$d, Medical_History: %2$s"; + public static final String MESSAGE_ADD_MEDICAL_HISTORY_SUCCESS = "Added medical history to Person: %1$s"; + public static final String MESSAGE_DELETE_MEDICAL_HISTORY_SUCCESS = "Removed medical history from Person: %1$s"; + + private final Index index; + private final MedicalHistory medicalHistory; + + /** + * @param index of the student in the filtered person list to edit the {@code MedicalHistory} + * @param medicalHistory of the student to be updated to + */ + public MedicalHistoryCommand(Index index, MedicalHistory medicalHistory) { + requireAllNonNull(index, medicalHistory); + + this.index = index; + this.medicalHistory = medicalHistory; + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + List lastShownList = model.getFilteredPersonList(); + + if (index.getZeroBased() >= lastShownList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + Person personToEdit = lastShownList.get(index.getZeroBased()); + if (!(personToEdit instanceof Student)) { + throw new CommandException("You cannot add medical history to a teacher!"); + } + Student studentToEdit = (Student) personToEdit; + Student editedStudent = new Student(studentToEdit, studentToEdit.getEmergencyContact(), + studentToEdit.getFormClass(), studentToEdit.getAddress(), medicalHistory); + + model.setPerson(studentToEdit, editedStudent); + model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + return new CommandResult(generateSuccessMessage(editedStudent)); + } + + /** + * Generates a command execution success message based on whether + * the medical history is added to or removed from + * {@code studentToEdit}. + */ + private String generateSuccessMessage(Student studentToEdit) { + String message = !medicalHistory.value.isEmpty() ? MESSAGE_ADD_MEDICAL_HISTORY_SUCCESS + : MESSAGE_DELETE_MEDICAL_HISTORY_SUCCESS; + return String.format(message, studentToEdit); + } + + @Override + public boolean equals(Object other) { + // short circuit if same object + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof MedicalHistoryCommand)) { + return false; + } + + // state check + MedicalHistoryCommand e = (MedicalHistoryCommand) other; + return index.equals(e.index) + && medicalHistory.equals(e.medicalHistory); + } +} diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index f11f25bd7ee..a36e1bda060 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -17,11 +17,13 @@ import seedu.address.logic.commands.ListCommand; import seedu.address.logic.commands.student.AddStudentCommand; import seedu.address.logic.commands.student.EditStudentCommand; +import seedu.address.logic.commands.student.MedicalHistoryCommand; import seedu.address.logic.commands.teacher.AddTeacherCommand; import seedu.address.logic.commands.teacher.EditTeacherCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.logic.parser.student.AddStudentCommandParser; import seedu.address.logic.parser.student.EditStudentCommandParser; +import seedu.address.logic.parser.student.MedicalHistoryCommandParser; import seedu.address.logic.parser.teacher.AddTeacherCommandParser; import seedu.address.logic.parser.teacher.EditTeacherCommandParser; @@ -88,6 +90,9 @@ public Command parseCommand(String userInput) throws ParseException { case FilterCommand.COMMAND_WORD: return new FilterCommandParser().parse(arguments); + case MedicalHistoryCommand.COMMAND_WORD: + return new MedicalHistoryCommandParser().parse(arguments); + default: throw new ParseException(MESSAGE_UNKNOWN_COMMAND); } diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index a4ecd283558..b26bf5aa797 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -17,4 +17,5 @@ public class CliSyntax { public static final Prefix PREFIX_GENDER = new Prefix("g/"); public static final Prefix PREFIX_OFFICE_TABLE = new Prefix("o/"); public static final Prefix PREFIX_COPY_FIELD = new Prefix("c/"); + public static final Prefix PREFIX_MEDICAL_HISTORY = new Prefix("m/"); } diff --git a/src/main/java/seedu/address/logic/parser/student/AddStudentCommandParser.java b/src/main/java/seedu/address/logic/parser/student/AddStudentCommandParser.java index 73b2e958132..0ee018880db 100644 --- a/src/main/java/seedu/address/logic/parser/student/AddStudentCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/student/AddStudentCommandParser.java @@ -67,10 +67,8 @@ public AddStudentCommand parse(String args) throws ParseException { Phone emergencyContact = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_EMERGENCY_CONTACT).get()); FormClass formClass = ParserUtil.parseFormClass(argMultimap.getValue(PREFIX_FORM_CLASS).get()); Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); - Student student = new Student(name, phone, email, gender, involvement, address, emergencyContact, formClass, tagList); - return new AddStudentCommand(student); } } diff --git a/src/main/java/seedu/address/logic/parser/student/MedicalHistoryCommandParser.java b/src/main/java/seedu/address/logic/parser/student/MedicalHistoryCommandParser.java new file mode 100644 index 00000000000..6e43b05419b --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/student/MedicalHistoryCommandParser.java @@ -0,0 +1,45 @@ +package seedu.address.logic.parser.student; + +import static java.util.Objects.requireNonNull; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CliSyntax.PREFIX_MEDICAL_HISTORY; + +import seedu.address.commons.core.index.Index; +import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.logic.commands.student.MedicalHistoryCommand; +import seedu.address.logic.parser.ArgumentMultimap; +import seedu.address.logic.parser.ArgumentTokenizer; +import seedu.address.logic.parser.Parser; +import seedu.address.logic.parser.ParserUtil; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.person.MedicalHistory; + +/** + * Parses input arguments and creates a new {@code MedicalHistoryCommand} object. + */ +public class MedicalHistoryCommandParser implements Parser { + + /** + * Parses the given {@code String} of arguments in the context of the MedicalHistoryCommand + * and returns an MedicalHistoryCommand object for execution. + * + * @throws ParseException if the user input does not conform the expected format + */ + public MedicalHistoryCommand parse(String args) throws ParseException { + requireNonNull(args); + ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, + PREFIX_MEDICAL_HISTORY); + + Index index; + try { + index = ParserUtil.parseIndex(argMultimap.getPreamble()); + } catch (IllegalValueException ive) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, + MedicalHistoryCommand.MESSAGE_USAGE), ive); + } + + String medicalHistory = argMultimap.getValue(PREFIX_MEDICAL_HISTORY).orElse(""); + + return new MedicalHistoryCommand(index, new MedicalHistory(medicalHistory)); + } +} diff --git a/src/main/java/seedu/address/logic/parser/teacher/AddTeacherCommandParser.java b/src/main/java/seedu/address/logic/parser/teacher/AddTeacherCommandParser.java index 5e458e5dff6..b4aca96db33 100644 --- a/src/main/java/seedu/address/logic/parser/teacher/AddTeacherCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/teacher/AddTeacherCommandParser.java @@ -68,7 +68,6 @@ public AddTeacherCommand parse(String args) throws ParseException { Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); Teacher teacher = new Teacher(name, phone, email, gender, involvement, tableNumber, tagList); - return new AddTeacherCommand(teacher); } } diff --git a/src/main/java/seedu/address/model/person/MedicalHistory.java b/src/main/java/seedu/address/model/person/MedicalHistory.java new file mode 100644 index 00000000000..fcfbc39a880 --- /dev/null +++ b/src/main/java/seedu/address/model/person/MedicalHistory.java @@ -0,0 +1,38 @@ +package seedu.address.model.person; + +import static java.util.Objects.requireNonNull; + +/** + * Represents a Student's medical history in the address book. + * Guarantees: immutable; is always valid + */ +public class MedicalHistory { + public final String value; + + /** + * Constructs a {@code MedicalHistory}. + * + * @param medicalHistory A medicalHistory. + */ + public MedicalHistory(String medicalHistory) { + requireNonNull(medicalHistory); + value = medicalHistory; + } + + @Override + public String toString() { + return value; + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof MedicalHistory // instanceof handles nulls + && value.equals(((MedicalHistory) other).value)); // state check + } + + @Override + public int hashCode() { + return value.hashCode(); + } +} diff --git a/src/main/java/seedu/address/model/person/Student.java b/src/main/java/seedu/address/model/person/Student.java index 430b16c77d4..753e0d89ba1 100644 --- a/src/main/java/seedu/address/model/person/Student.java +++ b/src/main/java/seedu/address/model/person/Student.java @@ -10,12 +10,13 @@ */ public class Student extends Person { - private final Phone emergencyContact; - private final FormClass formClass; - private final Address address; + private Phone emergencyContact; + private FormClass formClass; + private Address address; + private MedicalHistory medicalHistory; /** - * Constructor for {@code Student} + * Constructor for {@code Student} with every fields present * @param name Name of student * @param phone Phone number of student * @param email Email of student @@ -25,14 +26,15 @@ public class Student extends Person { * @param emergencyContact Emergency contact of student * @param formClass Form Class of Student * @param tags Tags associated to student + * @param medicalHistory Medical History of student */ - public Student(Name name, Phone phone, Email email, Gender gender, Involvement involvement, Address address, - Phone emergencyContact, FormClass formClass, Set tags) { + Phone emergencyContact, FormClass formClass, Set tags, MedicalHistory medicalHistory) { super(name, phone, email, gender, involvement, tags); this.emergencyContact = emergencyContact; this.formClass = formClass; this.address = address; + this.medicalHistory = medicalHistory; } /** @@ -42,14 +44,26 @@ public Student(Name name, Phone phone, Email email, Gender gender, Involvement i * @param emergencyContact Emergency contact of student * @param formClass Form Class of Student * @param address Address of student + * @param medicalHistory Medical History of student */ - - public Student(Person person, Phone emergencyContact, FormClass formClass, Address address) { + public Student(Person person, Phone emergencyContact, FormClass formClass, + Address address, MedicalHistory medicalHistory) { super(person.getName(), person.getPhone(), person.getEmail(), person.getGender(), person.getInvolvement(), - person.getTags()); + person.getTags()); this.emergencyContact = emergencyContact; this.formClass = formClass; this.address = address; + this.medicalHistory = medicalHistory; + } + + /** + * Constructor for {@code Student} without medical history + * + */ + public Student(Name name, Phone phone, Email email, Gender gender, Involvement involvement, Address address, + Phone emergencyContact, FormClass formClass, Set tags) { + this(name, phone, email, gender, involvement, address, + emergencyContact, formClass, tags, new MedicalHistory("")); } public Phone getEmergencyContact() { @@ -64,6 +78,10 @@ public Address getAddress() { return this.address; } + public MedicalHistory getMedicalHistory() { + return this.medicalHistory; + } + /** * Returns true if both person are students, and have the same identity and data fields. * This defines a stronger notion of equality between two students. @@ -82,13 +100,19 @@ public boolean equals(Object other) { return super.equals(otherStudent) && this.getEmergencyContact().equals(otherStudent.getEmergencyContact()) && this.getFormClass().equals(otherStudent.getFormClass()) - && this.getAddress().equals(otherStudent.getAddress()); + && this.getAddress().equals(otherStudent.getAddress()) + && this.getMedicalHistory().equals(otherStudent.getMedicalHistory()); } @Override public String toString() { - return super.toString() + "; " + String.format("Emergency contact: %s", getEmergencyContact()) + return super.toString() + "; " + + String.format("Emergency contact: %s", getEmergencyContact()) + + "; " + String.format("Form class: %s", getFormClass()) - + String.format("Address: %s", getAddress()); + + "; " + + String.format("Address: %s", getAddress()) + + "; " + + String.format("Medical History: %s", getMedicalHistory()); } } diff --git a/src/main/java/seedu/address/storage/JsonAdaptedStudent.java b/src/main/java/seedu/address/storage/JsonAdaptedStudent.java index 99a175d7b31..ee6f934a6f1 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedStudent.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedStudent.java @@ -14,6 +14,7 @@ import seedu.address.model.person.FormClass; import seedu.address.model.person.Gender; import seedu.address.model.person.Involvement; +import seedu.address.model.person.MedicalHistory; import seedu.address.model.person.Name; import seedu.address.model.person.Phone; import seedu.address.model.person.Student; @@ -29,6 +30,7 @@ class JsonAdaptedStudent extends JsonAdaptedPerson { private final String address; private final String emergencyContact; private final String formClass; + private final String medicalHistory; /** * Constructs a {@code JsonAdaptedStudent} with the given student details. @@ -40,11 +42,13 @@ public JsonAdaptedStudent(@JsonProperty("name") String name, @JsonProperty("phon @JsonProperty("address") String address, @JsonProperty("emergencyContact") String emergencyContact, @JsonProperty("formClass") String formClass, + @JsonProperty("medicalHistory") String medicalHistory, @JsonProperty("tagged") List tagged) { super(name, phone, email, gender, involvement, tagged); this.address = address; this.emergencyContact = emergencyContact; this.formClass = formClass; + this.medicalHistory = medicalHistory; } /** @@ -55,6 +59,7 @@ public JsonAdaptedStudent(Student source) { this.address = source.getAddress().value; this.emergencyContact = source.getEmergencyContact().value; this.formClass = source.getFormClass().value; + this.medicalHistory = source.getMedicalHistory().value; } /** @@ -136,8 +141,15 @@ public Student toModelType() throws IllegalValueException { } final FormClass modelFormClass = new FormClass(formClass); + if (medicalHistory == null) { + throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, + MedicalHistory.class.getSimpleName())); + } + final MedicalHistory modelMedicalHistory = new MedicalHistory(medicalHistory); + + final Set modelTags = new HashSet<>(studentTags); return new Student(modelName, modelPhone, modelEmail, modelGender, modelInvolvement, - modelAddress, modelEmergencyContact, modelFormClass, modelTags); + modelAddress, modelEmergencyContact, modelFormClass, modelTags, modelMedicalHistory); } } diff --git a/src/main/java/seedu/address/ui/StudentCard.java b/src/main/java/seedu/address/ui/StudentCard.java index 6a17ad798e2..6f1fe98c362 100644 --- a/src/main/java/seedu/address/ui/StudentCard.java +++ b/src/main/java/seedu/address/ui/StudentCard.java @@ -48,6 +48,8 @@ public class StudentCard extends UiPart { private Label formClass; @FXML private Label gender; + @FXML + private Label medicalHistory; /** * Creates a {@code PersonCode} with the given {@code Person} and index to display. @@ -62,11 +64,12 @@ public StudentCard(Student student, int displayedIndex) { email.setText(student.getEmail().value); formClass.setText(student.getFormClass().value); gender.setText(student.getGender().value); + medicalHistory.setText(student.getMedicalHistory().value); involvement.setText(student.getInvolvement().involvement); - student.getTags().stream() - .sorted(Comparator.comparing(tag -> tag.tagName)) - .forEach(tag -> tags.getChildren().add(new Label(tag.tagName))); emergencyContact.setText(student.getEmergencyContact().value); + student.getTags().stream() + .sorted(Comparator.comparing(tag -> tag.tagName)) + .forEach(tag -> tags.getChildren().add(new Label(tag.tagName))); } @Override diff --git a/src/main/resources/view/PersonListCard.fxml b/src/main/resources/view/PersonListCard.fxml index b0064e289d2..1bb66d0ad14 100644 --- a/src/main/resources/view/PersonListCard.fxml +++ b/src/main/resources/view/PersonListCard.fxml @@ -32,6 +32,7 @@