|
| 1 | +package seedu.address.logic.commands.routines; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | +import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; |
| 5 | +import static seedu.address.logic.parser.CliSyntax.PREFIX_ROUTINE; |
| 6 | + |
| 7 | +import seedu.address.logic.commands.Command; |
| 8 | +import seedu.address.logic.commands.CommandResult; |
| 9 | +import seedu.address.logic.commands.exceptions.CommandException; |
| 10 | +import seedu.address.model.Model; |
| 11 | +import seedu.address.model.person.Exercise; |
| 12 | +import seedu.address.model.person.Routine; |
| 13 | + |
| 14 | +/** |
| 15 | + * Deletes a routine identified using it's displayed index from fitNUS. |
| 16 | + */ |
| 17 | +public class RoutineDeleteExerciseCommand extends Command { |
| 18 | + |
| 19 | + public static final String COMMAND_WORD = "routine_delete_exercise"; |
| 20 | + |
| 21 | + public static final String MESSAGE_USAGE = COMMAND_WORD |
| 22 | + + ": Deletes the mentioned Exercise used in the specified Routine.\n" |
| 23 | + + "Parameters: " |
| 24 | + + PREFIX_ROUTINE + "ROUTINE_NAME " |
| 25 | + + PREFIX_EMAIL + "EXERCISE_NAME" |
| 26 | + + "\n" |
| 27 | + + "Example: " + COMMAND_WORD + " " |
| 28 | + + PREFIX_ROUTINE + "Leg Day Session " |
| 29 | + + PREFIX_EMAIL + "Squats "; |
| 30 | + |
| 31 | + public static final String MESSAGE_DELETE_EXERCISE_SUCCESS = "Deleted Exercise from Routine: %1$s"; |
| 32 | + public static final String MESSAGE_MISSING_ROUTINE = "Deleted Exercise from Routine: %1$s"; |
| 33 | + public static final String MESSAGE_MISSING_EXERCISE = "Deleted Exercise from Routine: %1$s"; |
| 34 | + |
| 35 | + private final Routine routine; |
| 36 | + private final Exercise exercise; |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates a RoutineDeleteExericseCommand object. |
| 40 | + * @param routine Specified Routine that the user wants to delete an Exercise from. |
| 41 | + * @param exercise Specified Exercise that the user wants to delete. |
| 42 | + */ |
| 43 | + public RoutineDeleteExerciseCommand(Routine routine, Exercise exercise) { |
| 44 | + this.routine = routine; |
| 45 | + this.exercise = exercise; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public CommandResult execute(Model model) throws CommandException { |
| 50 | + requireNonNull(model); |
| 51 | + |
| 52 | + if (!model.hasRoutine(routine)) { |
| 53 | + throw new CommandException(MESSAGE_MISSING_ROUTINE); |
| 54 | + } else if (!model.hasExercise(exercise)) { |
| 55 | + throw new CommandException(MESSAGE_MISSING_EXERCISE); |
| 56 | + } |
| 57 | + |
| 58 | + model.deleteExerciseToRoutine(routine, exercise); |
| 59 | + return new CommandResult(String.format(String.format(MESSAGE_DELETE_EXERCISE_SUCCESS, |
| 60 | + routine), exercise)); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean equals(Object other) { |
| 65 | + return other == this // short circuit if same object |
| 66 | + || (other instanceof RoutineDeleteExerciseCommand // instanceof handles nulls |
| 67 | + && exercise.equals(((RoutineDeleteExerciseCommand) other).exercise) // state check |
| 68 | + && routine.equals(((RoutineDeleteExerciseCommand) other).routine)); // state check |
| 69 | + } |
| 70 | +} |
0 commit comments