Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/AY2021S1-CS2103T-T09-2/tp
Browse files Browse the repository at this point in the history
…into branch-Lesson-find
  • Loading branch information
iqbxl committed Oct 19, 2020
2 parents 0b47a32 + 27a53af commit 0749343
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docs/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ Priorities: High (must have) - `* * *`, Medium (nice to have) - `* *`, Low (unli
| `* * *` | student who has no knowledge of working out|view what exercise routines the application has|choose the right one for me
| `* * *` | busy student | add workout routines into my schedule | have the time to exercise
| `* * *` | student | delete a workout routine | keep my schedule up-to-date
| `* * *` | NUS student | see my timetable | slot in my workout sessions with ease
| `* * *` | NUS student | see my timetable | slot in my workout sessions with ease

*{More to be added}*

Expand Down Expand Up @@ -293,7 +293,7 @@ Priorities: High (must have) - `* * *`, Medium (nice to have) - `* *`, Low (unli
* 3a1. AddressBook shows an error message.

Use case resumes at step 2.

**Use case: Create a new routine**

**MSS**
Expand All @@ -312,7 +312,7 @@ Priorities: High (must have) - `* * *`, Medium (nice to have) - `* *`, Low (unli
* 3a1. fitNUS shows an error message.

Use case resumes at step 2.

**Use case: Delete a routine**

**MSS**
Expand Down
2 changes: 1 addition & 1 deletion docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fitNUS is **optimized for use via a Command Line Interface** (CLI) while still

--------------------------------------------------------------------------------------------------------------------
<a name="quick-start"></a>
## Quick start
## Quick start

1. Ensure you have Java `11` or above installed in your Computer.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.model.Model;
import seedu.address.model.person.Exercise;
import seedu.address.model.person.Routine;
import seedu.address.model.person.exceptions.DuplicateExerciseException;

/**
* Adds an Routine to fitNUS.
Expand All @@ -30,6 +31,8 @@ public class RoutineAddExerciseCommand extends Command {
public static final String MESSAGE_SUCCESS = "Exercise added to Routine: %1$s";
public static final String MESSAGE_MISSING_ROUTINE = "This routine does not exist in fitNUS";
public static final String MESSAGE_MISSING_EXERCISE = "This exercise does not exist in fitNUS";
public static final String MESSAGE_DUPLICATE_EXERCISE = "This exercise already exist in the routine!";


private final Routine routineToAdd;
private final Exercise exerciseToAdd;
Expand All @@ -53,8 +56,12 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_MISSING_EXERCISE);
}

model.addExerciseToRoutine(routineToAdd, exerciseToAdd);
return new CommandResult(String.format(String.format(MESSAGE_SUCCESS, routineToAdd), exerciseToAdd));
try {
model.addExerciseToRoutine(routineToAdd, exerciseToAdd);
return new CommandResult(String.format(String.format(MESSAGE_SUCCESS, routineToAdd), exerciseToAdd));
} catch (DuplicateExerciseException error) {
throw new CommandException(MESSAGE_DUPLICATE_EXERCISE);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package seedu.address.logic.commands.routines;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ROUTINE;

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.Exercise;
import seedu.address.model.person.Routine;

/**
* Deletes a routine identified using it's displayed index from fitNUS.
*/
public class RoutineDeleteExerciseCommand extends Command {

public static final String COMMAND_WORD = "routine_delete_exercise";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the mentioned Exercise used in the specified Routine.\n"
+ "Parameters: "
+ PREFIX_ROUTINE + "ROUTINE_NAME "
+ PREFIX_EMAIL + "EXERCISE_NAME"
+ "\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_ROUTINE + "Leg Day Session "
+ PREFIX_EMAIL + "Squats ";

public static final String MESSAGE_DELETE_EXERCISE_SUCCESS = "Deleted Exercise from Routine: %1$s";
public static final String MESSAGE_MISSING_ROUTINE = "Deleted Exercise from Routine: %1$s";
public static final String MESSAGE_MISSING_EXERCISE = "Deleted Exercise from Routine: %1$s";

private final Routine routine;
private final Exercise exercise;

/**
* Creates a RoutineDeleteExericseCommand object.
* @param routine Specified Routine that the user wants to delete an Exercise from.
* @param exercise Specified Exercise that the user wants to delete.
*/
public RoutineDeleteExerciseCommand(Routine routine, Exercise exercise) {
this.routine = routine;
this.exercise = exercise;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (!model.hasRoutine(routine)) {
throw new CommandException(MESSAGE_MISSING_ROUTINE);
} else if (!model.hasExercise(exercise)) {
throw new CommandException(MESSAGE_MISSING_EXERCISE);
}

model.deleteExerciseToRoutine(routine, exercise);
return new CommandResult(String.format(String.format(MESSAGE_DELETE_EXERCISE_SUCCESS,
routine), exercise));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof RoutineDeleteExerciseCommand // instanceof handles nulls
&& exercise.equals(((RoutineDeleteExerciseCommand) other).exercise) // state check
&& routine.equals(((RoutineDeleteExerciseCommand) other).routine)); // state check
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import seedu.address.logic.commands.routines.RoutineAddExerciseCommand;
import seedu.address.logic.commands.routines.RoutineCreateCommand;
import seedu.address.logic.commands.routines.RoutineDeleteCommand;
import seedu.address.logic.commands.routines.RoutineDeleteExerciseCommand;
import seedu.address.logic.commands.routines.RoutineListCommand;
import seedu.address.logic.commands.routines.RoutineViewCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.routines.RoutineAddExerciseCommandParser;
import seedu.address.logic.parser.routines.RoutineCreateCommandParser;
import seedu.address.logic.parser.routines.RoutineDeleteCommandParser;
import seedu.address.logic.parser.routines.RoutineDeleteExerciseCommandParser;
import seedu.address.logic.parser.routines.RoutineViewCommandParser;

/**
Expand Down Expand Up @@ -144,6 +146,10 @@ public Command parseCommand(String userInput) throws ParseException {
case AddWeightCommand.COMMAND_WORD:
return new AddWeightCommandParser().parse(arguments);


case RoutineDeleteExerciseCommand.COMMAND_WORD:
return new RoutineDeleteExerciseCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package seedu.address.logic.parser.routines;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ROUTINE;

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

import seedu.address.logic.commands.routines.RoutineDeleteExerciseCommand;
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.Prefix;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Exercise;
import seedu.address.model.person.Name;
import seedu.address.model.person.Routine;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new AddCommand object
*/
public class RoutineDeleteExerciseCommandParser implements Parser<RoutineDeleteExerciseCommand> {

/**
* Parses the given {@code String} of arguments in the context of the RoutineDeleteExerciseCommand
* and returns an RoutineDeleteExerciseCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public RoutineDeleteExerciseCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_ROUTINE, PREFIX_EMAIL);

if (!arePrefixesPresent(argMultimap, PREFIX_ROUTINE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
RoutineDeleteExerciseCommand.MESSAGE_USAGE));
}

Name routineName = ParserUtil.parseName(argMultimap.getValue(PREFIX_ROUTINE).get());

Name exerciseName = ParserUtil.parseName(argMultimap.getValue(PREFIX_EMAIL).get());
Routine routine = new Routine(routineName);
Set<Tag> tagList = new HashSet<>();
Exercise exercise = new Exercise(exerciseName, tagList);

return new RoutineDeleteExerciseCommand(routine, exercise);
}

/**
* 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());
}

}
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public void removePerson(Person key) {
*/
public void removeExercise(Exercise key) {
exercises.remove(key);
routines.deleteExercise(key);
}

/**
Expand Down Expand Up @@ -359,4 +360,18 @@ public Routine retrieveRoutine(Routine routine) {
return routines.retrieveRoutine(routine);
}

/**
* Deletes an existing Exercise in fitNUS from an existing Routine.
*
* @param r Existing Routine.
* @param e Existing Exercise.
*/
public void deleteExerciseToRoutine(Routine r, Exercise e) {
requireNonNull(r);
requireNonNull(e);

Exercise retrievedExercise = exercises.retrieveExercise(e);
Routine retrievedRoutine = routines.retrieveRoutine(r);
routines.deleteExerciseFromRoutine(retrievedRoutine, retrievedExercise);
}
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,11 @@ public interface Model {
* @return Routine object that exists within fitNUS that the user is looking for.
*/
Routine retrieveRoutine(Routine routine);

/**
* Deletes the specified exercise from an existing routine.
* {@code routine} must already exist within fitNUS.
* {@code exercise} must already exist within fitNUS.
*/
void deleteExerciseToRoutine(Routine routine, Exercise exercise);
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ public void addExerciseToRoutine(Routine r, Exercise e) {
updateFilteredExerciseList(PREDICATE_SHOW_ALL_EXERCISES);
}

@Override
public void deleteExerciseToRoutine(Routine r, Exercise e) {
addressBook.deleteExerciseToRoutine(r, e);
updateFilteredRoutineList(PREDICATE_SHOW_ALL_ROUTINES);
updateFilteredExerciseList(PREDICATE_SHOW_ALL_EXERCISES);
}

@Override
public boolean hasExercise(Exercise exercise) {
requireNonNull(exercise);
Expand Down
43 changes: 42 additions & 1 deletion src/main/java/seedu/address/model/person/UniqueRoutineList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.model.person.exceptions.DuplicateExerciseException;
import seedu.address.model.person.exceptions.DuplicateRoutineException;
import seedu.address.model.person.exceptions.RoutineNotFoundException;

Expand Down Expand Up @@ -52,7 +54,8 @@ public void add(Routine toAdd) {

/**
* Adds an existing Exercise within fitNUS into an existing Routine within fitNUS.
* @param r Existing Routine.
*
* @param r Existing Routine.
* @param exercise Existing Exercise.
*/
public void addExercise(Routine r, Exercise exercise) {
Expand All @@ -62,6 +65,10 @@ public void addExercise(Routine r, Exercise exercise) {
} else {
for (Routine routine : internalList) {
if (routine.isSameActivity(r)) {
Set<Exercise> routineExercises = routine.getExercises();
if (routineExercises.contains(exercise)) {
throw new DuplicateExerciseException();
}
routine.addExercise(exercise);
break;
}
Expand All @@ -71,6 +78,7 @@ public void addExercise(Routine r, Exercise exercise) {

/**
* Returns the toString method of the Routine that the user wants to view.
*
* @param index Index of the Routine that the user wants to view.
* @return The toString method of the Routine that the user wants to see.
*/
Expand All @@ -80,6 +88,7 @@ public String viewRoutine(int index) {

/**
* Lists out all the Routine objects in UniqueRoutineList.
*
* @return String containing all the Routine object toString method.
*/
public String listRoutines() {
Expand Down Expand Up @@ -126,6 +135,7 @@ public void remove(Routine toRemove) {

/**
* Returns the size of the UniqueRoutineList.
*
* @return Integer of the size of the UniqueRoutineList.
*/
public int checkSize() {
Expand Down Expand Up @@ -190,6 +200,7 @@ private boolean routinesAreUnique(List<Routine> routines) {

/**
* Retrieves the Routine object from UniqueRoutineList that the user specified.
*
* @param r Routine object that the user wants.
* @return Routine object that exists within fitNUS that the user is looking for.
*/
Expand All @@ -201,4 +212,34 @@ public Routine retrieveRoutine(Routine r) {
}
return r;
}

/**
* Deletes the specified Exercise from the specified Routine.
*
* @param retrievedRoutine User-specified Routine.
* @param retrievedExercise User-specified Exercise.
*/
public void deleteExerciseFromRoutine(Routine retrievedRoutine, Exercise retrievedExercise) {
if (!internalList.contains(retrievedRoutine)) {
throw new RoutineNotFoundException();
} else {
for (Routine routine : internalList) {
if (routine.isSameActivity(retrievedRoutine)) {
routine.deleteExercise(retrievedExercise);
break;
}
}
}
}

/**
* Deletes specified Exercise from all Routines.
* @param retrievedExercise User-specified Exercise to remove from all Routines.
*/
public void deleteExercise(Exercise retrievedExercise) {
for (Routine routine : internalList) {
Set<Exercise> routineExercises = routine.getExercises();
routineExercises.remove(retrievedExercise);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Routine toModelType() throws IllegalValueException {
for (Exercise exercise : modelExercise) {
modelRoutine.addExercise(exercise);
}
return new Routine(modelName);
return modelRoutine;
}

}
Loading

0 comments on commit 0749343

Please sign in to comment.