Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasbozdemir committed Mar 20, 2024
1 parent dc9f258 commit 372138e
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 164 deletions.
24 changes: 24 additions & 0 deletions quiz-console-app/Constants/MenuOptions.cs
Original file line number Diff line number Diff line change
@@ -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())
};
}


7 changes: 0 additions & 7 deletions quiz-console-app/Enums/QuizMode.cs

This file was deleted.

77 changes: 77 additions & 0 deletions quiz-console-app/Models/MenuManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using quiz_console_app.Helpers;

namespace quiz_console_app.Models;

public class MenuManager
{
private readonly Dictionary<int, MenuOption> menuOptions;
public string ErrorMessage { get; set; }

public MenuManager()
{
menuOptions = new Dictionary<int, MenuOption>();
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();
}
}

16 changes: 16 additions & 0 deletions quiz-console-app/Models/MenuOption.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

7 changes: 3 additions & 4 deletions quiz-console-app/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
QuizMainMenuScreen quizModeHandlerService = new QuizMainMenuScreen();
quizModeHandlerService.Show();
83 changes: 9 additions & 74 deletions quiz-console-app/Screens/ExportDataScreen.cs
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -97,7 +33,6 @@ public void CreateAndExportBookletToCsv()
public void ReturnToMainMenu()
{
Console.Clear();
new QuizModeHandlerService().ShowMainMenu();
new QuizMainMenuScreen().Show();
}

}
15 changes: 15 additions & 0 deletions quiz-console-app/Screens/QuizMainMenuScreen.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
79 changes: 0 additions & 79 deletions quiz-console-app/Services/QuizModeHandlerService.cs

This file was deleted.

0 comments on commit 372138e

Please sign in to comment.