00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using Foodolini.Database;
00006
00007 namespace Foodolini.BusinessLogic
00008 {
00012 public class ShoppingList
00013 {
00017 private Person owner;
00018
00023 public ShoppingList(Person person)
00024 {
00025 this.owner = person;
00026
00027 GetShoppingListItems();
00028 }
00029
00033 private List<ShoppingListItem> shoppingList;
00034
00041 public IEnumerable<ShoppingListItem> GetShoppingListItems()
00042 {
00043
00044 if (shoppingList == null) {
00045 shoppingList = new List<ShoppingListItem>(ShoppingListItem.GetShoppingListItems(this.owner));
00046 }
00047 return shoppingList;
00048 }
00049
00056 public bool AddIngredient(Ingredient ingredient, double quantity)
00057 {
00058 bool added = false;
00059 var shoppingItem = this.shoppingList.Find(item => Ingredient.Compare(item.Ingredient,ingredient));
00060 if (shoppingItem == null) {
00061 added = true;
00062 this.shoppingList.Add(new ShoppingListItem(ingredient, quantity, this.owner));
00063 } else{
00064 shoppingItem.Quantity += quantity;
00065 }
00066 return added;
00067 }
00068
00074 public void AddRecipe(Recipe recipe, double servings)
00075 {
00076 foreach (KeyValuePair<Ingredient, double> item in recipe.Ingredients)
00077 this.AddIngredient(item.Key, item.Value*servings);
00078 }
00079
00089 public bool ContainsIngredient(Ingredient ingredient){
00090 return this.shoppingList.Find(item => Ingredient.Compare(item.Ingredient,ingredient)) != null;
00091 }
00092
00097 public void RemoveIngredient(Ingredient ingredient)
00098 {
00099 ShoppingListItem toRemove = this.shoppingList.Find(item => item.Ingredient == ingredient);
00100 RemoveItem(toRemove);
00101 }
00102
00107 private void RemoveItem(ShoppingListItem item)
00108 {
00109 if (item != null) {
00110
00111 item.Delete();
00112
00113 this.shoppingList.Remove(item);
00114 }
00115 }
00116
00121 public void RemoveRecipe(Recipe recipe)
00122 {
00123
00124
00125 ShoppingListItem toRemove = this.shoppingList.Find(item => item.Recipe.Id == recipe.Id);
00126 RemoveItem(toRemove);
00127 }
00128
00132 public void Save()
00133 {
00134
00135 foreach (ShoppingListItem item in this.shoppingList)
00136 item.Save();
00137 }
00138
00142 public void Clear()
00143 {
00144 foreach (ShoppingListItem item in this.shoppingList)
00145 item.Delete();
00146
00147 this.shoppingList = new List<ShoppingListItem>();
00148 }
00149 }
00150 }