Пример #1
0
        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();
            }
        }
Пример #2
0
        public static void AddRecipe(Recipe recipe)
        {
            string filePath = @"..\..\TextFiles\Recepies.txt";
            string line;
            bool   recpieExist = false;

            try
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] recipeCheck = line.Split('#');

                        if (recipeCheck[0] == recipe.Name)
                        {
                            recpieExist = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler error = new ErrorHandler(ex);
                error.LogException();
            }

            if (recpieExist)
            {
                MessageBox.Show("Ett recept med det namnet finns redan.");
            }
            else
            {
                try
                {
                    using (StreamWriter writer = new StreamWriter(filePath, true))
                    {
                        writer.WriteLine();
                        writer.Write(recipe.Name);

                        writer.Write("#");
                        foreach (string ingredient in recipe.Ingredients)
                        {
                            writer.Write(ingredient + "-");
                        }

                        writer.Write("#");
                        foreach (string instruction in recipe.Instructions)
                        {
                            writer.Write(instruction + "-");
                        }

                        writer.Write("#" + recipe.Type.Type);
                    }
                }
                catch (Exception ex)
                {
                    ErrorHandler error = new ErrorHandler(ex);
                    error.LogException();
                }
            }
        }