Skip to content

Commit

Permalink
Merge pull request #107 from tharshita/master
Browse files Browse the repository at this point in the history
Cooked command
  • Loading branch information
beatricetay authored Mar 31, 2020
2 parents 87eb853 + e475550 commit fed6f24
Show file tree
Hide file tree
Showing 44 changed files with 1,171 additions and 97 deletions.
27 changes: 27 additions & 0 deletions data/cookedRecords.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"records" : [ {
"name" : "Chinese Tomato Egg Stir-fry"
}, {
"name" : "5 Ingredient Spelt Spaghetti"
}, {
"name" : "Bacon, Spinach Caramelized Onion Pasta"
}, {
"name" : "Healthy Chicken Salad Sandwich"
}, {
"name" : "All Veggie Sandwich"
}, {
"name" : "Summer Asian Slaw"
}, {
"name" : "Avocado Couscous Grapefruit salad with honey lime dressing"
}, {
"name" : "testMissingIngredient"
}, {
"name" : "Vegan Thai Green Curry Soup"
}, {
"name" : "Thai Basil Ground Beef Bowl"
}, {
"name" : "Barley Pilaf"
}, {
"name" : "Asian BBQ Chicken"
} ]
}
31 changes: 28 additions & 3 deletions src/main/java/seedu/recipe/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
import seedu.recipe.logic.LogicManager;
import seedu.recipe.model.Model;
import seedu.recipe.model.ModelManager;
import seedu.recipe.model.ReadOnlyCookedRecordBook;
import seedu.recipe.model.ReadOnlyRecipeBook;
import seedu.recipe.model.ReadOnlyUserPrefs;
import seedu.recipe.model.RecipeBook;
import seedu.recipe.model.UserPrefs;
import seedu.recipe.model.cooked.CookedRecordBook;
import seedu.recipe.model.plan.PlannedBook;
import seedu.recipe.model.plan.ReadOnlyPlannedBook;
import seedu.recipe.model.util.SampleDataUtil;
import seedu.recipe.storage.CookedRecordBookStorage;
import seedu.recipe.storage.JsonCookedRecordBookStorage;
import seedu.recipe.storage.JsonRecipeBookStorage;
import seedu.recipe.storage.JsonUserPrefsStorage;
import seedu.recipe.storage.RecipeBookStorage;
Expand Down Expand Up @@ -61,8 +65,10 @@ public void init() throws Exception {
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
RecipeBookStorage recipeBookStorage = new JsonRecipeBookStorage(userPrefs.getRecipeBookFilePath());
CookedRecordBookStorage cookedRecordBookStorage = new JsonCookedRecordBookStorage(
userPrefs.getCookedRecordFilePath());
PlannedBookStorage plannedBookStorage = new JsonPlannedBookStorage(userPrefs.getPlannedBookFilePath());
storage = new StorageManager(recipeBookStorage, plannedBookStorage, userPrefsStorage);
storage = new StorageManager(recipeBookStorage, cookedRecordBookStorage, plannedBookStorage, userPrefsStorage);

initLogging(config);

Expand All @@ -81,8 +87,11 @@ public void init() throws Exception {
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
Optional<ReadOnlyRecipeBook> recipeBookOptional;
ReadOnlyRecipeBook initialData;
Optional<ReadOnlyCookedRecordBook> recordBookOptional;
ReadOnlyCookedRecordBook initialRecords;
Optional<ReadOnlyPlannedBook> plannedBookOptional;
ReadOnlyPlannedBook initialPlannedData;

try {
recipeBookOptional = storage.readRecipeBook();
if (!recipeBookOptional.isPresent()) {
Expand All @@ -94,10 +103,26 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
logger.warning("Data file not in the correct format. Will be starting with an empty RecipeBook");
initialData = new RecipeBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty RecipeBook");
logger.warning("Problem while reading from the file for recipes. "
+ "Will be starting with an empty RecipeBook");
initialData = new RecipeBook();
}

try {
recordBookOptional = storage.readCookedRecordBook();
if (!recordBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample Recordbook");
}
initialRecords = recordBookOptional.orElseGet(SampleDataUtil::getSampleRecordBook);

} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty Recordbook");
initialRecords = new CookedRecordBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty Recordbook");
initialRecords = new CookedRecordBook();
}

try {
plannedBookOptional = storage.readPlannedBook();
if (!plannedBookOptional.isPresent()) {
Expand All @@ -114,7 +139,7 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
initialPlannedData = new PlannedBook();
}

return new ModelManager(initialData, initialPlannedData, userPrefs);
return new ModelManager(initialData, userPrefs, initialRecords, initialPlannedData);
}

private void initLogging(Config config) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/recipe/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.recipe.logic.commands.exceptions.CommandException;
import seedu.recipe.logic.parser.exceptions.ParseException;
import seedu.recipe.model.ReadOnlyRecipeBook;
import seedu.recipe.model.cooked.Record;
import seedu.recipe.model.plan.PlannedRecipe;
import seedu.recipe.model.recipe.Recipe;

Expand Down Expand Up @@ -49,6 +50,12 @@ public interface Logic {
*/
void setGuiSettings(GuiSettings guiSettings);

/**
* Returns an unmodifiable view of cooked records
* @return list
*/
ObservableList<Record> getFilteredRecordList();

/**
* Returns an unmodifiable view of the scheduled recipes.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/recipe/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.recipe.logic.parser.exceptions.ParseException;
import seedu.recipe.model.Model;
import seedu.recipe.model.ReadOnlyRecipeBook;
import seedu.recipe.model.cooked.Record;
import seedu.recipe.model.plan.PlannedRecipe;
import seedu.recipe.model.recipe.Recipe;
import seedu.recipe.storage.Storage;
Expand Down Expand Up @@ -44,7 +45,9 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
commandResult = command.execute(model);
try {
storage.saveRecipeBook(model.getRecipeBook());
storage.saveCookedRecordBook(model.getRecordBook());
storage.savePlannedBook(model.getPlannedBook());

} catch (IOException ioe) {
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
}
Expand Down Expand Up @@ -77,9 +80,15 @@ public void setGuiSettings(GuiSettings guiSettings) {
model.setGuiSettings(guiSettings);
}

@Override
public ObservableList<Record> getFilteredRecordList() {
return model.getFilteredRecordList();
}

@Override
public ObservableList<PlannedRecipe> getFilteredPlannedList() {
return model.getFilteredPlannedList();
}


}
70 changes: 70 additions & 0 deletions src/main/java/seedu/recipe/logic/commands/CookedCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package seedu.recipe.logic.commands;
import static java.util.Objects.requireNonNull;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import seedu.recipe.commons.core.Messages;
import seedu.recipe.commons.core.index.Index;
import seedu.recipe.logic.commands.exceptions.CommandException;
import seedu.recipe.model.Model;
import seedu.recipe.model.cooked.Record;
import seedu.recipe.model.recipe.Recipe;

/**
* Adds cooked recipes identified by index into cookedRecordsBook.
*/
public class CookedCommand extends Command {
public static final String COMMAND_WORD = "cooked";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Indicate that the recipe has been cooked at the current time\n"
+ "Parameters: INDEX NUMBER(s) (must be positive integers)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_DUPLICATE_RECORD = "This recipe has already been added!";

private final Index[] targetIndex;

public CookedCommand(Index[] targetIndex) {
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Recipe> mostRecentList = model.getFilteredRecipeList();
StringBuilder sb = new StringBuilder().append("Cooked ");

for (int i = 0; i < targetIndex.length; i++) {
if (targetIndex[i].getZeroBased() >= mostRecentList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_RECIPE_DISPLAYED_INDEX);
}
Recipe recipeCooked = mostRecentList.get(targetIndex[i].getZeroBased());
Date now = new Date();
Record record = new Record(recipeCooked.getName());
if (model.hasRecord(record)) {
throw new CommandException(MESSAGE_DUPLICATE_RECORD);
}
model.addRecord(record);
if (i == targetIndex.length - 1 && targetIndex.length != 1) {
sb.append(" and ");
}
sb.append(recipeCooked.getName().toString());
if (i < targetIndex.length - 2) {
sb.append(", ");
}
}
sb.append("!");
return new CommandResult(sb.toString());
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof CookedCommand // instanceof handles nulls
&& Arrays.equals(targetIndex, ((CookedCommand) other).targetIndex)); // state check
}

}
6 changes: 3 additions & 3 deletions src/main/java/seedu/recipe/logic/commands/PlanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import seedu.recipe.commons.core.Messages;
import seedu.recipe.commons.core.index.Index;
import seedu.recipe.logic.commands.exceptions.CommandException;
import seedu.recipe.model.Date;
import seedu.recipe.model.Model;
import seedu.recipe.model.plan.PlannedDate;
import seedu.recipe.model.plan.PlannedRecipe;
import seedu.recipe.model.recipe.Recipe;

Expand All @@ -33,12 +33,12 @@ public class PlanCommand extends Command {
public static final String MESSAGE_SUCCESS = "Recipe planned: %1$s, %2$s";

private final Index index;
private final PlannedDate atDate;
private final Date atDate;

/**
* Creates an PlanCommand to set the specified {@code Recipe} on a certain date
*/
public PlanCommand(Index index, PlannedDate date) {
public PlanCommand(Index index, Date date) {
requireNonNull(index);
requireNonNull(date);
this.index = index;
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/seedu/recipe/logic/parser/CookedCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.recipe.logic.parser;

import static seedu.recipe.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.recipe.commons.core.index.Index;
import seedu.recipe.logic.commands.CookedCommand;
import seedu.recipe.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new CookedCommand object
*/
public class CookedCommandParser implements Parser<CookedCommand> {

/**
* Parses the given {@code String} of arguments in the context of the CookedCommand
* and returns a CookedCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public CookedCommand parse(String args) throws ParseException {
try {
Index[] index = ParserUtil.parseMultipleIndex(args);
return new CookedCommand(index);
} catch (ParseException exception) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, CookedCommand.MESSAGE_USAGE), exception);
}
}

}
13 changes: 6 additions & 7 deletions src/main/java/seedu/recipe/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
import seedu.recipe.commons.core.index.Index;
import seedu.recipe.commons.util.StringUtil;
import seedu.recipe.logic.parser.exceptions.ParseException;
import seedu.recipe.model.Date;
import seedu.recipe.model.goal.Goal;
import seedu.recipe.model.plan.PlannedDate;
import seedu.recipe.model.recipe.Name;
import seedu.recipe.model.recipe.Step;
import seedu.recipe.model.recipe.Time;

import seedu.recipe.model.recipe.ingredient.Fruit;
import seedu.recipe.model.recipe.ingredient.Grain;
import seedu.recipe.model.recipe.ingredient.Ingredient;
Expand Down Expand Up @@ -514,16 +513,16 @@ public static Set<Other> parseOthersNameOnly(Collection<String> others) throws P
}

/**
* Parses {@code String date} into a {@code PlannedDate}.
* Parses {@code String date} into a {@code Date}.
*
* @throws ParseException if the given {@code date} is invalid.
*/
public static PlannedDate parseDate(String date) throws ParseException {
public static Date parseDate(String date) throws ParseException {
requireNonNull(date);
String trimmedDate = date.trim();
if (!PlannedDate.isValidDate(trimmedDate)) {
throw new ParseException(PlannedDate.MESSAGE_CONSTRAINTS);
if (!Date.isValidDate(trimmedDate)) {
throw new ParseException(Date.MESSAGE_CONSTRAINTS);
}
return new PlannedDate(trimmedDate);
return new Date(trimmedDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import seedu.recipe.commons.core.index.Index;
import seedu.recipe.logic.commands.PlanCommand;
import seedu.recipe.logic.parser.exceptions.ParseException;
import seedu.recipe.model.plan.PlannedDate;
import seedu.recipe.model.Date;

/**
* Parses input arguments and creates a new PlanCommand object
Expand All @@ -35,7 +35,7 @@ public PlanCommand parse(String args) throws ParseException {
if (!arePrefixesPresent(argMultimap, PREFIX_DATE)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, PlanCommand.MESSAGE_USAGE));
}
PlannedDate date = ParserUtil.parseDate(argMultimap.getValue(PREFIX_DATE).get());
Date date = ParserUtil.parseDate(argMultimap.getValue(PREFIX_DATE).get());

return new PlanCommand(index, date);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/recipe/logic/parser/RecipeBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.recipe.logic.commands.AddStepCommand;
import seedu.recipe.logic.commands.ClearCommand;
import seedu.recipe.logic.commands.Command;
import seedu.recipe.logic.commands.CookedCommand;
import seedu.recipe.logic.commands.DeleteCommand;
import seedu.recipe.logic.commands.DeleteIngredientCommand;
import seedu.recipe.logic.commands.DeleteStepCommand;
Expand Down Expand Up @@ -85,6 +86,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ListGoalsCommand.COMMAND_WORD:
return new ListGoalsCommand();

case CookedCommand.COMMAND_WORD:
return new CookedCommandParser().parse(arguments);

case FavouriteCommand.COMMAND_WORD:
return new FavouriteCommandParser().parse(arguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import seedu.recipe.logic.commands.ViewCommand;
import seedu.recipe.logic.parser.exceptions.ParseException;
import seedu.recipe.model.plan.PlannedDate;
import seedu.recipe.model.Date;
import seedu.recipe.model.plan.PlannedRecipeWithinDateRangePredicate;

/**
Expand All @@ -27,7 +27,7 @@ public ViewCommand parse(String args) throws ParseException {
}

PlannedRecipeWithinDateRangePredicate predicate = new PlannedRecipeWithinDateRangePredicate(
new PlannedDate(LocalDate.parse("2020-03-30")), new PlannedDate(LocalDate.parse("2020-04-06")));
new Date(LocalDate.parse("2020-03-30")), new Date(LocalDate.parse("2020-04-06")));
// model.updateFilteredRecipeList(predicate);
return new ViewCommand(predicate);
}
Expand Down
Loading

0 comments on commit fed6f24

Please sign in to comment.