Time Counter

My First Project: Step by step

Let's crate a new .Net Framework project...

#01

Alt text

#02

Alt text

#03

Alt text

#04

Alt text

Alt text

If you can't see Solution Explorer

Alt text

Let's start the project :-)

Alt text

First lines...

Alt text

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
            Console.WriteLine("Hello ABC IT!");
            Console.ReadKey(true);
        }
    }
}

Run (Start) project now again

Congratullations - you'v created your first program in C#


Something more interesting and usfull

Time counter project

Objective

Create a program that can sum times - for example 2:37:52 + 0:44:18 = ?

#01

Alt text

#02

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

    var userInput = Console.ReadLine();

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(userInput);

            Console.ReadKey(true);
        }
    }
}

#03 (Let's get the segments of the time - separate hours minuts and seconds...)

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");


    var userInput = Console.ReadLine();

    var timeParts = userInput.Split(':');


            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"Hour: {timeParts[0]} minut: {timeParts[1]} seconds: {timeParts[2]}");

            Console.ReadKey(true);
        }
    }
}

#04 (Loop)

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");

    while (true)
            {
    var userInput = Console.ReadLine();
    var timeParts = userInput.Split(':');

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Hour: {timeParts[0]} minut: {timeParts[1]} seconds: {timeParts[2]}");
                Console.ResetColor();
            }

    //Console.ReadKey(true);
        }
    }
}

#05 (Get the "number" values out of "text")

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");

    while (true)
            {
    var userInput = Console.ReadLine();
    var timeParts = userInput.Split(':');

    var hours = int.Parse(timeParts[0]);
    var minuts = int.Parse(timeParts[1]);
    var seconds = int.Parse(timeParts[2]);

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Hour: {hours} minut: {minuts} seconds: {seconds}");
                Console.ResetColor();
            }

    //Console.ReadKey(true);
        }
    }
}

#06 (Sum times)

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");

    var result = new DateTime();

    while (true)
            {
    var userInput = Console.ReadLine();
    var timeParts = userInput.Split(':');

    var hours = int.Parse(timeParts[0]);
    var minuts = int.Parse(timeParts[1]);
    var seconds = int.Parse(timeParts[2]);

                result = result.AddHours(hours);
                result = result.AddMinutes(minuts);
                result = result.AddSeconds(seconds);


                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
                Console.ResetColor();
            }

    //Console.ReadKey(true);
        }
    }
}

#07 (Ending the "endless loop)

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");

    var result = new DateTime();

    while (true)
            {
    var userInput = Console.ReadLine();

    if ("end".Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("... Press any key to close the window ...");
                    Console.ReadKey(true);
    break;
                }

    var timeParts = userInput.Split(':');

    var hours = int.Parse(timeParts[0]);
    var minuts = int.Parse(timeParts[1]);
    var seconds = int.Parse(timeParts[2]);

                result = result.AddHours(hours);
                result = result.AddMinutes(minuts);
                result = result.AddSeconds(seconds);


                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
                Console.ResetColor();
            }

    //Console.ReadKey(true);
        }
    }
}

#08 Let's deal with errors

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");

    var result = new DateTime();

    while (true)
            {
    try
                {
    var userInput = Console.ReadLine();

    if ("end".Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
                    {
                        Console.WriteLine("... Press any key to close the window ...");
                        Console.ReadKey(true);
    break;
                    }

    var timeParts = userInput.Split(':');

    var hours = int.Parse(timeParts[0]);
    var minuts = int.Parse(timeParts[1]);
    var seconds = int.Parse(timeParts[2]);

                    result = result.AddHours(hours);
                    result = result.AddMinutes(minuts);
                    result = result.AddSeconds(seconds);


                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
                    Console.ResetColor();
                }
    catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
    // TODO : Display result anyway
                }
            }

    //Console.ReadKey(true);
        }
    }
}

#09 Validate input

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");

    var result = new DateTime();

    while (true)
            {
    try
                {
    var userInput = Console.ReadLine();

    if ("end".Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
                    {
                        Console.WriteLine("... Press any key to close the window ...");
                        Console.ReadKey(true);
    break;
                    }

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


                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
                    Console.ResetColor();
                }
    catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
    // TODO : Display result anyway
                }
            }

    //Console.ReadKey(true);
        }
    }
}

#10 Code improvements (extract methods)

Alt text

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");

    var result = new DateTime();

    while (true)
            {
    try
                {
    var userInput = Console.ReadLine();

    if ("end".Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
                    {
                        Console.WriteLine("... Press any key to close the window ...");
                        Console.ReadKey(true);
    break;
                    }

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

                    DisplayResult(result);
                }
    catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    DisplayResult(result);
                }
            }

    //Console.ReadKey(true);
        }

    private static void DisplayResult(DateTime result)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
            Console.ResetColor();
        }
    }
}

#11 Extract the method for counting the result

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");

    var result = new DateTime();

    while (true)
            {
    try
                {
    var userInput = Console.ReadLine();

    if ("end".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);
                }
            }

    //Console.ReadKey(true);
        }

    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)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
            Console.ResetColor();
        }
    }
}

#12 Create constant for "end"

Alt text

... now the EndProgram constant represents the string "end":

Alt text

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    private const string EndProgram = "end";

    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

            Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");

    var result = new DateTime();

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

    //Console.ReadKey(true);
        }

    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)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
            Console.ResetColor();
        }
    }
}

#13 Fixing the bug

using System;

namespace AbcItNetFramework
{
    internal class Program
    {
    private const string EndProgram = "end";
    private static readonly DateTime startTime = new DateTime();

    static void Main()
        {
    // Console.WriteLine("Hello ABC IT!");

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

    //Console.ReadKey(true);
        }

    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();
        }
    }
}