From 372138e090c10f2dbd64277c5bc05f971d7a9760 Mon Sep 17 00:00:00 2001 From: ilyasBozdemir Date: Wed, 20 Mar 2024 10:28:39 +0300 Subject: [PATCH] code refactoring --- quiz-console-app/Constants/MenuOptions.cs | 24 ++++++ quiz-console-app/Enums/QuizMode.cs | 7 -- quiz-console-app/Models/MenuManager.cs | 77 +++++++++++++++++ quiz-console-app/Models/MenuOption.cs | 16 ++++ quiz-console-app/Program.cs | 7 +- quiz-console-app/Screens/ExportDataScreen.cs | 83 ++----------------- .../Screens/QuizMainMenuScreen.cs | 15 ++++ .../Services/QuizModeHandlerService.cs | 79 ------------------ 8 files changed, 144 insertions(+), 164 deletions(-) create mode 100644 quiz-console-app/Constants/MenuOptions.cs delete mode 100644 quiz-console-app/Enums/QuizMode.cs create mode 100644 quiz-console-app/Models/MenuManager.cs create mode 100644 quiz-console-app/Models/MenuOption.cs create mode 100644 quiz-console-app/Screens/QuizMainMenuScreen.cs delete mode 100644 quiz-console-app/Services/QuizModeHandlerService.cs diff --git a/quiz-console-app/Constants/MenuOptions.cs b/quiz-console-app/Constants/MenuOptions.cs new file mode 100644 index 0000000..075b13b --- /dev/null +++ b/quiz-console-app/Constants/MenuOptions.cs @@ -0,0 +1,24 @@ +using quiz_console_app.Models; +using quiz_console_app.Screens; + +namespace quiz_console_app.Constants; + +public static class MenuOptions +{ + + public static readonly MenuOption[] GeneralOptions = + { + new MenuOption(1, "Quiz Çözme Modu", () => new QuizModeScreen().StartQuiz()), + new MenuOption(2, "Verileri Oluşturma ve Dışa Aktarma Modu", () => new ExportDataScreen().DisplayMenuOptions()) + }; + + public static readonly MenuOption[] ExportOptions = + { + new MenuOption(1, "Kitapçık Oluştur ve JSON Olarak Dışa Aktar",() => new ExportDataScreen().CreateAndExportBookletToJson()), + new MenuOption(2, "Kitapçık Oluştur ve XML Olarak Dışa Aktar", () => new ExportDataScreen().CreateAndExportBookletToXml()), + new MenuOption(3, "Kitapçık Oluştur ve CSV Olarak Dışa Aktar", () => new ExportDataScreen().CreateAndExportBookletToCsv()), + new MenuOption(4, "Ana Menüye Dön", () => new ExportDataScreen().ReturnToMainMenu()) + }; +} + + diff --git a/quiz-console-app/Enums/QuizMode.cs b/quiz-console-app/Enums/QuizMode.cs deleted file mode 100644 index 2daa823..0000000 --- a/quiz-console-app/Enums/QuizMode.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace quiz_console_app.Enums; -[Flags] -public enum QuizMode -{ - Quiz = 1, - ExportData = 2 -} diff --git a/quiz-console-app/Models/MenuManager.cs b/quiz-console-app/Models/MenuManager.cs new file mode 100644 index 0000000..9326d50 --- /dev/null +++ b/quiz-console-app/Models/MenuManager.cs @@ -0,0 +1,77 @@ +using quiz_console_app.Helpers; + +namespace quiz_console_app.Models; + +public class MenuManager +{ + private readonly Dictionary menuOptions; + public string ErrorMessage { get; set; } + + public MenuManager() + { + menuOptions = new Dictionary(); + ErrorMessage = "Geçersiz seçim. Lütfen geçerli bir seçenek girin."; + } + + public void AddMenuOption(MenuOption option) + { + menuOptions[option.Id] = option; + } + + public void AddMenuOptions(MenuOption[] options) + { + foreach (MenuOption option in options) + { + AddMenuOption(option); + } + } + + public void DisplayMenu() + { + for (int i = 0; i < menuOptions.Count; i++) + { + ErrorMessage += (i != 0) + ? (i != menuOptions.Count - 1) + ? $", {i + 1}" + : $" veya {i + 1}" + : $"{i + 1}"; + } + + ErrorMessage = $"Geçersiz seçim. Lütfen {ErrorMessage} girin."; + + ConsoleHelper.WriteColoredLine("Seçiminizi yapın\n".ToUpper(), ConsoleColors.Title); + + foreach (var option in menuOptions.Values) + { + ConsoleHelper.WriteColored($"{option.Id}", ConsoleColors.Info); + Console.WriteLine($" => {option.Name}"); + } + } + + public void HandleSelection() + { + bool menuState = true; + + while (menuState) + { + Console.ForegroundColor = ConsoleColors.Prompt; + Console.Write("Seçiminizi yapın: "); + Console.ForegroundColor = ConsoleColors.Default; + string userInput = Console.ReadLine(); + int choice; + + if (int.TryParse(userInput, out choice)) + if (menuOptions.ContainsKey(choice)) + menuOptions[choice].Action(); + else + ConsoleHelper.WriteColoredLine("Geçersiz seçim. Lütfen listedeki bir seçeneği seçin.", ConsoleColors.Error); + else + ConsoleHelper.WriteColoredLine("Geçersiz giriş. Lütfen bir sayı girin.", ConsoleColors.Error); + } + + + ConsoleHelper.WriteColored("Çıkış için enter tuşuna basın.", ConsoleColors.Debug); + Console.ReadLine(); + } +} + diff --git a/quiz-console-app/Models/MenuOption.cs b/quiz-console-app/Models/MenuOption.cs new file mode 100644 index 0000000..1d5af28 --- /dev/null +++ b/quiz-console-app/Models/MenuOption.cs @@ -0,0 +1,16 @@ +namespace quiz_console_app.Models; + +public class MenuOption +{ + public int Id { get; set; } + public string Name { get; set; } + public Action Action { get; set; } + + public MenuOption(int id, string name, Action action) + { + Id = id; + Name = name; + Action = action; + } +} + diff --git a/quiz-console-app/Program.cs b/quiz-console-app/Program.cs index 78bcfcd..8961367 100644 --- a/quiz-console-app/Program.cs +++ b/quiz-console-app/Program.cs @@ -1,5 +1,4 @@ -using quiz_console_app.Helpers; -using quiz_console_app.Services; +using quiz_console_app.Screens; -QuizModeHandlerService quizModeHandlerService = new QuizModeHandlerService(); -quizModeHandlerService.ShowMainMenu(); \ No newline at end of file +QuizMainMenuScreen quizModeHandlerService = new QuizMainMenuScreen(); +quizModeHandlerService.Show(); \ No newline at end of file diff --git a/quiz-console-app/Screens/ExportDataScreen.cs b/quiz-console-app/Screens/ExportDataScreen.cs index b5684a0..39bfc3c 100644 --- a/quiz-console-app/Screens/ExportDataScreen.cs +++ b/quiz-console-app/Screens/ExportDataScreen.cs @@ -1,84 +1,20 @@ -using quiz_console_app.Helpers; +using quiz_console_app.Constants; +using quiz_console_app.Models; using quiz_console_app.Services; namespace quiz_console_app.Screens; public class ExportDataScreen { - public void Start() + public void DisplayMenuOptions() { - ShowMenu(); + MenuManager menuManager = new MenuManager(); + menuManager.AddMenuOptions(MenuOptions.ExportOptions); + menuManager.DisplayMenu(); + menuManager.HandleSelection(); } - public void ShowMenu() - { - string errorMessage = ""; - string[] options = - { - "Kitapçık Oluştur ve JSON Olarak Dışa Aktar", - "Kitapçık Oluştur ve XML Olarak Dışa Aktar", - "Kitapçık Oluştur ve CSV Olarak Dışa Aktar", - "Ana Menüye Dön" - }; - - for (int i = 0; i < options.Length; i++) - errorMessage += (i != 0) - ? (i != options.Length - 1) - ? $", {i + 1}" - : $" veya {i + 1}" - : $"{i + 1}"; - - errorMessage = $"Geçersiz seçim. Lütfen {errorMessage} girin."; - - ConsoleHelper.WriteColoredLine("Seçiminizi yapın\n".ToUpper(), ConsoleColors.Title); - - for (int i = 0; i < options.Length; i++) - { - ConsoleHelper.WriteColored($"{i + 1}", ConsoleColors.Info); - Console.WriteLine($" => {options[i]}"); - } - bool menuState = true; - - while (menuState) - { - Console.ForegroundColor = ConsoleColors.Prompt; - Console.Write("Seçiminizi yapın: "); - Console.ForegroundColor = ConsoleColors.Default; - string userInput = Console.ReadLine(); - Console.ResetColor(); - int choice; - - if (int.TryParse(userInput, out choice)) - HandleMenuOption(choice); - else - ConsoleHelper.WriteColoredLine("Geçersiz giriş. Lütfen bir sayı girin.", ConsoleColors.Error); - - } - } - - private void HandleMenuOption(int option) - { - switch (option) - { - case 1: - CreateAndExportBookletToJson(); - break; - case 2: - CreateAndExportBookletToXml(); - break; - case 3: - CreateAndExportBookletToCsv(); - break; - case 4: - ReturnToMainMenu(); - break; - default: - Console.WriteLine("Geçersiz seçenek. Lütfen geçerli bir seçenek seçin."); - break; - } - } - - + public void CreateAndExportBookletToJson() { Console.WriteLine("CreateAndExportBookletToJson"); @@ -97,7 +33,6 @@ public void CreateAndExportBookletToCsv() public void ReturnToMainMenu() { Console.Clear(); - new QuizModeHandlerService().ShowMainMenu(); + new QuizMainMenuScreen().Show(); } - } diff --git a/quiz-console-app/Screens/QuizMainMenuScreen.cs b/quiz-console-app/Screens/QuizMainMenuScreen.cs new file mode 100644 index 0000000..049dbe6 --- /dev/null +++ b/quiz-console-app/Screens/QuizMainMenuScreen.cs @@ -0,0 +1,15 @@ +using quiz_console_app.Constants; +using quiz_console_app.Models; + +namespace quiz_console_app.Screens; + +public class QuizMainMenuScreen +{ + public void Show() + { + MenuManager menuManager = new MenuManager(); + menuManager.AddMenuOptions(MenuOptions.GeneralOptions); + menuManager.DisplayMenu(); + menuManager.HandleSelection(); + } +} \ No newline at end of file diff --git a/quiz-console-app/Services/QuizModeHandlerService.cs b/quiz-console-app/Services/QuizModeHandlerService.cs deleted file mode 100644 index da559e3..0000000 --- a/quiz-console-app/Services/QuizModeHandlerService.cs +++ /dev/null @@ -1,79 +0,0 @@ -using quiz_console_app.Enums; -using quiz_console_app.Helpers; -using quiz_console_app.Screens; - -namespace quiz_console_app.Services; - -public class QuizModeHandlerService -{ - public void StartMode(QuizMode mode) - { - Console.Clear(); - switch (mode) - { - case QuizMode.Quiz: - new QuizModeScreen().StartQuiz(); - break; - case QuizMode.ExportData: - new ExportDataScreen().Start(); - break; - default: - Console.WriteLine("Geçersiz mod."); - break; - } - } - - public void ShowMainMenu() - { - string errorMessage = ""; - string[] options = - { - "Quiz Çözme Modu", - "Verileri Dışa Aktarma Modu", - }; - - for (int i = 0; i < options.Length; i++) - errorMessage += (i != 0) - ? (i != options.Length - 1) - ? $", {i + 1}" - : $" veya {i + 1}" - : $"{i + 1}"; - - errorMessage = $"Geçersiz seçim. Lütfen {errorMessage} girin."; - - ConsoleHelper.WriteColoredLine("Seçiminizi yapın\n".ToUpper(), ConsoleColors.Title); - - for (int i = 0; i < options.Length; i++) - { - ConsoleHelper.WriteColored($"{i + 1}", ConsoleColors.Info); - Console.WriteLine($" => {options[i]}"); - } - - bool menuState = true; - while (menuState) - { - Console.ForegroundColor = ConsoleColors.Prompt; - Console.Write("Seçiminizi yapın: "); - Console.ForegroundColor = ConsoleColors.Default; - string userInput = Console.ReadLine(); - Console.ResetColor(); - int choice; - if (int.TryParse(userInput, out choice)) - if (Enum.IsDefined(typeof(QuizMode), choice)) - { - StartMode((QuizMode)choice); - break; - } - else - ConsoleHelper.WriteColoredLine(errorMessage, ConsoleColors.Error); - - else - ConsoleHelper.WriteColoredLine("Geçersiz giriş. Lütfen bir sayı girin.", ConsoleColors.Error); - } - - ConsoleHelper.WriteColored("Çıkış için enter tuşuna basın.", ConsoleColors.Debug); - - Console.ReadLine(); - } - -} \ No newline at end of file