Пример #1
0
 //Tar emot en lista med ingredienser, låter användaren lägga till en ingrediens till listan och returnerar sedan den nya listan.
 public static List<Ingredient> AddIngredient(List<Ingredient> ingredients)
 {
     Console.Write("Vad heter ingrediensen du vill lägga till? Tomt namn avslutar. ");
     string name = Console.ReadLine();
     if (string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name))
     {
         Console.Clear();
         return ingredients;
     }
     else
     {
         Console.Write("Hur mycket av {0}? ", name);
         string amount = Console.ReadLine();
         Console.Write("Vilken enhet av mängden? (Exempel dl, l, st). ", name);
         string measure = Console.ReadLine();
         Ingredient ingredient = new Ingredient();
         ingredient.Name = name;
         ingredient.Amount = amount;
         ingredient.Measure = measure;
         ingredients.Add(ingredient);
         Console.Clear();
         ChangeConsoleColor("Ingrediensen har lagts till", ConsoleColor.DarkGreen);
         return ingredients;
     }
 }
Пример #2
0
        //Lägger till ingedienser till en lista. Inmatningen avslutas när en tom rad kommer som input och listan returneras. För varje ingrediens frågas det om namn, mått och mängd.
        //Varje ingrediens nåste minst ha ett namn. Anges inte några ingredienser frågas användaren om den vill avsluta och kan göra det genom att trycka ESC.
        public static List<Ingredient> ReadIngredients()
        {
            List<Ingredient> ingredients = new List<Ingredient>();
            int iterator = 0;
            Console.WriteLine("Ange receptets ingredienser: ");
            while (true)
            {
                iterator++;
                Ingredient ingredient = new Ingredient();
                bool success = false;
                Console.WriteLine("(" + iterator + ")");
                while (success == false)
                {
                    Console.Write("Ingrediens: ");
                    ingredient.Name = Console.ReadLine();

                    if (String.IsNullOrEmpty(ingredient.Name))
                    {
                        Console.WriteLine("Är du säker på att du vill avsluta? J/N?");
                        string input = Console.ReadLine();
                        if (input == "j" && ingredients.Count > 0)
                        {
                            return ingredients;
                        }
                        else if (input == "j" && ingredients.Count <= 0)
                        {
                            ConsoleKeyInfo cki;
                            Console.WriteLine("Du måste ange minst en ingrediens");
                            Console.WriteLine("Tryck en tangent för att fortsätta skiva in ingredienser? ESC avbryter inläsningen av hela receptet");
                            cki = Console.ReadKey();
                            if (cki.Key == ConsoleKey.Escape)
                            {
                                return null;
                            }
                        }
                    }
                    else
                    {
                        success = true;
                    }
                }
                Console.Write("Mängd: ");
                ingredient.Amount = Console.ReadLine();

                Console.Write("Mått: ");
                ingredient.Measure = Console.ReadLine();

                ingredients.Add(ingredient);
            }
        }
 //Läser igenom en textfil. Textfilens sökväg är fältet "_path". För varje rad sätts statusen till antingen "Indefinate", "Ingredient", "Direction" eller "New" beroende på vad raden innehåller.
 //Om statusen är "New" skapas ett nytt Recipe-objekt. Om statusen är "Ingredient" läggs raden till ingredienslistan. Om statusen är "Direction" läggs raden till instruktionslistan.
 //Om statusen är "Indefinate" händer ingen och nästa rad läses in. När statusen ändras till new eller om textfilen är s**t läggs receptet till listan över recept.
 //När textfilen lästs klart sorteras receptlistan och returneras.
 public List<Recipe> Load()
 {
     List<Recipe> recipeList = new List<Recipe>();
     try
     {
         Recipe recipe = null;
         int rowCount = 0;
         RecipeReadStatus status = RecipeReadStatus.Indefinate;
         using (StreamReader sr = new StreamReader(this.Path))
         {
             string line;
             while ((line = sr.ReadLine()) != null)
             {
                 rowCount++;
                 try
                 {
                     if (line == String.Empty)
                     {
                         status = RecipeReadStatus.Indefinate;
                     }
                     else if (line == "[Recept]")
                     {
                         status = RecipeReadStatus.New;
                     }
                     else if (line == "[Ingredienser]")
                     {
                         status = RecipeReadStatus.Ingredient;
                     }
                     else if (line == "[Instruktioner]")
                     {
                         status = RecipeReadStatus.Direction;
                     }
                     else
                     {
                         if (status == RecipeReadStatus.New)
                         {
                             recipe = new Recipe(line);
                             status = RecipeReadStatus.Indefinate;
                         }
                         else if (status == RecipeReadStatus.Ingredient)
                         {
                             string[] ingredientsContainer = line.Split(';');
                             try
                             {
                                 Ingredient ingredient = new Ingredient();
                                 ingredient.Amount = ingredientsContainer[0];
                                 ingredient.Measure = ingredientsContainer[1];
                                 ingredient.Name = ingredientsContainer[2];
                                 recipe.Add(ingredient);
                             }
                             catch
                             {
                                 throw new ArgumentException("Någonting blev fel");
                             }
                         }
                         else if (status == RecipeReadStatus.Direction)
                         {
                             recipe.Add(line);
                         }
                     }
                     if ((status == RecipeReadStatus.New && rowCount != 1) || sr.Peek() < 0)
                     {
                         recipeList.Add(recipe);
                     }
                 }
                 catch
                 {
                     throw new ArgumentException("Någonting blev fel");
                 }
             }
         }
         recipeList.Sort();
         return recipeList;
     }
     catch (Exception e)
     {
         Console.BackgroundColor = ConsoleColor.Red;
         Console.ForegroundColor = ConsoleColor.White;
         Console.WriteLine(e.Message);
         Console.ResetColor();
     }
     return null;
 }