Skip to content

Commit

Permalink
Crud regime (#90)
Browse files Browse the repository at this point in the history
* Move muscle to exercise package

* Add Regime

* Add  UniqueRegimeList

* Add regime to AddCommand

* Add regime to UI

* Add regime to storage

* Edit tests

* Seperate AddCommand to AddECommand and AddRCommand

* Implement delete regime

* Add sample data and edit command message

* Refactor code

* Refactor code

* Change regimeName to be compulsory field

* Implement add exercise to same regime name

* Implement delete of exercises in regime

* Change messages in logic

* Refactor code to pass checkstyle

* Refactor code

Close #66
  • Loading branch information
jietung authored and t-cheepeng committed Oct 12, 2019
1 parent aee0060 commit b44a7ed
Show file tree
Hide file tree
Showing 69 changed files with 1,812 additions and 291 deletions.
2 changes: 1 addition & 1 deletion docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Examples:
Adds exercises from the exercises you have added into a new exercise regime.
If a regime of the same name already exists, exercises will be added to the same scheduling regime.

Format: `add t/regime [n/REGIME_NAME] [i/INDEX]...`
Format: `add t/regime n/REGIME_NAME [i/INDEX]...`

Example:

Expand Down
33 changes: 27 additions & 6 deletions src/main/java/seedu/exercise/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
import seedu.exercise.model.Model;
import seedu.exercise.model.ModelManager;
import seedu.exercise.model.ReadOnlyExerciseBook;
import seedu.exercise.model.ReadOnlyRegimeBook;
import seedu.exercise.model.ReadOnlyUserPrefs;
import seedu.exercise.model.RegimeBook;
import seedu.exercise.model.UserPrefs;
import seedu.exercise.model.util.SampleDataUtil;
import seedu.exercise.storage.ExerciseBookStorage;
import seedu.exercise.storage.JsonExerciseBookStorage;
import seedu.exercise.storage.JsonRegimeBookStorage;
import seedu.exercise.storage.JsonUserPrefsStorage;
import seedu.exercise.storage.RegimeBookStorage;
import seedu.exercise.storage.Storage;
import seedu.exercise.storage.StorageManager;
import seedu.exercise.storage.UserPrefsStorage;
Expand Down Expand Up @@ -57,7 +61,8 @@ public void init() throws Exception {
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
ExerciseBookStorage exerciseBookStorage = new JsonExerciseBookStorage(userPrefs.getExerciseBookFilePath());
storage = new StorageManager(exerciseBookStorage, userPrefsStorage);
RegimeBookStorage regimeBookStorage = new JsonRegimeBookStorage(userPrefs.getRegimeBookFilePath());
storage = new StorageManager(exerciseBookStorage, regimeBookStorage, userPrefsStorage);

initLogging(config);

Expand All @@ -75,22 +80,38 @@ public void init() throws Exception {
*/
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
Optional<ReadOnlyExerciseBook> exerciseBookOptional;
ReadOnlyExerciseBook initialData;
ReadOnlyExerciseBook initialExerciseData;
Optional<ReadOnlyRegimeBook> regimeBookOptional;
ReadOnlyRegimeBook initialRegimeData;
try {
exerciseBookOptional = storage.readExerciseBook();
if (!exerciseBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample ExerciseBook");
}
initialData = exerciseBookOptional.orElseGet(SampleDataUtil::getSampleExerciseBook);
initialExerciseData = exerciseBookOptional.orElseGet(SampleDataUtil::getSampleExerciseBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty ExerciseBook");
initialData = new ExerciseBook();
initialExerciseData = new ExerciseBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty ExerciseBook");
initialData = new ExerciseBook();
initialExerciseData = new ExerciseBook();
}

return new ModelManager(initialData, userPrefs);
try {
regimeBookOptional = storage.readRegimeBook();
if (!regimeBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample RegimeBook");
}
initialRegimeData = regimeBookOptional.orElseGet(SampleDataUtil::getSampleRegimeBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty RegimeBook");
initialRegimeData = new RegimeBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty RegimeBook");
initialRegimeData = new RegimeBook();
}

return new ModelManager(initialExerciseData, initialRegimeData, userPrefs);
}

private void initLogging(Config config) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/exercise/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_EXERCISE_DISPLAYED_INDEX = "The exercise index provided is invalid";
public static final String MESSAGE_INVALID_REGIME_DISPLAYED_INDEX = "The regime index provided is invalid";
public static final String MESSAGE_EXERCISES_LISTED_OVERVIEW = "%1$d exercises listed!";

}
17 changes: 15 additions & 2 deletions src/main/java/seedu/exercise/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import seedu.exercise.logic.commands.exceptions.CommandException;
import seedu.exercise.logic.parser.exceptions.ParseException;
import seedu.exercise.model.ReadOnlyExerciseBook;
import seedu.exercise.model.ReadOnlyRegimeBook;
import seedu.exercise.model.exercise.Exercise;
import seedu.exercise.model.regime.Regime;

/**
* API of the Logic component
Expand All @@ -26,20 +28,31 @@ public interface Logic {
/**
* Returns the ExerciseBook.
*
* @see seedu.exercise.model.Model#getAllData()
* @see seedu.exercise.model.Model#getAllExerciseData()
*/
ReadOnlyExerciseBook getExerciseBook();

/** Returns an unmodifiable view of the filtered list of persons */
ObservableList<Exercise> getFilteredExerciseList();
/**
* Returns the RegimeBook.
*
* @see seedu.exercise.model.Model#getAllRegimeData()
*/
ReadOnlyRegimeBook getRegimeBook();

ObservableList<Exercise> getSortedExerciseList();
ObservableList<Regime> getFilteredRegimeList();

/**
* Returns the user prefs' exercise book file path.
*/
Path getExerciseBookFilePath();

/**
* Returns the user prefs' regime book file path.
*/
Path getRegimeBookFilePath();

/**
* Returns the user prefs' GUI settings.
*/
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/seedu/exercise/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import seedu.exercise.logic.parser.exceptions.ParseException;
import seedu.exercise.model.Model;
import seedu.exercise.model.ReadOnlyExerciseBook;
import seedu.exercise.model.ReadOnlyRegimeBook;
import seedu.exercise.model.exercise.Exercise;
import seedu.exercise.model.regime.Regime;
import seedu.exercise.storage.Storage;

/**
Expand Down Expand Up @@ -43,7 +45,13 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
commandResult = command.execute(model);

try {
storage.saveExerciseBook(model.getAllData());
storage.saveExerciseBook(model.getAllExerciseData());
} catch (IOException ioe) {
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
}

try {
storage.saveRegimeBook(model.getAllRegimeData());
} catch (IOException ioe) {
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
}
Expand All @@ -53,7 +61,7 @@ public CommandResult execute(String commandText) throws CommandException, ParseE

@Override
public ReadOnlyExerciseBook getExerciseBook() {
return model.getAllData();
return model.getAllExerciseData();
}

@Override
Expand All @@ -62,15 +70,25 @@ public ObservableList<Exercise> getFilteredExerciseList() {
}

@Override
public ObservableList<Exercise> getSortedExerciseList() {
return model.getSortedExerciseList();
public ReadOnlyRegimeBook getRegimeBook() {
return model.getAllRegimeData();
}

@Override
public ObservableList<Regime> getFilteredRegimeList() {
return model.getFilteredRegimeList();
}

@Override
public Path getExerciseBookFilePath() {
return model.getExerciseBookFilePath();
}

@Override
public Path getRegimeBookFilePath() {
return model.getRegimeBookFilePath();
}

@Override
public GuiSettings getGuiSettings() {
return model.getGuiSettings();
Expand Down
68 changes: 8 additions & 60 deletions src/main/java/seedu/exercise/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,69 +1,17 @@
package seedu.exercise.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_CALORIES;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_MUSCLE;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_QUANTITY;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_UNIT;

import seedu.exercise.logic.commands.exceptions.CommandException;
import seedu.exercise.model.Model;
import seedu.exercise.model.exercise.Exercise;
import static seedu.exercise.logic.commands.AddExerciseCommand.MESSAGE_USAGE_EXERCISE;
import static seedu.exercise.logic.commands.AddRegimeCommand.MESSAGE_USAGE_REGIME;

/**
* Adds an exercise to the exercise book.
* Represents an AddCommand with hidden internal logic and the ability to be executed.
*/
public class AddCommand extends Command {
public abstract class AddCommand extends Command {

public static final String COMMAND_WORD = "add";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a exercise to the exercise list. "
+ "Parameters: "
+ PREFIX_NAME + "EXERCISE NAME "
+ PREFIX_DATE + "DATE "
+ PREFIX_CALORIES + "CALORIES "
+ PREFIX_QUANTITY + "QUANTITY "
+ PREFIX_UNIT + "UNITS"
+ "[" + PREFIX_MUSCLE + "MUSCLE]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "Run "
+ PREFIX_DATE + "22/09/2019 "
+ PREFIX_CALORIES + "1500 "
+ PREFIX_QUANTITY + "2.4 "
+ PREFIX_UNIT + "km "
+ PREFIX_MUSCLE + "Leg";

public static final String MESSAGE_SUCCESS = "New exercise added: %1$s";
public static final String MESSAGE_DUPLICATE_EXERCISE = "This exercise already exists in the exercise book";

private final Exercise toAdd;

/**
* Creates an AddCommand to add the specified {@code Person}
*/
public AddCommand(Exercise exercise) {
requireNonNull(exercise);
toAdd = exercise;
}

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

if (model.hasExercise(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_EXERCISE);
}

model.addExercise(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AddCommand // instanceof handles nulls
&& toAdd.equals(((AddCommand) other).toAdd));
}
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Adds exercise to exercise list or adds regime to regime list.\n"
+ "EXERCISE: " + MESSAGE_USAGE_EXERCISE + "\n"
+ "REGIME: " + MESSAGE_USAGE_REGIME;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package seedu.exercise.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_CALORIES;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_CATEGORY;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_MUSCLE;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_QUANTITY;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_UNIT;

import seedu.exercise.logic.commands.exceptions.CommandException;
import seedu.exercise.model.Model;
import seedu.exercise.model.exercise.Exercise;

/**
* Adds an exercise to the exercise book.
*/
public class AddExerciseCommand extends AddCommand {

public static final String MESSAGE_USAGE_EXERCISE = "Parameters: "
+ PREFIX_CATEGORY + "CATEGORY "
+ PREFIX_NAME + "EXERCISE NAME "
+ PREFIX_DATE + "DATE "
+ PREFIX_CALORIES + "CALORIES "
+ PREFIX_QUANTITY + "QUANTITY "
+ PREFIX_UNIT + "UNITS "
+ "[" + PREFIX_MUSCLE + "MUSCLE]...\n"
+ "\t\tExample: " + COMMAND_WORD + " "
+ PREFIX_CATEGORY + "exercise"
+ PREFIX_NAME + "Run "
+ PREFIX_DATE + "22/09/2019 "
+ PREFIX_CALORIES + "1500 "
+ PREFIX_QUANTITY + "2.4 "
+ PREFIX_UNIT + "km "
+ PREFIX_MUSCLE + "Leg";

public static final String MESSAGE_SUCCESS = "New exercise added: %1$s";
public static final String MESSAGE_DUPLICATE_EXERCISE = "This exercise already exists in the exercise book";

private Exercise toAdd;

/**
* Creates an AddExerciseCommand to add the specified {@code Exercise}
*/
public AddExerciseCommand(Exercise exercise) {
requireNonNull(exercise);
toAdd = exercise;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
if (model.hasExercise(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_EXERCISE);
}

model.addExercise(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AddExerciseCommand // instanceof handles nulls
&& toAdd.equals(((AddExerciseCommand) other).toAdd));
}
}
Loading

0 comments on commit b44a7ed

Please sign in to comment.