Example #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
            }
        }
        /// <summary>
        /// Load recipes.
        /// Recipes are loaded from the textfile recipes.txt.
        /// When the user selects "1. Open" in the menu, the application will open the textfile, read and translate
        /// it row by row to create a list of reipes which the user is going to be able to select through the menu.
        /// </summary>
        public virtual void Load()
        {
            RecipeReadStatus status     = RecipeReadStatus.Indefinite;
            List <IRecipe>   recipeList = new List <IRecipe>();     //Create a new list

            using (StreamReader reader = new StreamReader(_path))   //Use a Streamreader object to read from file
            {
                string line;
                while ((line = reader.ReadLine()) != null) //Loop through the rows of the text until the end of file
                {
                    if (line == "")                        //If row is empty, continue to next row.
                    {
                        continue;
                    }
                    if (line == SectionRecipe)      //If line is equal to the value of SectionRecipe, create e new Recipe for the next line.
                    {
                        status = RecipeReadStatus.New;
                    }
                    else if (line == SectionIngredients)       //If line is equal to the vlue of SectionIngredients, create a new ingredient from next line.
                    {
                        status = RecipeReadStatus.Ingredient;
                    }
                    else if (line == SectionInstructions)      //If line is equal to the value of SectionInstructions, create new instruction from next line.
                    {
                        status = RecipeReadStatus.Instruction;
                    }
                    else
                    {
                        if (status == RecipeReadStatus.New)
                        {
                            recipeList.Add(new Recipe(line));       //new recipe object is added to the list of recipes.
                        }
                        else if (status == RecipeReadStatus.Ingredient)
                        {
                            string[] parts = line.Split(new char[] { ';' });    //Divide the line into 3 sections by using the method Split() in the String class.
                            if (parts.Length != 3)
                            {
                                throw new FileFormatException();    //If the number of sections are not three, throw new FileFormatException.
                            }
                            Ingredient newIngredient = new Ingredient();
                            newIngredient.Amount  = parts[0];
                            newIngredient.Measure = parts[1];
                            newIngredient.Name    = parts[2];
                            //Create an ingredient object and initiate it with the three sections for Measure, Amount and Name.
                            recipeList.Last().Add(newIngredient);   //Add the ingredient to the recipes' list of ingredients.
                        }
                        else if (status == RecipeReadStatus.Instruction)
                        {
                            recipeList.Last().Add(line);            //Add the line to the recipes' list of instructions.
                        }
                        else
                        {
                            throw new FileFormatException();        //If else - something is wrong and a new exceptoin of the type FileFormatException will be thrown.
                        }
                    }
                }
                recipeList.TrimExcess();
                recipeList.Sort();
                //Sort the list with recipes according to the Names of the recipes.
                _recipes = recipeList;
                //Assign the field _recipes, in the class, a refrence to the list.
                IsModified = false;
                //Assign the property IsModified, in the class, a value which indicates that the list of recipes is unchanged.
                OnRecipesChanged(EventArgs.Empty);
                //Advertise that the recipes has been loaded by calling the method OnRecipesChanged and send it the parameter EventArgs.Empty.
            }
        }
        public virtual void Load()
        {
            List<IRecipe> recipesList = new List<IRecipe>();
            RecipeReadStatus status = new RecipeReadStatus();
            Recipe recipe = null;

            using (StreamReader loadedRecipe = new StreamReader(_path))
            {
                string line;
                while ((line = loadedRecipe.ReadLine()) != null)
                {
                    if (line == SectionRecipe)
                    {
                        status = RecipeReadStatus.New;
                    }
                    else if (line == SectionIngredients)
                    {
                        status = RecipeReadStatus.Ingredient;
                    }
                    else if (line == SectionInstructions)
                    {
                        status = RecipeReadStatus.Instruction;
                    }
                    else
                    {
                        switch (status)
                            {
                                case RecipeReadStatus.Indefinite:
                                    throw new FileFormatException();
                                case RecipeReadStatus.New:
                                    recipe = new Recipe(line);
                                    recipesList.Add(recipe);
                                    break;
                                case RecipeReadStatus.Ingredient:
                                    string[] ingredientArray = line.Split(new char[] { ';' } );
                                    if (ingredientArray.Length != 3)
                                    {
                                        throw new FileFormatException();
                                    }
                                    Ingredient ingredient = new Ingredient();
                                    ingredient.Amount = ingredientArray[0];
                                    ingredient.Measure = ingredientArray[1];
                                    ingredient.Name = ingredientArray[2];
                                    recipe.Add(ingredient);
                                    break;
                                case RecipeReadStatus.Instruction:
                                    if (line.Length > 0)
                                    {
                                        recipe.Add(line);
                                    }
                                    break;
                            }
                    }
                    recipesList.Sort();
                    _recipes = recipesList;
                    IsModified = false;
                    OnRecipesChanged(EventArgs.Empty);
                }
            }
        }
        public void Load()
        {
            // 1. Lista med refrens till receptobjekt.
            List <IRecipe> listRecipes = new List <IRecipe>();

            // 2. Öppnar textfilen för läsning.
            using (StreamReader reader = new StreamReader(_path))
            {
                string           line;
                RecipeReadStatus lineType = RecipeReadStatus.Indefinite;

                while ((line = reader.ReadLine()) != null)
                {
                    if (line == string.Empty)
                    {
                        continue;
                    }

                    if (line == SectionRecipe)
                    {
                        lineType = RecipeReadStatus.New;
                    }
                    else if (line == SectionIngredients)
                    {
                        lineType = RecipeReadStatus.Ingredient;
                    }
                    else if (line == SectionInstructions)
                    {
                        lineType = RecipeReadStatus.Instruction;
                    }
                    else
                    {
                        if (lineType == RecipeReadStatus.New)
                        {
                            // Skapar nytt Recipeobjekt.
                            Recipe myRecipe = new Recipe(line);
                            listRecipes.Add(myRecipe);
                        }
                        else if (lineType == RecipeReadStatus.Ingredient)
                        {
                            string[] parts = line.Split(';');

                            // Om antalet delar är 3.
                            if (parts.Length == 3)
                            {
                                // Skapar ett ingrediensobjekt och initierar med de tre delarna för mängd, mått och namn.
                                Ingredient myIngridient = new Ingredient
                                {
                                    Amount  = parts[0],
                                    Measure = parts[1],
                                    Name    = parts[2]
                                };

                                // Lägg till ingrediensen till receptets lista med ingredienser.
                                listRecipes.Last().Add(myIngridient);
                            }
                            // Om antalet delar är något annat än 3.
                            else
                            {
                                throw new FileFormatException();
                            }
                        }
                        // Läser instruktioner.
                        else if (lineType == RecipeReadStatus.Instruction)
                        {
                            listRecipes.Last().Add(line);
                        }
                        else
                        {
                            throw new FileFormatException();
                        }
                    }
                }
            }

            listRecipes.TrimExcess();

            // Sorterar listan med avsikt på receptets namn.
            listRecipes.Sort();

            //listRecipes.OrderBy(listRecipe => listRecipe.Name);

            // Tildela avsett fält i klassen, _recipes, en refrens i listan.
            _recipes = listRecipes;

            IsModified = false;

            OnRecipesChanged(EventArgs.Empty);
        }
        //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);
        }
Example #6
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);
            }
        }
Example #7
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);

            }
        }