public override object GenerateQuestion(Difficulty difficulty) { PercentageProblem problem = new PercentageProblem(); Random rand = new Random(); problem.Base = rand.Next(1, 10) * 10; problem.Percent = rand.Next(1, 10) * 10; problem.Amount = Percentage(problem.Percent, problem.Base); switch (rand.Next(3)) { case 0: problem.MissingPart = PercentageProblemPart.Amount; break; case 1: problem.MissingPart = PercentageProblemPart.Percent; break; case 2: problem.MissingPart = PercentageProblemPart.Base; break; default: throw new Exception("Missing part generated was not valid."); } return(problem); }
public override bool IsCorrect(object problem, int answer) { PercentageProblem p = (PercentageProblem)problem; switch (p.MissingPart) { case PercentageProblemPart.Amount: return(p.Amount == answer); case PercentageProblemPart.Percent: return(p.Percent == answer); case PercentageProblemPart.Base: return(p.Base == answer); default: return(false); } }
public static void PrintQuestion(this PercentageProblem pp, int questionNumber) { switch (pp.MissingPart) { case PercentageProblemPart.Amount: Console.WriteLine($"({questionNumber}) _ is {pp.Percent}% of {pp.Base}"); break; case PercentageProblemPart.Percent: Console.WriteLine($"({questionNumber}) {pp.Amount} is _% of {pp.Base}"); break; case PercentageProblemPart.Base: Console.WriteLine($"({questionNumber}) {pp.Amount} is {pp.Percent}% of _"); break; default: throw new ArgumentOutOfRangeException(paramName: nameof(pp.MissingPart), message: $"Missing Part is '{pp.MissingPart}'."); } }
public static void AskPercentageQuestion(Generator generator) { Percentages subject = new Percentages(); var questionCount = generator.QuestionCount; var currentQuestion = 1; while (currentQuestion <= questionCount) { PercentageProblem p = (PercentageProblem)subject.GenerateQuestion(generator.Difficulty); var isCorrect = false; while (!isCorrect) { // Ask question p.PrintQuestion(currentQuestion); // Get answer Console.Write("Answer: "); int answer = 0; bool validAnswer = true; try { answer = Convert.ToInt32(Console.ReadLine()); } catch (Exception) { validAnswer = false; } // Work out if answer is correct if (validAnswer) { isCorrect = subject.IsCorrect(p, answer); Console.WriteLine(isCorrect ? "Correct!" : "Not quite right. Try again."); } } currentQuestion++; } }