Skip to content

Commit

Permalink
Added Prescription add/delete/list commands (#136)
Browse files Browse the repository at this point in the history
* Added Prescription add/delete/list commands

* Merge with master

* Fix checkstyle errors

* Updated to latest command format

* Fix checkstyle error

* Remove redundant prefix

* Merge master, resolve naming recommendation

* Fix checkstyle error
  • Loading branch information
huyuxin0429 authored Oct 26, 2021
1 parent 2297f1c commit f9deb2b
Show file tree
Hide file tree
Showing 21 changed files with 737 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package seedu.address.logic.commands.prescription;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DURATION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_VOLUME;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AppointmentCommand;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.prescription.Prescription;


/**
* Adds a prescription to an appointment.
*/
public class AddPrescriptionCommand extends AppointmentCommand {
public static final String COMMAND_WORD = "pa";

public static final String MESSAGE_USAGE =
"apmt "
+ COMMAND_WORD + ": Adds a prescription to an appointment. "
+ "Parameters: \n"
+ PREFIX_INDEX + "ID OF APPOINTMENT \n"
+ PREFIX_NAME + "MEDICINE \n"
+ PREFIX_VOLUME + "VOLUME \n"
+ PREFIX_DURATION + "DURATION \n"
+ "Example: apmt " + COMMAND_WORD + " "
+ PREFIX_INDEX + "1 "
+ PREFIX_NAME + "Penicillin "
+ PREFIX_VOLUME + "400 ml "
+ PREFIX_DURATION + "2 times a week ";

public static final String MESSAGE_SUCCESS = "New prescription added";
public static final String MESSAGE_DUPLICATE_MEDICINE =
"This medicine already exists in the prescription for this appointment";

private final Index targetAppointmentIndex;
private String medicine;
private String volume;
private String duration;

/**
* Creates an AddPrescriptionCommand to add the specified {@code Prescription}
* @param targetAppointmentIndex appointment index to make prescription
*/
public AddPrescriptionCommand(Index targetAppointmentIndex, String medicine, String volume, String duration) {
requireNonNull(targetAppointmentIndex);
requireNonNull(medicine);
requireNonNull(volume);
requireNonNull(duration);
this.targetAppointmentIndex = targetAppointmentIndex;
this.volume = volume;
this.medicine = medicine;
this.duration = duration;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Appointment> lastShownList = model.getFilteredAppointmentList();

if (targetAppointmentIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX);
}

Appointment appointmentToMakePrescription = lastShownList.get(targetAppointmentIndex.getZeroBased());
Prescription toAdd = new Prescription(medicine, volume, duration, appointmentToMakePrescription);

if (appointmentToMakePrescription.containsPrescription(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_MEDICINE);
}
appointmentToMakePrescription.addPrescription(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package seedu.address.logic.commands.prescription;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.List;
import java.util.Objects;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AppointmentCommand;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.prescription.exceptions.MedicineNotFoundException;



public class DeletePrescriptionCommand extends AppointmentCommand {
public static final String COMMAND_WORD = "pd";

public static final String MESSAGE_USAGE = "apmt "
+ COMMAND_WORD + ": Deletes a prescription from an appointment based "
+ "on appointment index and medicine name. \n"
+ "Parameters: \n"
+ PREFIX_INDEX + "ID OF APPOINTMENT \n"
+ PREFIX_NAME + "MEDICINE \n"
+ "Example: appt " + COMMAND_WORD + " "
+ PREFIX_INDEX + "1 "
+ PREFIX_NAME + "Penicillin ";

public static final String MESSAGE_DELETE_PRESCRIPTION_SUCCESS = "Deleted prescription";

private final Index targetAppointmentIndex;
private final String targetMedicineName;

/**
* Deletes a prescription from an appointment based on appointment index and medicine name
* @param targetAppointmentIndex Index of targeted appointment
* @param targetMedicineName Name of medicine associated with prescription to be deleted
*/
public DeletePrescriptionCommand(Index targetAppointmentIndex, String targetMedicineName) {
this.targetAppointmentIndex = targetAppointmentIndex;
this.targetMedicineName = targetMedicineName;
}

@Override public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Appointment> lastShownList = model.getFilteredAppointmentList();

if (targetAppointmentIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX);
}

Appointment appointmentToTarget = lastShownList.get(targetAppointmentIndex.getZeroBased());
try {
appointmentToTarget.removePrescription(targetMedicineName);
return new CommandResult(MESSAGE_DELETE_PRESCRIPTION_SUCCESS);
} catch (MedicineNotFoundException e) {
throw new CommandException(e.getMessage());
}

}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DeletePrescriptionCommand that = (DeletePrescriptionCommand) o;
return Objects.equals(targetAppointmentIndex, that.targetAppointmentIndex)
&& Objects.equals(targetMedicineName, that.targetMedicineName);
}

@Override
public int hashCode() {
return Objects.hash(targetAppointmentIndex, targetMedicineName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.address.logic.commands.prescription;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PRESCRIPTIONS;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AppointmentCommand;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.appointment.Appointment;



public class ListPrescriptionsCommand extends AppointmentCommand {
public static final String COMMAND_WORD = "pl";

public static final String MESSAGE_USAGE = "apmt "
+ COMMAND_WORD + ": List all prescriptions of an appointment "
+ "Parameters: \n"
+ PREFIX_INDEX + "ID OF APPOINTMENT \n"
+ "Example: appt " + COMMAND_WORD + " "
+ PREFIX_INDEX + "1 ";

public static final String MESSAGE_SUCCESS = "Listed all prescriptions of appointment";

private final Index targetAppointmentIndex;

public ListPrescriptionsCommand(Index targetAppointmentIndex) {
this.targetAppointmentIndex = targetAppointmentIndex;
}

@Override public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Appointment> lastShownList = model.getFilteredAppointmentList();
if (targetAppointmentIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX);
}

Appointment appointmentToTarget = lastShownList.get(targetAppointmentIndex.getZeroBased());

appointmentToTarget.updateFilteredPrescriptions(PREDICATE_SHOW_ALL_PRESCRIPTIONS);

return new CommandResult(MESSAGE_SUCCESS + appointmentToTarget.getFilteredPrescriptions());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATETIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.logic.parser.ParserUtil.hasAllPrefixes;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.stream.Stream;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AddAppointmentCommand;
Expand All @@ -28,7 +28,7 @@ public class AddAppointmentCommandParser implements AppointmentParser<AddAppoint
public AddAppointmentCommand parseAppointmentCommand(String args) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_INDEX, PREFIX_DATETIME);

if (!arePrefixesPresent(argMultimap, PREFIX_INDEX, PREFIX_DATETIME) || !argMultimap.getPreamble().isEmpty()) {
if (!hasAllPrefixes(argMultimap, PREFIX_INDEX, PREFIX_DATETIME) || !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddAppointmentCommand.MESSAGE_USAGE));
}
Expand All @@ -54,12 +54,5 @@ public AddAppointmentCommand parseAppointmentCommand(String args) throws ParseEx
return new AddAppointmentCommand(patientIndex, localDateTime);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given {@code
* ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.ParserUtil.hasAllPrefixes;

import java.util.Set;
import java.util.stream.Stream;

import seedu.address.logic.commands.AddPatientCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand All @@ -36,7 +36,7 @@ public AddPatientCommand parsePatientCommand(String args) throws ParseException
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_TAG, PREFIX_MEDICAL);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
if (!hasAllPrefixes(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPatientCommand.MESSAGE_USAGE));
}
Expand All @@ -53,12 +53,6 @@ public AddPatientCommand parsePatientCommand(String args) throws ParseException
return new AddPatientCommand(patient);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.parser.exceptions.ParseException;


