Math Challange
Let's continue coding...
#01 Before we start new stuff we have to make some cleanings:
Create a separate class for the logic
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
TimeCounter.Run();
}
}
internal class TimeCounter
{
private const string EndProgram = "end";
private static readonly DateTime startTime = new DateTime();
public static void Run()
{
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var result = startTime;
while (true)
{
try
{
var userInput = Console.ReadLine();
if (EndProgram.Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("... Press any key to close the window ...");
Console.ReadKey(true);
break;
}
result = CountResult(result, userInput);
DisplayResult(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
DisplayResult(result);
}
}
}
private static DateTime CountResult(DateTime result, string userInput)
{
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
if (minuts > 59 || minuts < 0)
{
throw new Exception("The max value of minuts is 59");
}
var seconds = int.Parse(timeParts[2]);
if (seconds > 59 || minuts < 0)
{
throw new Exception("The max value of seconds is 59");
}
result = result.AddHours(hours);
result = result.AddMinutes(minuts);
result = result.AddSeconds(seconds);
return result;
}
private static void DisplayResult(DateTime result)
{
var realResult = result - startTime;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{Math.Floor(realResult.TotalHours):00}:{realResult.Minutes:00}:{realResult.Seconds:00}");
Console.ResetColor();
}
}
}
Create a new folder for exercises
Move the new created class in a separate file
Move the new created file into the folder
Now create a new class called "MathChallange" directly in a
"Excercises" folder
Now we have a brand new class prepared
#01 Add some implementation
using System;
namespace AbcItNetFramework.Exercises
{
internal class MathChallange
{
public static void Run()
{
Console.WriteLine("Hello from MathChallange class!");
Console.ReadKey(true);
}
}
}
#02 Call it from the Program/Main() method
using AbcItNetFramework.Exercises;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
MathChallange.Run();
}
}
}
#03 Create some math exercises
using System;
namespace AbcItNetFramework.Exercises
{
internal class MathChallange
{
public static void Run()
{
Random random = new Random();
do
{
int value1 = random.Next(1, 99);
int value2 = random.Next(1, 99);
Console.WriteLine($"{value1} + {value2} = ?");
Console.ReadKey(true);
}
while (true);
}
}
}
using System;
namespace AbcItNetFramework.Exercises
{
internal class MathChallange
{
public static void Run()
{
Random random = new Random();
do
{
int value1 = random.Next(1, 99);
int value2 = random.Next(1, 99);
Console.WriteLine($"{value1} + {value2} = ?");
var line = Console.ReadLine();
var parse = int.TryParse(line, out var response);
if (!parse || value1 + value2 != response)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("KO");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("OK");
}
Console.ResetColor();
}
while (true);
}
}
}
#05 Works? Greate - but... Let's improve it a bit
Instad of infinit loop will have just - say - 10 exercises to begin with
using System;
namespace AbcItNetFramework.Exercises
{
internal class MathChallange
{
public static void Run()
{
Random random = new Random();
for (int i = 0; i < 10; i++)
{
int value1 = random.Next(1, 99);
int value2 = random.Next(1, 99);
Console.WriteLine($"{value1} + {value2} = ?");
var line = Console.ReadLine();
var parse = int.TryParse(line, out var response);
if (!parse || value1 + value2 != response)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("KO");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("OK");
}
Console.ResetColor();
}
}
}
}
#06 Count the number of right / wrong answers
using System;
namespace AbcItNetFramework.Exercises
{
internal class MathChallange
{
public static void Run()
{
Random random = new Random();
var rightAnswers = 0;
var wrongAnswers = 0;
for (int i = 0; i < 10; i++)
{
int value1 = random.Next(1, 99);
int value2 = random.Next(1, 99);
Console.WriteLine($"{value1} + {value2} = ?");
var line = Console.ReadLine();
var parse = int.TryParse(line, out var response);
if (!parse || value1 + value2 != response)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("KO");
wrongAnswers++;
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("OK");
rightAnswers++;
}
Console.ResetColor();
}
Console.WriteLine($"OK: {rightAnswers}x");
Console.WriteLine($"KO: {wrongAnswers}x");
Console.ReadKey(true);
}
}
}
#07 Make user choose number of excercies
using System;
namespace AbcItNetFramework.Exercises
{
internal class MathChallange
{
public static void Run()
{
Console.WriteLine("Welcome to the Math Test Application");
Console.WriteLine("Set the number of exercises:");
string exercisesNumber = Console.ReadLine();
if (!int.TryParse(exercisesNumber, out var problemsCount))
{
problemsCount = 10;
Console.WriteLine("Your choise was not valid - the value was set to 10!");
}
Random random = new Random();
var rightAnswers = 0;
var wrongAnswers = 0;
for (int i = 0; i < problemsCount; i++)
{
int value1 = random.Next(1, 99);
int value2 = random.Next(1, 99);
Console.WriteLine($"{value1} + {value2} = ?");
var line = Console.ReadLine();
var parse = int.TryParse(line, out var response);
if (!parse || value1 + value2 != response)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("KO");
wrongAnswers++;
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("OK");
rightAnswers++;
}
Console.ResetColor();
}
Console.WriteLine($"OK: {rightAnswers}x");
Console.WriteLine($"KO: {wrongAnswers}x");
Console.ReadKey(true);
}
}
}
#08 We don't need store the wrong answers count - we can count it
using System;
namespace AbcItNetFramework.Exercises
{
internal class MathChallange
{
public static void Run()
{
Console.WriteLine("Welcome to the Math Test Application");
Console.WriteLine("Set the number of exercises:");
string exercisesNumber = Console.ReadLine();
if (!int.TryParse(exercisesNumber, out var problemsCount))
{
problemsCount = 10;
Console.WriteLine("Your choise was not valid - the value was set to 10!");
}
Random random = new Random();
var rightAnswers = 0;
for (int i = 0; i < problemsCount; i++)
{
int value1 = random.Next(1, 99);
int value2 = random.Next(1, 99);
Console.WriteLine($"{value1} + {value2} = ?");
var line = Console.ReadLine();
var parse = int.TryParse(line, out var response);
if (!parse || value1 + value2 != response)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("KO");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("OK");
rightAnswers++;
}
Console.ResetColor();
}
Console.WriteLine($"OK: {rightAnswers}x");
Console.WriteLine($"KO: {problemsCount - rightAnswers}x");
Console.ReadKey(true);
}
}
}
#09 Improve evaluation
using System;
namespace AbcItNetFramework.Exercises
{
internal class MathChallange
{
public static void Run()
{
Console.WriteLine("Welcome to the Math Test Application");
Console.WriteLine("Set the number of exercises:");
string exercisesNumber = Console.ReadLine();
if (!int.TryParse(exercisesNumber, out var problemsCount))
{
problemsCount = 10;
Console.WriteLine("Your choise was not valid - the value was set to 10!");
}
Random random = new Random();
var rightAnswers = 0;
for (int i = 0; i < problemsCount; i++)
{
int value1 = random.Next(1, 99);
int value2 = random.Next(1, 99);
Console.WriteLine($"{value1} + {value2} = ?");
var line = Console.ReadLine();
var parse = int.TryParse(line, out var response);
if (!parse || value1 + value2 != response)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("KO");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("OK");
rightAnswers++;
}
Console.ResetColor();
}
string evaluation = EvaluateChallange(problemsCount, rightAnswers);
Console.WriteLine();
Console.WriteLine($"{evaluation} - all done with score {rightAnswers} / {problemsCount}");
Console.ReadKey(true);
}
private static string EvaluateChallange(int problemsCount, int rightAnswers)
{
var score = rightAnswers == 0
? 0
: (int)((decimal)rightAnswers / problemsCount * 10);
string evaluation;
switch (score)
{
case 10:
evaluation = "Excellent";
break;
case 9:
case 8:
evaluation = "Spectacular";
break;
case 7:
case 6:
evaluation = "Good";
break;
case 5:
case 4:
evaluation = "Poor";
break;
default:
evaluation = "Are you joking?";
break;
}
return evaluation;
}
}
}
using System;
namespace AbcItNetFramework.Exercises
{
internal class MathChallange
{
public static void Run()
{
int problemsCount = GetTheNumberOfExercises();
Random random = new Random();
var rightAnswers = 0;
for (int i = 0; i < problemsCount; i++)
{
rightAnswers = ProcessExcercise(random, rightAnswers);
}
string evaluation = EvaluateChallange(problemsCount, rightAnswers);
Console.WriteLine();
Console.WriteLine($"{evaluation} - all done with score {rightAnswers} / {problemsCount}");
Console.ReadKey(true);
}
private static int GetTheNumberOfExercises()
{
Console.WriteLine("Welcome to the Math Test Application");
Console.WriteLine("Set the number of exercises:");
string exercisesNumber = Console.ReadLine();
if (!int.TryParse(exercisesNumber, out var problemsCount))
{
problemsCount = 10;
Console.WriteLine("Your choise was not valid - the value was set to 10!");
}
return problemsCount;
}
private static int ProcessExcercise(Random random, int rightAnswers)
{
int value1 = random.Next(1, 99);
int value2 = random.Next(1, 99);
Console.WriteLine($"{value1} + {value2} = ?");
var line = Console.ReadLine();
var parse = int.TryParse(line, out var response);
if (!parse || value1 + value2 != response)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("KO");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("OK");
rightAnswers++;
}
Console.ResetColor();
return rightAnswers;
}
private static string EvaluateChallange(int problemsCount, int rightAnswers)
{
var score = rightAnswers == 0
? 0
: (int)((decimal)rightAnswers / problemsCount * 10);
string evaluation;
switch (score)
{
case 10:
evaluation = "Excellent";
break;
case 9:
case 8:
evaluation = "Spectacular";
break;
case 7:
case 6:
evaluation = "Good";
break;
case 5:
case 4:
evaluation = "Poor";
break;
default:
evaluation = "Are you joking?";
break;
}
return evaluation;
}
}
}