Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasbozdemir committed Mar 19, 2024
1 parent aa5659c commit 4f9131a
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 151 deletions.
20 changes: 20 additions & 0 deletions quiz-console-app/Helpers/OptionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace quiz_console_app.Helpers;

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");

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

public static int FromOptionLetter(char optionLetter)
{
if (!char.IsLetter(optionLetter))
throw new ArgumentException("Input must be a letter.");

return char.ToUpper(optionLetter) - 65;
}
}
33 changes: 27 additions & 6 deletions quiz-console-app/Helpers/QuestionLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Newtonsoft.Json;
using quiz_console_app.Exceptions;
using quiz_console_app.Models;

namespace quiz_console_app.Helpers;
Expand All @@ -17,19 +16,41 @@ public List<BookletQuestion> LoadQuestionsFromJson(string jsonFilePath = null, s
json = File.ReadAllText(fullPath);
}
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);
}



return questions;
}
catch (JsonLoadFailedException ex)
catch (JsonReaderException ex)
{
ConsoleHelper.WriteColored($"JSON formatında bir hata oluştu: {ex.Message}", ConsoleColors.Error);
return new List<BookletQuestion>();
}
catch (Exception ex)
{
throw new JsonLoadFailedException("JSON dosyası yüklenirken bir hata oluştu.", ex);
ConsoleHelper.WriteColored($"JSON dosyası yüklenirken bir hata oluştu: {ex.Message}", ConsoleColors.Error);
return new List<BookletQuestion>();
}

}