/**
* Parses user input.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import seedu.address.logic.commands.ArchiveAppointmentCommand;
import seedu.address.logic.commands.DeleteAppointmentCommand;
import seedu.address.logic.commands.ListAppointmentsCommand;
import seedu.address.logic.commands.prescription.AddPrescriptionCommand;
import seedu.address.logic.commands.prescription.DeletePrescriptionCommand;
import seedu.address.logic.commands.prescription.ListPrescriptionsCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.prescription.AddPrescriptionCommandParser;
import seedu.address.logic.parser.prescription.DeletePrescriptionCommandParser;
import seedu.address.logic.parser.prescription.ListPrescriptionsCommandParser;

/**
* Parses user input.
Expand All @@ -30,6 +36,12 @@ public AppointmentCommand parseAppointmentCommand(String commandWord, String arg
return new ArchiveAppointmentCommandParser().parseAppointmentCommand(arguments);
case ListAppointmentsCommand.COMMAND_WORD:
return new ListAppointmentsCommand();
case AddPrescriptionCommand.COMMAND_WORD:
return new AddPrescriptionCommandParser().parse(arguments);
case DeletePrescriptionCommand.COMMAND_WORD:
return new DeletePrescriptionCommandParser().parse(arguments);
case ListPrescriptionsCommand.COMMAND_WORD:
return new ListPrescriptionsCommandParser().parse(arguments);
default:
throw new ParseException(MESSAGE_INVALID_COMMAND_FORMAT);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public class CliSyntax {
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_MEDICAL = new Prefix("m/");
public static final Prefix PREFIX_DATETIME = new Prefix("d/");
public static final Prefix PREFIX_VOLUME = new Prefix("v/");
public static final Prefix PREFIX_DURATION = new Prefix("d/");

}
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.StringUtil;
Expand Down Expand Up @@ -213,4 +214,12 @@ private static Object[] breakMhIntoEntries(String medicalHistory) {
private static boolean isValidMh(String entry) {
return !(entry.length() == 0 || entry == " " || entry == null);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given {@code
* ArgumentMultimap}.
*/
public static boolean hasAllPrefixes(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}
Loading

0 comments on commit f9deb2b

Please sign in to comment.