Exemplo n.º 1
0
        public void Load()
        {
            List <IRecipe> recipes = new List <IRecipe>();

            using (StreamReader read = new StreamReader(_path)) // instansierar nytt streamreader objekt.
            {
                string           reader;
                Recipe           FullRecipe = null;
                RecipeReadStatus RecipeEnum = RecipeReadStatus.Indefinite; // ett enum, Indefinite, New, Ingredient, Instruction


                while ((reader = read.ReadLine()) != null)
                {
                    if (reader != "") // kollar om tomt
                    {
                        if (reader == SectionRecipe)
                        {
                            RecipeEnum = RecipeReadStatus.New;
                        }

                        else if (reader == SectionIngredients)
                        {
                            RecipeEnum = RecipeReadStatus.Ingredient;
                        }

                        else if (reader == SectionInstructions)
                        {
                            RecipeEnum = RecipeReadStatus.Instruction;
                        }


                        else
                        {
                            switch (RecipeEnum)
                            {
                            case RecipeReadStatus.New:

                                FullRecipe = new Recipe(reader);                    // skapar ett nytt objekt varje gång den läser in
                                recipes.Add(FullRecipe);                            // ett nytt recept namn.
                                break;

                            case RecipeReadStatus.Ingredient:

                                string[] ingredients = reader.Split(new char[] { ';' }, StringSplitOptions.None);     // tar bort comman frå n den inlästa strängen

                                if (ingredients.Length % 3 != 0)
                                {
                                    throw new FileFormatException();
                                }

                                Ingredient ingredient = new Ingredient();
                                ingredient.Amount  = ingredients[0];               //1:a värdet i arrayen = mängden
                                ingredient.Measure = ingredients[1];               //2:a värdet i arrayen = måttet
                                ingredient.Name    = ingredients[2];               //3:e värdet i arrayen = namnet

                                FullRecipe.Add(ingredient);
                                break;


                            case RecipeReadStatus.Instruction:

                                FullRecipe.Add(reader);
                                break;

                            case RecipeReadStatus.Indefinite:
                                throw new FileFormatException();
                            }
                        }
                    }
                }
                recipes.TrimExcess();

                _recipes = recipes.OrderBy(recipe => recipe.Name).ToList();

                IsModified = false;

                OnRecipesChanged(EventArgs.Empty); //Utlöser händelse om att recept har lästs in
            }
        }
        //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);
        }