Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Set;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.HashMap;
  8.  
  9. /**
  10.  * Write a description of class Fridge here.
  11.  *
  12.  * @author (your name)
  13.  * @version (a version number or a date)
  14.  */
  15. public class Fridge {
  16.     private int capacity;
  17.     private List<Ingredient> ingredientsInFridge;
  18.  
  19.     public Fridge(int capacity) {
  20.         if(capacity < 0){
  21.             throw new IllegalArgumentException("capacity");
  22.         }
  23.         this.capacity = capacity;
  24.         ingredientsInFridge = new ArrayList<Ingredient>(capacity);
  25.     }
  26.  
  27.     public List<Ingredient> addIngredient(Ingredient ingredient) throws FridgeOverflowException {
  28.         if(capacity < ingredientsInFridge.size()+1) {
  29.             throw new FridgeOverflowException("capacity");
  30.         }
  31.         try  {
  32.             ingredientsInFridge.add(ingredient);
  33.         } catch (IllegalArgumentException ex)  {
  34.             throw new FridgeOverflowException("capacity");
  35.         }
  36.         return ingredientsInFridge;
  37.     }
  38.  
  39.     public boolean removeIngredient(Ingredient ingredient){
  40.         if (ingredientsInFridge.contains(ingredient)) {
  41.             ingredientsInFridge.remove(ingredient);
  42.             return true;
  43.         }
  44.         return false;
  45.     }
  46.  
  47.     public List<Ingredient> getIngredients() {
  48.         return Collections.unmodifiableList(ingredientsInFridge);
  49.     }
  50.  
  51.     public boolean canWeCookThis(Recipe recipe) {
  52.         return ingredientsInFridge.containsAll(recipe.getIngredients());
  53.     }
  54.  
  55.     public Set<Recipe> whichOfTheseCanWeCook(Set<Recipe> recipes) {
  56.         Set<Recipe> canCookRecipes = new HashSet<Recipe>();
  57.         for(Recipe recipe : recipes) {
  58.             if(canWeCookThis(recipe)){
  59.                 canCookRecipes.add(recipe);
  60.             }
  61.         }
  62.         return canCookRecipes;
  63.     }
  64.  
  65.     public Set<Recipe> whichOfTheseCANNOTWeCook(Set<Recipe> recipes) {
  66.        recipes.removeAll(whichOfTheseCanWeCook(recipes));
  67.        return recipes;
  68.     }
  69.    
  70.     public Set<Ingredient> shoplistFor(Recipe recipe) {
  71.         Set<Ingredient> shoppingList = new HashSet<Ingredient>();
  72.         for(Ingredient ingredient : recipe.getIngredients()) {
  73.             if (!ingredientsInFridge.contains(ingredient)){
  74.                 shoppingList.add(ingredient);
  75.             }
  76.           }
  77.         return shoppingList;      
  78.     }
  79.    
  80.     public Set<Ingredient> shoplistFor(Set<Recipe> recipes) {
  81.         Set<Ingredient> shoppingList = new HashSet<Ingredient>();
  82.         for(Recipe recipe : recipes) {
  83.             shoppingList.addAll(shoplistFor(recipe));
  84.         }
  85.         return shoppingList;
  86.     }
  87.    
  88.     public Map<Recipe, Set<Ingredient>> shoplistByRecipe (Set<Recipe> recipes) {
  89.         Map<Recipe, Set<Ingredient>> shoppingList = new HashMap<>();
  90.         for(Recipe recipe : recipes) {
  91.             shoppingList.put(recipe, shoplistFor(recipe));
  92.         }
  93.         return shoppingList;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement