public Recipe(string name, List <string> ingredients, List <string> instructions, RecipeType type) { this.Name = name; this.Ingredients = ingredients; this.Instructions = instructions; this.Type = type; }
public static List <RecipeType> GetRecipeType() { List <RecipeType> recipesTypes = new List <RecipeType>(); string filePath = @"..\..\TextFiles\RecipeTypes.txt"; string line; try { using (StreamReader reader = new StreamReader(filePath)) { while ((line = reader.ReadLine()) != null) { string[] recipeTypeArray = line.Split(','); foreach (string recipeType in recipeTypeArray) { RecipeType type = new RecipeType(recipeType); recipesTypes.Add(type); } } } } catch (Exception ex) { ErrorHandler error = new ErrorHandler(ex); error.LogException(); } return(recipesTypes); }
public static List <Recipe> GetRecipes() { List <Recipe> recipes = new List <Recipe>(); string filePath = @"..\..\TextFiles\Recepies.txt"; string line; try { using (StreamReader reader = new StreamReader(filePath)) { while ((line = reader.ReadLine()) != null) { string[] recipeArray = line.Split('#'); RecipeType type = new RecipeType(recipeArray[3]); Recipe recipe = new Recipe(recipeArray[0], recipeArray[1].Split('-').ToList(), recipeArray[2].Split('-').ToList(), type); recipes.Add(recipe); } } } catch (Exception ex) { ErrorHandler error = new ErrorHandler(ex); error.LogException(); } return(recipes); }
public static void AddRecipeType(RecipeType recipeType) { StringBuilder sbRecipeType = new StringBuilder(); string filePath = @"..\..\TextFiles\RecipeTypes.txt"; string line; bool recpieTypeExist = false; try { using (StreamReader reader = new StreamReader(filePath)) { while ((line = reader.ReadLine()) != null) { string[] recipeTypeArray = line.Split(','); foreach (string type in recipeTypeArray) { if (type == recipeType.Type) { recpieTypeExist = true; break; } else { sbRecipeType.Append(type + ","); } } } } } catch (Exception ex) { ErrorHandler error = new ErrorHandler(ex); error.LogException(); } if (recpieTypeExist) { MessageBox.Show("En recepttype med det namnet finns redan."); } else { try { sbRecipeType.Append(recipeType.Type); using (StreamWriter writer = new StreamWriter(filePath)) { writer.Write(sbRecipeType.ToString()); } } catch (Exception ex) { ErrorHandler error = new ErrorHandler(ex); error.LogException(); } } }
public static void UpdateRecipe(string headline, List <string> ingredients, List <string> instructions, RecipeType type) { StringBuilder sb = new StringBuilder(); List <string> updatedList = new List <string>(); string filePath = @"..\..\TextFiles\Recepies.txt"; string line; try { using (StreamReader reader = new StreamReader(filePath)) { while ((line = reader.ReadLine()) != null) { string[] recipeCheck = line.Split('#'); if (recipeCheck[0] == headline) { sb.Append(headline); sb.Append("#"); foreach (string ingredient in ingredients) { sb.Append(ingredient + "-"); } sb.Append("#"); foreach (string instruction in instructions) { sb.Append(instruction + "-"); } sb.Append(type.Type); updatedList.Add(sb.ToString()); } else { updatedList.Add(line); } } } } catch (Exception ex) { ErrorHandler error = new ErrorHandler(ex); error.LogException(); } try { using (StreamWriter writer = new StreamWriter(filePath)) { foreach (string recipe in updatedList) { writer.WriteLine(recipe); } } } catch (Exception ex) { ErrorHandler error = new ErrorHandler(ex); error.LogException(); } }