From 513942e111b4c4e88783f44494136950a7cdcf96 Mon Sep 17 00:00:00 2001 From: didymus <51285113+didymental@users.noreply.github.com> Date: Thu, 28 Oct 2021 23:13:35 +0800 Subject: [PATCH] Fix issues with Prescription Ui Display and Delete Prescription (#174) * Fix Prescription Flowpane to appear immediately upon add * Made prescription VBox appear upon first input * Send fix for prescription ui * Fix prescription immutable implementation * Fix Delete Prescription bug * Fix checkstyle and stub test * Resolve merge conflicts * Update variable name * Added checks for addPrescription and removePrescription Co-authored-by: huyuxin0429 --- .../prescription/AddPrescriptionCommand.java | 3 +- .../DeletePrescriptionCommand.java | 4 +-- .../seedu/docit/model/AppointmentBook.java | 35 ++++++++++++++++--- src/main/java/seedu/docit/model/Model.java | 6 ++++ .../java/seedu/docit/model/ModelManager.java | 30 ++++++++++++++++ .../docit/model/appointment/Appointment.java | 34 ++++++++++++++---- .../appointment/UniqueAppointmentList.java | 22 ++++++++++++ .../seedu/docit/model/patient/Patient.java | 8 ----- .../model/prescription/Prescription.java | 8 ++--- .../java/seedu/docit/ui/AppointmentCard.java | 13 +++++++ .../seedu/docit/ui/AppointmentListPanel.java | 11 +++--- .../resources/view/AppointmentListCard.fxml | 7 ++-- src/main/resources/view/PatientListCard.fxml | 3 +- .../seedu/docit/testutil/stubs/ModelStub.java | 16 ++++++++- 14 files changed, 163 insertions(+), 37 deletions(-) diff --git a/src/main/java/seedu/docit/logic/commands/prescription/AddPrescriptionCommand.java b/src/main/java/seedu/docit/logic/commands/prescription/AddPrescriptionCommand.java index b566f0dd7fe..fd94af48276 100644 --- a/src/main/java/seedu/docit/logic/commands/prescription/AddPrescriptionCommand.java +++ b/src/main/java/seedu/docit/logic/commands/prescription/AddPrescriptionCommand.java @@ -71,12 +71,11 @@ public CommandResult execute(Model model) throws CommandException { Prescription prescriptionToAdd = new Prescription(medicine, volume, duration); - if (appointmentToMakePrescription.containsPrescription(prescriptionToAdd)) { throw new CommandException(MESSAGE_DUPLICATE_MEDICINE); } - appointmentToMakePrescription.addPrescription(prescriptionToAdd); + model.addPrescription(appointmentToMakePrescription, prescriptionToAdd); model.updateFilteredAppointmentList(Model.PREDICATE_SHOW_ALL_APPOINTMENTS); return new CommandResult(String.format(MESSAGE_SUCCESS, prescriptionToAdd)); } diff --git a/src/main/java/seedu/docit/logic/commands/prescription/DeletePrescriptionCommand.java b/src/main/java/seedu/docit/logic/commands/prescription/DeletePrescriptionCommand.java index d6a32f41b66..a59d46f8859 100644 --- a/src/main/java/seedu/docit/logic/commands/prescription/DeletePrescriptionCommand.java +++ b/src/main/java/seedu/docit/logic/commands/prescription/DeletePrescriptionCommand.java @@ -41,7 +41,7 @@ public class DeletePrescriptionCommand extends AppointmentCommand { */ public DeletePrescriptionCommand(Index targetAppointmentIndex, String targetMedicineName) { this.targetAppointmentIndex = targetAppointmentIndex; - this.targetMedicineName = targetMedicineName; + this.targetMedicineName = targetMedicineName.trim().toLowerCase(); } @Override public CommandResult execute(Model model) throws CommandException { @@ -54,7 +54,7 @@ public DeletePrescriptionCommand(Index targetAppointmentIndex, String targetMedi Appointment appointmentToTarget = lastShownList.get(targetAppointmentIndex.getZeroBased()); try { - appointmentToTarget.removePrescription(targetMedicineName); + model.deletePrescription(appointmentToTarget, targetMedicineName); return new CommandResult(MESSAGE_DELETE_PRESCRIPTION_SUCCESS); } catch (MedicineNotFoundException e) { throw new CommandException(e.getMessage()); diff --git a/src/main/java/seedu/docit/model/AppointmentBook.java b/src/main/java/seedu/docit/model/AppointmentBook.java index 395726ed7a8..cfa0e92a012 100644 --- a/src/main/java/seedu/docit/model/AppointmentBook.java +++ b/src/main/java/seedu/docit/model/AppointmentBook.java @@ -10,6 +10,7 @@ import seedu.docit.model.appointment.Appointment; import seedu.docit.model.appointment.UniqueAppointmentList; import seedu.docit.model.patient.Patient; +import seedu.docit.model.prescription.Prescription; /** * Wraps all data at the docit-book level Duplicates are not allowed (by .isSameAppointment comparison) @@ -55,7 +56,6 @@ public void setAppointments(List appointments) { */ public void resetData(ReadOnlyAppointmentBook newData) { requireNonNull(newData); - setAppointments(newData.getAppointmentList()); } @@ -91,6 +91,27 @@ public void sortAppointments() { appointments.sort(); } + /** + * Adds a prescription to the indexed appointment in the list. + */ + public void addPrescription(int index, Prescription p) { + appointments.addPrescription(index, p); + } + + /** + * Removes a prescription from an appointment specified by the index in the list. + */ + public void deletePrescription(int index, String medicine) { + appointments.deletePrescription(index, medicine); + } + + /** + * Edits a prescription from an appointment specified by the index in the list. + */ + public void editPrescription(int index, Prescription p) { + appointments.editPrescription(index, p); + } + /** * Updates appointments in the list with {@code target} when there are changes to the patient's details. * The appointment identity of {@code editedAppointment} must not be the same as another @@ -134,7 +155,8 @@ public void removeAppointment(Appointment key) { //// util methods - @Override public String toString() { + @Override + public String toString() { Iterator i = appointments.iterator(); StringBuilder r = new StringBuilder(); while (i.hasNext()) { @@ -144,17 +166,20 @@ public void removeAppointment(Appointment key) { // TODO: refine later } - @Override public ObservableList getAppointmentList() { + @Override + public ObservableList getAppointmentList() { return appointments.asUnmodifiableObservableList(); } - @Override public boolean equals(Object other) { + @Override + public boolean equals(Object other) { return other == this // short circuit if same object || (other instanceof AppointmentBook // instanceof handles nulls && appointments.equals(((AppointmentBook) other).appointments)); } - @Override public int hashCode() { + @Override + public int hashCode() { return appointments.hashCode(); } } diff --git a/src/main/java/seedu/docit/model/Model.java b/src/main/java/seedu/docit/model/Model.java index 2f0fce452c2..90180325b38 100644 --- a/src/main/java/seedu/docit/model/Model.java +++ b/src/main/java/seedu/docit/model/Model.java @@ -168,6 +168,12 @@ public interface Model { String getArchivedAppointments(); + public void addPrescription(Appointment target, Prescription p); + + public void deletePrescription(Appointment target, String medicine); + + public void editPrescription(int i, Prescription p); + /** * Returns an unmodifiable view of the filtered appointment list */ diff --git a/src/main/java/seedu/docit/model/ModelManager.java b/src/main/java/seedu/docit/model/ModelManager.java index bf0f707021f..7209340af7c 100644 --- a/src/main/java/seedu/docit/model/ModelManager.java +++ b/src/main/java/seedu/docit/model/ModelManager.java @@ -10,6 +10,7 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.ArrayList; +import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -22,6 +23,7 @@ import seedu.docit.commons.core.LogsCenter; import seedu.docit.model.appointment.Appointment; import seedu.docit.model.patient.Patient; +import seedu.docit.model.prescription.Prescription; /** * Represents the in-memory model of the address book data. @@ -273,6 +275,34 @@ public void sortAppointments() { updateFilteredAppointmentList(PREDICATE_SHOW_ALL_APPOINTMENTS); } + /** + * Adds a prescription to appointment i in the list. + */ + public void addPrescription(Appointment target, Prescription p) { + Set nextPrescription = target.getPrescriptions(); + nextPrescription.add(p); + + Appointment editedAppt = new Appointment(target.getPatient(), target.getDatetime(), nextPrescription); + appointmentBook.setAppointment(target, editedAppt); + } + + /** + * Removes a prescription from an appointment i in the list. + */ + public void deletePrescription(Appointment target, String medicine) { + target.removePrescription(medicine); + Set nextPrescription = target.getPrescriptions(); + Appointment editedAppt = new Appointment(target.getPatient(), target.getDatetime(), nextPrescription); + appointmentBook.setAppointment(target, editedAppt); + } + + /** + * Edits a prescription from an appointment i in the list. + */ + public void editPrescription(int i, Prescription p) { + appointmentBook.editPrescription(i, p); + } + //=========== ArchivedAppointmentBook ======================================================================= /** diff --git a/src/main/java/seedu/docit/model/appointment/Appointment.java b/src/main/java/seedu/docit/model/appointment/Appointment.java index 2575dd5f243..081d95079e8 100644 --- a/src/main/java/seedu/docit/model/appointment/Appointment.java +++ b/src/main/java/seedu/docit/model/appointment/Appointment.java @@ -5,7 +5,6 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Objects; @@ -29,11 +28,9 @@ public class Appointment implements Comparable { // Identity fields private final Patient patient; + private Set prescriptions = new HashSet<>(); private final LocalDateTime datetime; - // Data fields - private final Set prescriptions = new HashSet<>(); - /** * Every field must be present and not null. */ @@ -60,16 +57,39 @@ public LocalDateTime getDatetime() { } public Set getPrescriptions() { - return Collections.unmodifiableSet(prescriptions); + return prescriptions; } - + /** + * Adds a prescription into the appointment. + * @param prescription prescription to be added. + * @throws DuplicatePrescriptionException if prescription already exists. + */ public void addPrescription(Prescription prescription) throws DuplicatePrescriptionException { + for (Prescription p : prescriptions) { + if (p.getMedicine().equals(prescription.getMedicine())) { + throw new DuplicatePrescriptionException(); + } + } this.prescriptions.add(prescription); + Set p = new HashSet<>(); + p.addAll(prescriptions); + this.prescriptions = p; } + /** + * Removes a prescription from an appointment. + * @param medicineName medicine name of prescription to be removed. + * @throws MedicineNotFoundException if no such medicine exists. + */ public void removePrescription(String medicineName) throws MedicineNotFoundException { - this.prescriptions.remove(medicineName); + if (!this.prescriptions.removeIf(p -> p.hasSameMedicalName( + new Prescription(medicineName, "", "")))) { + throw new MedicineNotFoundException(); + } + Set p = new HashSet<>(); + p.addAll(prescriptions); + this.prescriptions = p; } /** diff --git a/src/main/java/seedu/docit/model/appointment/UniqueAppointmentList.java b/src/main/java/seedu/docit/model/appointment/UniqueAppointmentList.java index af0428e50a3..eeb9f8f088b 100644 --- a/src/main/java/seedu/docit/model/appointment/UniqueAppointmentList.java +++ b/src/main/java/seedu/docit/model/appointment/UniqueAppointmentList.java @@ -10,6 +10,7 @@ import javafx.collections.ObservableList; import seedu.docit.model.appointment.exceptions.AppointmentNotFoundException; import seedu.docit.model.appointment.exceptions.DuplicateAppointmentException; +import seedu.docit.model.prescription.Prescription; /** * A list of appointments that enforces uniqueness between its elements and does not allow nulls. An appointment is @@ -103,6 +104,27 @@ public void sort() { internalList.sort(Appointment::compareTo); } + /** + * Adds a prescription to the indexed appointment in the list. + */ + public void addPrescription(int index, Prescription p) { + internalList.get(index).addPrescription(p); + } + + /** + * Removes a prescription from an appointment specified by the index in the list. + */ + public void deletePrescription(int index, String medicine) { + internalList.get(index).removePrescription(medicine); + } + + /** + * Edits a prescription from an appointment specified by the index in the list. + */ + public void editPrescription(int index, Prescription p) { + internalList.get(index).editPrescription(p); + } + /** * Returns the backing list as an unmodifiable {@code ObservableList}. */ diff --git a/src/main/java/seedu/docit/model/patient/Patient.java b/src/main/java/seedu/docit/model/patient/Patient.java index 43ad2a58e1c..2dbab273cdb 100644 --- a/src/main/java/seedu/docit/model/patient/Patient.java +++ b/src/main/java/seedu/docit/model/patient/Patient.java @@ -70,13 +70,6 @@ public MedicalHistory getMedicalHistory() { } /** -<<<<<<< HEAD:src/main/java/seedu/address/model/person/Patient.java -======= -<<<<<<< HEAD:src/main/java/seedu/docit/model/patient/Patient.java - * Returns true if both patients have the same name. - * This defines a weaker notion of equality between two patients. -======= ->>>>>>> appearance-update:src/main/java/seedu/docit/model/patient/Patient.java * Returns a {@code Patient} object that has the combined {@code MedicalHistory} object. * @param mH {@code MedicalHistory} object to be combined with existing patient medical history. * @return patient with combined {@code MedicalHistory} object. @@ -107,7 +100,6 @@ public Patient deleteMedicalHistory(Index index) { // tell-don't-ask /** * Returns true if both persons have the same name. * This defines a weaker notion of equality between two persons. ->>>>>>> master:src/main/java/seedu/address/model/person/Patient.java */ public boolean isSamePatient(Patient otherPatient) { if (otherPatient == this) { diff --git a/src/main/java/seedu/docit/model/prescription/Prescription.java b/src/main/java/seedu/docit/model/prescription/Prescription.java index 5d5c4636b85..0a26991b4c2 100644 --- a/src/main/java/seedu/docit/model/prescription/Prescription.java +++ b/src/main/java/seedu/docit/model/prescription/Prescription.java @@ -14,9 +14,9 @@ public class Prescription { * @param duration Duration of medicine intake */ public Prescription(String medicine, String volume, String duration) { - this.medicine = medicine; - this.volume = volume; - this.duration = duration; + this.medicine = medicine.toLowerCase(); + this.volume = volume.toLowerCase(); + this.duration = duration.toLowerCase(); } @@ -55,7 +55,7 @@ public boolean hasSameMedicalName(Prescription p) { } public String toUiFormat() { - return String.format("%s | %s | %s", medicine, volume, duration); + return medicine + " | " + volume + " | " + duration; } @Override diff --git a/src/main/java/seedu/docit/ui/AppointmentCard.java b/src/main/java/seedu/docit/ui/AppointmentCard.java index 214753cd298..d8710d03b03 100644 --- a/src/main/java/seedu/docit/ui/AppointmentCard.java +++ b/src/main/java/seedu/docit/ui/AppointmentCard.java @@ -7,6 +7,7 @@ import javafx.scene.layout.FlowPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Region; +import javafx.scene.layout.VBox; import seedu.docit.model.appointment.Appointment; import seedu.docit.model.patient.Patient; @@ -42,6 +43,10 @@ public class AppointmentCard extends UiPart { @FXML private Label time; @FXML + private Label prescription; + @FXML + private VBox prescriptionContainer; + @FXML private FlowPane tags; @FXML private FlowPane prescriptions; @@ -53,6 +58,7 @@ public class AppointmentCard extends UiPart { */ public AppointmentCard(Appointment appointment, int displayedIndex) { super(FXML); + this.appointment = appointment; Patient patient = appointment.getPatient(); id.setText(displayedIndex + ". "); @@ -63,9 +69,16 @@ public AppointmentCard(Appointment appointment, int displayedIndex) { .forEach(tag -> tags.getChildren().add(new Label(tag.tagName))); date.setText("\uD83D\uDCC5\t" + appointment.getFormattedDateString()); time.setText("\u0000\u23f0\t" + appointment.getFormattedTimeString()); + + if (appointment.getPrescriptions().size() == 0) { + prescriptionContainer.setVisible(false); + prescription.setVisible(false); + } + appointment.getPrescriptions().stream() .sorted(Comparator.comparing(presctn -> presctn.getMedicine())) .forEach(presctn -> prescriptions.getChildren().add(new Label(presctn.toUiFormat()))); + isToday.setVisible(appointment.isToday()); } diff --git a/src/main/java/seedu/docit/ui/AppointmentListPanel.java b/src/main/java/seedu/docit/ui/AppointmentListPanel.java index 8122619d267..7554c402cc1 100644 --- a/src/main/java/seedu/docit/ui/AppointmentListPanel.java +++ b/src/main/java/seedu/docit/ui/AppointmentListPanel.java @@ -32,16 +32,19 @@ public AppointmentListPanel(ObservableList appointmentList) { /** * Custom {@code ListCell} that displays the graphics of a {@code Appointment} using a {@code AppointmentCard}. */ - class AppointmentListViewCell extends ListCell { + private static class AppointmentListViewCell extends ListCell { @Override protected void updateItem(Appointment appointment, boolean empty) { super.updateItem(appointment, empty); - if (empty || appointment == null) { - setGraphic(null); + if (empty) { setText(null); - } else { + setGraphic(null); + } else if (appointment != null) { setGraphic(new AppointmentCard(appointment, getIndex() + 1).getRoot()); + } else { + setGraphic(null); + setText(null); } } } diff --git a/src/main/resources/view/AppointmentListCard.fxml b/src/main/resources/view/AppointmentListCard.fxml index e2dd8b65ddf..45fb80f849b 100644 --- a/src/main/resources/view/AppointmentListCard.fxml +++ b/src/main/resources/view/AppointmentListCard.fxml @@ -49,8 +49,9 @@ - -