Esempio n. 1
0
        /// <summary>
        /// Reads the ingredients for a recipe
        /// </summary>
        /// <param name="ingredients">A default set of ingredients, defaults to null</param>
        /// <returns>The ingredients or null if aborted</returns>
        private static List<Ingredient> ReadIngredients(List<Ingredient> ingredients = null)
        {
            if (ingredients == null) {
                ingredients = new List<Ingredient>();
            }
            Console.WriteLine("Ange receptets ingredienser - tom rad för namnet avslutar: ");

            do {
                Ingredient ingredient = new Ingredient();
                Console.WriteLine("{0}.", ingredients.Count+1);

                Console.Write("Namn:");
                string name = Console.ReadLine();
                if (String.IsNullOrEmpty(name)) {

                    Console.BackgroundColor = ConsoleColor.Yellow;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.WriteLine("Är du säker på att du inte vill lägga till fler ingredienser [j/N]");
                    Console.ResetColor();

                    switch (Console.ReadLine().ToLower()) {
                        case "j":
                        case "ja":
                        case "y":
                        case "yes":
                            if (ingredients.Count < 1) {
                                Console.BackgroundColor = ConsoleColor.Red;
                                Console.ForegroundColor = ConsoleColor.White;

                                Console.WriteLine(" FEL! Du måste ange minst en ingrediens.");
                                Console.WriteLine();

                                Console.BackgroundColor = ConsoleColor.Blue;
                                Console.WriteLine("  Tryck tangent för att fortsätta ange ingredienser - [Esc] avbryter   ");
                                Console.ResetColor();

                                if (Console.ReadKey(true).Key == ConsoleKey.Escape) {
                                    return null;
                                }

                                continue;
                            }
                            return ingredients;
                        default:
                            continue;
                    }
                }
                ingredient.Name = name;

                Console.Write("Mängd:");
                ingredient.Amount = Console.ReadLine();

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

                ingredients.Add(ingredient);
            } while (true);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads recipes from the file specified at Path
        /// </summary>
        /// <returns>A list of recipes</returns>
        public List<Recipe> Load()
        {
            RecipeReadStatus status = RecipeReadStatus.Indefinite;
            string[] lines = System.IO.File.ReadAllLines(Path);
            Recipe recipe = null;
            List<Recipe> recepies = new List<Recipe>();

            foreach (string line in lines) {
                if (String.IsNullOrWhiteSpace(line)) {
                    continue;
                } else if (line == "[Recept]") {
                    status = RecipeReadStatus.New;
                    continue;
                } else if (line == "[Ingredienser]") {
                    status = RecipeReadStatus.Ingredient;
                    continue;
                } else if (line == "[Instruktioner]") {
                    status = RecipeReadStatus.Direction;
                    continue;
                }

                try {
                    switch (status) {
                        case RecipeReadStatus.New:
                            recipe = new Recipe(line);
                            recepies.Add(recipe);
                            break;
                        case RecipeReadStatus.Ingredient:
                            Ingredient i = new Ingredient();
                            string[] parts = line.Split(';');

                            if (parts.Length != 3) {
                                throw new Exception("bad file format");
                            }

                            i.Amount = parts[0];
                            i.Measure = parts[1];
                            i.Name = parts[2];

                            recipe.Add(i);
                            break;
                        case RecipeReadStatus.Direction:
                            recipe.Add(line);
                            break;
                        default:
                            throw new Exception("bad file format");
                    }
                } catch (NullReferenceException) {
                    throw new Exception("bad file format");
                }
            }

            recepies.Sort();

            return recepies;
        }