public async Task<List<BookletQuestion>> LoadQuestionsFromUrl(Uri url)
{
try
Expand Down
56 changes: 54 additions & 2 deletions quiz-console-app/Helpers/QuizDisplay.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using quiz_console_app.Models;
using quiz_console_app.ViewModels;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace quiz_console_app.Helpers;

Expand Down Expand Up @@ -67,10 +68,61 @@ public static void DisplayAnswerKeys(List<AnswerKeyViewModel> answerKeys)
public static void EvaluateQuizResults(QuizResultSummary resultSummary)
{
Console.WriteLine();
ConsoleHelper.WriteColoredLine($"Toplam Soru Sayısı: {resultSummary.TotalQuestions}", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine($"Doğru Sayısı: {resultSummary.CorrectCount}", ConsoleColors.Success);
ConsoleHelper.WriteColoredLine($"Yanlış Sayısı: {resultSummary.IncorrectCount}", ConsoleColors.Error);
ConsoleHelper.WriteColoredLine($"Yanlış Cevap Cezası Durumu : {resultSummary.ScoringRules.PenaltyForIncorrectAnswer}", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine($"Boş Sayısı: {resultSummary.BlankCount}", ConsoleColors.Warning);
ConsoleHelper.WriteColoredLine($"Net: {resultSummary.NetCount}", ConsoleColors.Default);
ConsoleHelper.WriteColoredLine($"Başarı Yüzdesi: {resultSummary.SuccessPercentage:F2}%", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine($"Net: {resultSummary.NetCount:F2}/{resultSummary.TotalQuestions}", ConsoleColors.Default);
ConsoleHelper.WriteColoredLine($"Başarı Yüzdesi: {resultSummary.NetCount / resultSummary.TotalQuestions * 100:F2}%", ConsoleColors.Default);
Console.WriteLine();
}

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

public static void DisplayQuizAndUserData(Quiz quiz, User user)
{
ConsoleHelper.WriteColoredLine("Quiz Bilgileri: ", ConsoleColors.Title);

ConsoleHelper.WriteColored("Oluşturan: ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine(quiz.Creator, ConsoleColors.Default);

ConsoleHelper.WriteColored("Başlık: ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine(quiz.Title, ConsoleColors.Default);

ConsoleHelper.WriteColored("Açıklama: ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine(quiz.Description, ConsoleColors.Default);

Console.WriteLine();

ConsoleHelper.WriteColoredLine("Kullanıcı Bilgileri: ", ConsoleColors.Title);

ConsoleHelper.WriteColored("Kullanıcı ID: ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine(user.Id, ConsoleColors.Default);

ConsoleHelper.WriteColored("Ad Soyad : ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine($"{user.FirstName} {user.LastName}", ConsoleColors.Default);

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);
ConsoleHelper.WriteColored("ID: ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine($"{user.Id}", ConsoleColors.Default);
ConsoleHelper.WriteColored("Ad: ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine($"{user.FirstName} {user.LastName}", ConsoleColors.Default);
ConsoleHelper.WriteColored("Kullanıcı Adı: ", ConsoleColors.Info);
ConsoleHelper.WriteColoredLine($"{user.Username}", ConsoleColors.Default);
}
}
31 changes: 31 additions & 0 deletions quiz-console-app/Models/AnswerKeyCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using quiz_console_app.ViewModels;

namespace quiz_console_app.Models;

public class AnswerKeyCollection
{
private Dictionary<int, List<AnswerKeyViewModel>> _answerKeysByBookletId;

public AnswerKeyCollection()
{
_answerKeysByBookletId = new Dictionary<int, List<AnswerKeyViewModel>>();
}

public void AddAnswerKey(int bookletId, List<AnswerKeyViewModel> answerKeys)
{
_answerKeysByBookletId.Add(bookletId, answerKeys);
}

public List<AnswerKeyViewModel> GetAnswerKeys(int bookletId)
{
if (_answerKeysByBookletId.ContainsKey(bookletId))
{
return _answerKeysByBookletId[bookletId];
}
else
{
return new List<AnswerKeyViewModel>();
}
}
}

28 changes: 28 additions & 0 deletions quiz-console-app/Models/BookletQuestion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,36 @@ public class BookletQuestion
public string Explanation { get; set; }
public DifficultyLevel Difficulty { get; set; }
public List<BookletQuestionOption> QuestionOptions { get; set; }

public BookletQuestion()
{
QuestionOptions = new List<BookletQuestionOption>();
}


public bool ValidateCorrectOptionCount()
{
int correctCount = QuestionOptions.Count(option => option.IsCorrect);
return correctCount == 1;
}

public bool ValidateIncorrectOptionCount()
{
int correctCount = QuestionOptions.Count(option => option.IsCorrect);
int incorrectCount = QuestionOptions.Count(option => !option.IsCorrect);

return ValidateIncorrectOptionCount(correctCount, incorrectCount);
}

private bool ValidateIncorrectOptionCount(int correctCount, int incorrectCount)
{
return incorrectCount == QuestionOptions.Count - 1 && (correctCount + incorrectCount == QuestionOptions.Count);
}

private bool ValidateCorrectOptionCount(int correctCount, int incorrectCount)
{
return correctCount == 1 && (correctCount + incorrectCount == QuestionOptions.Count);
}


}
25 changes: 9 additions & 16 deletions quiz-console-app/Models/QuizResultSummary.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
namespace quiz_console_app.Models;


// ScoringRules buna göre net hesaplaması yapılcaktır.
public class QuizResultSummary
{
public int CorrectCount { get; set; } = 0;
public int IncorrectCount { get; set; } = 0;
public int BlankCount { get; set; } = 0;
public int NetCount => CorrectCount - (IncorrectCount + BlankCount);
public double SuccessPercentage => CalculateSuccessPercentage();
public double NetCount => CalculateNetCount();
public int TotalQuestions { get; set; } = 0;

public int QuestionCurrentNumber { get; set; } = 1;

public QuizResultSummary(int totalQuestions)
public ScoringRules ScoringRules { get; set; }
public QuizResultSummary(int totalQuestions, ScoringRules scoringRules)
{
this.TotalQuestions = totalQuestions;
TotalQuestions = totalQuestions;
this.ScoringRules = scoringRules;
}

private double CalculateSuccessPercentage()
private double CalculateNetCount()
{
int totalQuestions = CorrectCount + IncorrectCount + BlankCount;
if (totalQuestions == 0)
{
return 0.0;
}

return (double)CorrectCount / totalQuestions * 100;
return (ScoringRules.PenaltyForIncorrectAnswer)
? CorrectCount - ScoringRules.IncorrectAnswerScore * IncorrectCount
: CorrectCount;
}
}
2 changes: 1 addition & 1 deletion quiz-console-app/Models/ScoringRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class ScoringRules
public double IncorrectAnswerScore => 1.0 / (NumberOfChoices - 1); // Yanlış cevap için puanlar
public int BlankAnswerScore { get; set; } = 0; // Boş cevaplar için puan
public int NumberOfChoices { get; set; } // Sorunun şık sayısı
public bool PenaltyForIncorrectAnswer { get; set; } // Yanlış cevaplar için varsayılan olarak ceza uygulanacak
public bool PenaltyForIncorrectAnswer { get; set; } = true; // Yanlış cevaplar için varsayılan olarak ceza uygulanacak
public ScoringRules(int numberOfChoices)
{
NumberOfChoices = numberOfChoices;
Expand Down
31 changes: 31 additions & 0 deletions quiz-console-app/Models/UserAnswerKeyCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using quiz_console_app.ViewModels;

namespace quiz_console_app.Models;

public class UserAnswerKeyCollection
{
private Dictionary<Guid, List<UserAnswerKeyViewModel>> _userAnswerKeysByUserId;

public UserAnswerKeyCollection()
{
_userAnswerKeysByUserId = new Dictionary<Guid, List<UserAnswerKeyViewModel>>();
}

public void AddUserAnswerKey(Guid userId, List<UserAnswerKeyViewModel> userAnswerKeys)
{
_userAnswerKeysByUserId.Add(userId, userAnswerKeys);
}

public List<UserAnswerKeyViewModel> GetUserAnswerKeys(Guid userId)
{
if (_userAnswerKeysByUserId.ContainsKey(userId))
{
return _userAnswerKeysByUserId[userId];
}
else
{
return new List<UserAnswerKeyViewModel>();
}
}
}

5 changes: 3 additions & 2 deletions quiz-console-app/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using quiz_console_app.Services;
using quiz_console_app.Helpers;
using quiz_console_app.Services;

QuizModeHandlerService quizModeHandlerService = new QuizModeHandlerService();
quizModeHandlerService.ShowMainMenu();
quizModeHandlerService.ShowMainMenu();
20 changes: 3 additions & 17 deletions quiz-console-app/Screens/DisplayBookletScreen.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
using quiz_console_app.Helpers;
using quiz_console_app.Models;
using quiz_console_app.Services;
using quiz_console_app.ViewModels;

namespace quiz_console_app.Screens;
namespace quiz_console_app.Screens;

public class DisplayBookletScreen
{
private readonly QuizService _quizService;
private readonly QuestionLoader _questionLoader;
private readonly List<BookletQuestion> _questions;
private readonly List<UserAnswerKeyViewModel> _userAnswers;
private readonly List<AnswerKeyViewModel> _answerKeys;

public DisplayBookletScreen()
{
_questionLoader = new QuestionLoader();
_questions = _questionLoader.LoadQuestionsFromJson(jsonFilePath: "software_questions.json");
_quizService = new QuizService();
_quizService.GenerateBooklets(_questions, 1);
_userAnswers = new List<UserAnswerKeyViewModel>();
_answerKeys = QuizService.AnswerKeys;

}

public void Start()
Expand Down
Loading

0 comments on commit 4f9131a

Please sign in to comment.