Skip to content

Commit

Permalink
Fix issues with Prescription Ui Display and Delete Prescription (#174)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
didymental and huyuxin0429 authored Oct 28, 2021
1 parent 937574b commit 513942e
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/seedu/docit/model/AppointmentBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -55,7 +56,6 @@ public void setAppointments(List<Appointment> appointments) {
*/
public void resetData(ReadOnlyAppointmentBook newData) {
requireNonNull(newData);

setAppointments(newData.getAppointmentList());
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -134,7 +155,8 @@ public void removeAppointment(Appointment key) {

//// util methods

@Override public String toString() {
@Override
public String toString() {
Iterator<Appointment> i = appointments.iterator();
StringBuilder r = new StringBuilder();
while (i.hasNext()) {
Expand All @@ -144,17 +166,20 @@ public void removeAppointment(Appointment key) {
// TODO: refine later
}

@Override public ObservableList<Appointment> getAppointmentList() {
@Override
public ObservableList<Appointment> 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();
}
}
6 changes: 6 additions & 0 deletions src/main/java/seedu/docit/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/seedu/docit/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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<Prescription> 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<Prescription> 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 =======================================================================

/**
Expand Down
34 changes: 27 additions & 7 deletions src/main/java/seedu/docit/model/appointment/Appointment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,11 +28,9 @@ public class Appointment implements Comparable<Appointment> {

// Identity fields
private final Patient patient;
private Set<Prescription> prescriptions = new HashSet<>();
private final LocalDateTime datetime;

// Data fields
private final Set<Prescription> prescriptions = new HashSet<>();

/**
* Every field must be present and not null.
*/
Expand All @@ -60,16 +57,39 @@ public LocalDateTime getDatetime() {
}

public Set<Prescription> 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<Prescription> 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<Prescription> p = new HashSet<>();
p.addAll(prescriptions);
this.prescriptions = p;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}.
*/
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/seedu/docit/model/patient/Patient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


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

Expand Down Expand Up @@ -42,6 +43,10 @@ public class AppointmentCard extends UiPart<Region> {
@FXML
private Label time;
@FXML
private Label prescription;
@FXML
private VBox prescriptionContainer;
@FXML
private FlowPane tags;
@FXML
private FlowPane prescriptions;
Expand All @@ -53,6 +58,7 @@ public class AppointmentCard extends UiPart<Region> {
*/
public AppointmentCard(Appointment appointment, int displayedIndex) {
super(FXML);

this.appointment = appointment;
Patient patient = appointment.getPatient();
id.setText(displayedIndex + ". ");
Expand All @@ -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());
}

Expand Down
11 changes: 7 additions & 4 deletions src/main/java/seedu/docit/ui/AppointmentListPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ public AppointmentListPanel(ObservableList<Appointment> appointmentList) {
/**
* Custom {@code ListCell} that displays the graphics of a {@code Appointment} using a {@code AppointmentCard}.
*/
class AppointmentListViewCell extends ListCell<Appointment> {
private static class AppointmentListViewCell extends ListCell<Appointment> {
@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);
}
}
}
Expand Down
Loading

0 comments on commit 513942e

Please sign in to comment.