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 a3edbcd commit f8c514a
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 194 deletions.
4 changes: 0 additions & 4 deletions quiz-console-app/Constants/MenuOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ public static class MenuOptions
name: "Kitapçık Oluştur ve XML Olarak Dışa Aktar",
action: () => new ExportDataView().CreateAndExportBookletToXml()
),
new MenuOption(
name: "Kitapçık Oluştur ve CSV Olarak Dışa Aktar",
action: () => new ExportDataView().CreateAndExportBookletToCsv()
),
new MenuOption(
name: "Ana Menüye Dön",
action: () =>
Expand Down
9 changes: 6 additions & 3 deletions quiz-console-app/Helpers/OptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ public static class OptionHelper
{
public static char ToOptionLetter(int optionId)
{
if (optionId < 0 || optionId > 25)
throw new ArgumentException("Option ID must be between 0 and 25");
int startID = 0;
int endID = 25;

if (optionId < startID || optionId > endID)
throw new ArgumentException($"Seçenek kimliği {startID} ile {endID} arasında olmalıdır");

return ((char)(65 + optionId));
}

public static int FromOptionLetter(char optionLetter)
{
if (!char.IsLetter(optionLetter))
throw new ArgumentException("Input must be a letter.");
throw new ArgumentException("Giriş bir harf olmalıdır.");

return char.ToUpper(optionLetter) - 65;
}
Expand Down
8 changes: 8 additions & 0 deletions quiz-console-app/Interfaces/IExportService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using quiz_console_app.ViewModels;

namespace quiz_console_app.Interfaces;

public interface IExportService
{
void Export(List<BookletViewModel> Booklets, string filePath);
}
3 changes: 1 addition & 2 deletions quiz-console-app/Models/MenuOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

public class MenuOption
{
public Guid Id { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public Action Action { get; set; }

public MenuOption(string name, Action action)
{
Id = Guid.NewGuid();
Name = name;
Action = action;
}
Expand Down
134 changes: 0 additions & 134 deletions quiz-console-app/Services/ExportService.cs

This file was deleted.

12 changes: 12 additions & 0 deletions quiz-console-app/Services/JsonExportService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using quiz_console_app.Interfaces;
using quiz_console_app.ViewModels;

namespace quiz_console_app.Services;

public class JsonExportService : IExportService
{
public void Export(List<BookletViewModel> Booklets, string filePath)
{

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Newtonsoft.Json;
using quiz_console_app.Helpers;
using quiz_console_app.Models;

namespace quiz_console_app.Helpers;
public class QuestionLoader
namespace quiz_console_app.Services;
public class QuestionBuilderService
{
public List<BookletQuestion> LoadQuestionsFromJson(string jsonFilePath = null, string jsonSource = null)

Check warning on line 8 in quiz-console-app/Services/QuestionBuilderService.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 8 in quiz-console-app/Services/QuestionBuilderService.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
Expand All @@ -17,18 +18,18 @@ public List<BookletQuestion> LoadQuestionsFromJson(string jsonFilePath = null, s
}
if (jsonSource != null)
json = jsonSource;

if (json == null)
throw new ArgumentNullException("JSON dosyası yolu veya içeriği belirtilmemiş.");


List<BookletQuestion> questions = JsonConvert.DeserializeObject<List<BookletQuestion>>(json);

foreach (var question in questions)
{
if (!question.ValidateCorrectOptionCount())
ConsoleHelper.WriteColoredLine($"{question.Id}. Soru için doğru şık sayısı geçerli değil. Sadece bir tane doğru şık olmalıdır.", ConsoleColors.Error);

if (!question.ValidateIncorrectOptionCount())
ConsoleHelper.WriteColoredLine($"{question.Id}. Soru için yanlış şık sayısı geçerli değil. Yanlış şık olmamalıdır.", ConsoleColors.Error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using quiz_console_app.Models;
using quiz_console_app.ViewModels;

namespace quiz_console_app.Helpers;
namespace quiz_console_app.Services;

public class QuestionShuffler
public class QuestionOptionsSuffleService
{
private static Random random = new Random();

Expand All @@ -19,7 +19,7 @@ public static void ShuffleBookletQuestions(BookletViewModel booklet)

public static List<BookletQuestion> ShuffleQuestionOptions(List<BookletQuestion> questions)
{

foreach (var question in questions)
{
List<BookletQuestionOption> shuffledOptions = ShuffleOptions(question.QuestionOptions, random);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using quiz_console_app.Models;
using quiz_console_app.Helpers;
using quiz_console_app.Models;
using quiz_console_app.ViewModels;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace quiz_console_app.Helpers;
namespace quiz_console_app.Services;

public class QuizDisplay
public class QuizConsoleDisplayService
{
public static void DisplayBooklets(List<BookletViewModel> booklets)
{
Expand Down Expand Up @@ -78,10 +78,6 @@ public static void EvaluateQuizResults(QuizResultSummary resultSummary)
Console.WriteLine();
}

public static void DisplaySeparator()
{
ConsoleHelper.WriteColoredLine(new string('-', 50), ConsoleColors.Default);
}

public static void DisplayQuizAndUserData(Quiz quiz, User user)
{
Expand Down Expand Up @@ -109,12 +105,6 @@ public static void DisplayQuizAndUserData(Quiz quiz, User user)
ConsoleHelper.WriteColored("Kullanıcı Adı : ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine(user.Username, ConsoleColors.Default);
}

public static void ClearConsole()
{
Console.Clear();
}

public static void DisplayUserInfo(User user)
{
ConsoleHelper.WriteColoredLine("Kullanıcı Bilgileri:", ConsoleColors.Title);
Expand All @@ -125,4 +115,16 @@ public static void DisplayUserInfo(User user)
ConsoleHelper.WriteColored("Kullanıcı Adı: ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine($"{user.Username}", ConsoleColors.Default);
}

public static void DisplaySeparator()
{
ConsoleHelper.WriteColoredLine(new string('-', 50), ConsoleColors.Default);
}

public static void ClearConsole()
{
Console.Clear();
}


}
10 changes: 5 additions & 5 deletions quiz-console-app/Services/QuizService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class QuizService
public static AnswerKeyCollection AnswerKeys { get; private set; }

private List<BookletQuestion> _sourceQuestions;
private readonly QuestionLoader _questionLoader;
private readonly QuestionBuilderService _questionLoader;

public QuizService()
{
_questionLoader = new QuestionLoader();
_questionLoader = new QuestionBuilderService();
Booklets = new List<BookletViewModel>();
AnswerKeys = new AnswerKeyCollection();
ConsoleHelper.WriteColoredLine("Data Alınıyor.", ConsoleColors.Info);
Expand All @@ -40,7 +40,7 @@ public void GenerateBooklets(int shuffleCount = 1)
char prefix = (char)(65 + i);
string bookletName = $"{prefix}_{1}";

List<BookletQuestion> bookletQuestions = QuestionShuffler.ShuffleQuestionOptions(_sourceQuestions);
List<BookletQuestion> bookletQuestions = QuestionOptionsSuffleService.ShuffleQuestionOptions(_sourceQuestions);

BookletViewModel booklet = new BookletViewModel
{
Expand All @@ -50,7 +50,7 @@ public void GenerateBooklets(int shuffleCount = 1)
Prefix = bookletName,

};
QuestionShuffler.ShuffleBookletQuestions(booklet);
QuestionOptionsSuffleService.ShuffleBookletQuestions(booklet);
GenerateAnswerKeys(booklet);
booklets.Add(booklet);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public void EvaluateQuizResults(List<UserAnswerKeyViewModel> userAnswers, int us
else
ConsoleHelper.WriteColoredLine($"Hata: Beklenen kitapçık ID bulunamadı. Beklenen ID: {userBookletId}", ConsoleColors.Error);

QuizDisplay.EvaluateQuizResults(resultSummary);
QuizConsoleDisplayService.EvaluateQuizResults(resultSummary);
}

private static QuestionViewModel MapToQuestionViewModel(BookletQuestion question)
Expand Down
12 changes: 12 additions & 0 deletions quiz-console-app/Services/XmlExportService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using quiz_console_app.Interfaces;
using quiz_console_app.ViewModels;

namespace quiz_console_app.Services;

public class XmlExportService : IExportService
{
public void Export(List<BookletViewModel> Booklets, string filePath)
{

}
}
Loading

0 comments on commit f8c514a

Please sign in to comment.