//Läser in recept
        public virtual void Load()
        {
            //Skapar lista
            List <IRecipe>   recipes     = new List <IRecipe>();
            RecipeReadStatus status      = RecipeReadStatus.Indefinite;
            Recipe           totalRecipe = null;

            //Ser till så filen stängs när den inte används
            using (StreamReader reader = new StreamReader(_path))
            {
                String line;
                //Loop som läser in varje rad tills filen tar s**t
                while ((line = reader.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        //Switch sats som håller ordning på om det är ingredienser etc..
                        switch (line)
                        {
                        case SectionRecipe:
                            status = RecipeReadStatus.New;
                            break;

                        case SectionIngredients:
                            status = RecipeReadStatus.Ingredient;
                            break;

                        case SectionInstructions:
                            status = RecipeReadStatus.Instruction;
                            break;

                        case "":
                            break;

                        default:
                            //Switch sats som gör olika saker med raden beroende på om den var instruktioner etc..
                            switch (status)
                            {
                            case RecipeReadStatus.New:
                                totalRecipe = new Recipe(line);
                                recipes.Add(totalRecipe);
                                break;

                            case RecipeReadStatus.Ingredient:
                                //Delar upp ingrediens raden i Amount, Measure och Name
                                string[] ingredients = line.Split(new string[] { ";" }, StringSplitOptions.None);
                                if (ingredients.Length != 3)
                                {
                                    throw new FileFormatException();
                                }
                                Ingredient ingredient = new Ingredient();
                                ingredient.Amount  = ingredients[0];
                                ingredient.Measure = ingredients[1];
                                ingredient.Name    = ingredients[2];
                                totalRecipe.Add(ingredient);
                                break;

                            case RecipeReadStatus.Instruction:
                                totalRecipe.Add(line);
                                break;

                            case RecipeReadStatus.Indefinite:
                                throw new FileFormatException();

                            default:
                                break;
                            }
                            break;
                        }
                    }
                }
            }
            //Sorterar recepten efter namnet
            _recipes   = recipes.OrderBy(recipe => recipe.Name).ToList();
            IsModified = false;
            OnRecipesChanged(EventArgs.Empty);
        }
Пример #2
0
        public void Load()
        {
            //Skapa lista som kan innehålla referencer till receptobjekt
            List <IRecipe>   listRecipes  = new List <IRecipe>();
            RecipeReadStatus recipeStatus = new RecipeReadStatus();

            //Öppna textfilen för läsning
            using (StreamReader reader = new StreamReader(_path))
            {
                Recipe myRecipe = null;
                string line;
                //Läs rad från textfilen tills det är s**t på filen
                while ((line = reader.ReadLine()) != null)
                {
                    //Om det är en tom rad fortsätt med att läsa in nästa rad
                    if (line.Length == 0)
                    {
                        continue;
                    }
                    //om det är en avdelning för nytt recept
                    if (line == SectionRecipe)
                    {
                        recipeStatus = RecipeReadStatus.New;
                    }
                    //eller om det är avdelning för ingridienser
                    else if (line == SectionIngredients)
                    {
                        recipeStatus = RecipeReadStatus.Ingredient;
                    }
                    //eller om det är avdelningen för instruktioner
                    else if (line == SectionInstructions)
                    {
                        recipeStatus = RecipeReadStatus.Instruction;
                    }
                    //annars är det ett namn, en ingridiens eller en instruktion
                    else
                    {
                        if (recipeStatus == RecipeReadStatus.New)
                        {
                            myRecipe = new Recipe(line);
                            listRecipes.Add(myRecipe);
                        }
                        //eller om status är satt att raden ska tolkas som ett recepts namn
                        else if (recipeStatus == RecipeReadStatus.Ingredient)
                        {
                            string[] values = line.Split(';');
                            //Om antalet delar inte är tre
                            if (values.Length != 3)
                            {
                                throw new FileFormatException();
                            }
                            //Skapa ett ingridientsobjekt och initiera det med de tre delarna för mängd, mått och namn
                            Ingredient ingredient = new Ingredient();
                            ingredient.Amount  = values[0];
                            ingredient.Measure = values[1];
                            ingredient.Name    = values[2];

                            //Lägg till ingridiensen till receptets lista med ingridienser
                            myRecipe.Add(ingredient);
                        }
                        //eller om status är satt att raden ska tolkas som en instruktion
                        else if (recipeStatus == RecipeReadStatus.Instruction)
                        {
                            myRecipe.Add(line);
                        }
                        else
                        {
                            throw new FileFormatException();
                        }
                    }
                }
                //Sortera listan med recept med avseende på receptens namn
                _recipes = listRecipes.OrderBy(sort => sort.Name).ToList();

                //Tilldela avsedd egenskap i klassen, IsModified, ett värde som indikera att listan med recept är oförändrad
                IsModified = false;

                //Utlös händelse om att recept har läst in genom att anropa metoden OnRecipesChanged och skicka med parametern EventArgs.Empty
                OnRecipesChanged(EventArgs.Empty);
            }
        }