forked from nus-cs2103-AY1920S2/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from tharshita/master
Cooked command
- Loading branch information
Showing
44 changed files
with
1,171 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/seedu/recipe/logic/commands/CookedCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/seedu/recipe/logic/parser/CookedCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.