コード例 #1
0
ファイル: Program.cs プロジェクト: re222dv/1DV402-C-sharp
        /// <summary>
        /// Save the recipes to a file choosen by the user
        /// </summary>
        private static void SaveRecipes(List<Recipe> recipes)
        {
            Console.Clear();
            if (recipes.Count > 0) {
                bool repeat = false;
                string name;
                do {
                    Console.Write("Välj filnamn [recipes.txt]: ");
                    name = Console.ReadLine();
                    if (name == "") {
                        name = "recipes.txt";
                    }
                    if (System.IO.File.Exists(name)) {
                        Console.BackgroundColor = ConsoleColor.Yellow;
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.WriteLine("Är du säker på att du vill skriva över '{0}'? [j/N]", name);
                        Console.ResetColor();

                        switch (Console.ReadLine().ToLower()) {
                            case "j":
                            case "ja":
                            case "y":
                            case "yes":
                                repeat = false;
                                break;
                            default:
                                repeat = true;
                                break;
                        }
                    } else {
                        repeat = false;
                    }
                } while(repeat);
                try {
                    RecipeRepository rp = new RecipeRepository(name);
                    rp.Save(recipes);
                    RecipeView.RenderHeader("Recepten har sparats", Type.good);
                } catch {
                    RecipeView.RenderHeader("FEL! Ett fel inträffade då recepten skulle sparas.", Type.bad);
                }
            } else {
                RecipeView.RenderHeader("Det finns inga recept att spara", Type.warning);
            }
            ContinueOnKeyPressed();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: re222dv/1DV402-C-sharp
 /// <summary>
 /// Load recipes from a file choosen by the user
 /// </summary>
 private static List<Recipe> LoadRecepies()
 {
     List<Recipe> recepies;
     Console.Clear();
     Console.Write("Välj filnamn [recipes.txt]: ");
     string name = Console.ReadLine();
     if (name == "") {
         name = "recipes.txt";
     }
     try {
         RecipeRepository rp = new RecipeRepository(name);
         recepies = rp.Load();
         RecipeView.RenderHeader("Recepten har lästs in", Type.good);
     } catch {
         RecipeView.RenderHeader("FEL! Ett fel inträffade då recepten lästes in.", Type.bad);
         recepies = new List<Recipe>();
     }
     ContinueOnKeyPressed();
     return recepies;
 }