Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Set;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.HashMap;
- /**
- * Write a description of class Fridge here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Fridge {
- private int capacity;
- private List<Ingredient> ingredientsInFridge;
- public Fridge(int capacity) {
- if(capacity < 0){
- throw new IllegalArgumentException("capacity");
- }
- this.capacity = capacity;
- ingredientsInFridge = new ArrayList<Ingredient>(capacity);
- }
- public List<Ingredient> addIngredient(Ingredient ingredient) throws FridgeOverflowException {
- if(capacity < ingredientsInFridge.size()+1) {
- throw new FridgeOverflowException("capacity");
- }
- try {
- ingredientsInFridge.add(ingredient);
- } catch (IllegalArgumentException ex) {
- throw new FridgeOverflowException("capacity");
- }
- return ingredientsInFridge;
- }
- public boolean removeIngredient(Ingredient ingredient){
- if (ingredientsInFridge.contains(ingredient)) {
- ingredientsInFridge.remove(ingredient);
- return true;
- }
- return false;
- }
- public List<Ingredient> getIngredients() {
- return Collections.unmodifiableList(ingredientsInFridge);
- }
- public boolean canWeCookThis(Recipe recipe) {
- return ingredientsInFridge.containsAll(recipe.getIngredients());
- }
- public Set<Recipe> whichOfTheseCanWeCook(Set<Recipe> recipes) {
- Set<Recipe> canCookRecipes = new HashSet<Recipe>();
- for(Recipe recipe : recipes) {
- if(canWeCookThis(recipe)){
- canCookRecipes.add(recipe);
- }
- }
- return canCookRecipes;
- }
- public Set<Recipe> whichOfTheseCANNOTWeCook(Set<Recipe> recipes) {
- recipes.removeAll(whichOfTheseCanWeCook(recipes));
- return recipes;
- }
- public Set<Ingredient> shoplistFor(Recipe recipe) {
- Set<Ingredient> shoppingList = new HashSet<Ingredient>();
- for(Ingredient ingredient : recipe.getIngredients()) {
- if (!ingredientsInFridge.contains(ingredient)){
- shoppingList.add(ingredient);
- }
- }
- return shoppingList;
- }
- public Set<Ingredient> shoplistFor(Set<Recipe> recipes) {
- Set<Ingredient> shoppingList = new HashSet<Ingredient>();
- for(Recipe recipe : recipes) {
- shoppingList.addAll(shoplistFor(recipe));
- }
- return shoppingList;
- }
- public Map<Recipe, Set<Ingredient>> shoplistByRecipe (Set<Recipe> recipes) {
- Map<Recipe, Set<Ingredient>> shoppingList = new HashMap<>();
- for(Recipe recipe : recipes) {
- shoppingList.put(recipe, shoplistFor(recipe));
- }
- return shoppingList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement