LinkedIn Learning - C#

workload => 2 hrs 39 min - instructed by Thaise Medeiros

Table of contents

No heading

No headings in the article.

Project link: https://github.com/ThiagoKS-7/Linkedin-Learning-CSharp1

In this LinkedIn Learning course, the main objective was not necessarily to create a system or platform, but to train the fundamentals of the language, starting with variables, their types, conditionals, loops, Arrays and collections.

After configuring my Visual Studio, creating the solution file and initializing the repo, I established a personal goal to implement each fundamental in different classes, while also trying to follow the singleton design pattern, creating a common interface between all of them. The code below shows one of these fundamental's classes:

# Condicionais.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    public class Condicionais : IFundamentos
    {
        private Condicionais() { }

        private static Condicionais _instance;

        public static Condicionais GetInstance()
        {
            if (_instance == null)
            {
                _instance = new Condicionais();
            }
            return _instance;
        }

        public void run()
        {
            MostraCondicionais();
        }


        private List<String> GetTodosMeses()
        {
            List<String> meses = new List<String>();
            meses.Add("JANEIRO");
            meses.Add("FEVEREIRO");
            meses.Add("MARCO");
            meses.Add("ABRIL");
            meses.Add("MAIO");
            meses.Add("JUNHO");
            meses.Add("JULHO");
            meses.Add("AGOSTO");
            meses.Add("SETEMBRO");
            meses.Add("OUTUBRO");
            meses.Add("NOVEMBRO");
            meses.Add("DEZEMBRO");
            return meses;
        }

        private List<String> GetMesesComTrinta()
        {
            /* Criando uma lista com os meses que tem 30 dias,
            * pra dai checar se a string pega ta nesses meses
            */
            List<String> mesesComTrinta = new List<String>();
            mesesComTrinta.Add("ABRIL");
            mesesComTrinta.Add("JUNHO");
            mesesComTrinta.Add("SETEMBRO");
            mesesComTrinta.Add("NOVEMBRO");
            return mesesComTrinta;
        }

        private void ChecaNota()
        {
            const string APROVADO = "Aprovado!";
            const string REPROVADO = "Reprovado!";
            const string EM_REC = "Em Recuperação";

            Console.Write("[IF] Digite a nota: ");
            decimal nota = Convert.ToDecimal(Console.ReadLine());

            if (nota >= 7)
            {
                Console.WriteLine(APROVADO);
            }
            else if (nota >= 4 && nota < 5)
            {
                Console.WriteLine(EM_REC);
            }
            else
            {
                Console.WriteLine(REPROVADO);
            }
        }

        private void ChecaDiasDoMes()
        {
            List<String> meses = GetTodosMeses();
            List<String> mesesComTrinta = GetMesesComTrinta();
            string mes = "";

            while (!meses.Contains(mes.ToUpper()))
            {
                Console.Write("[SWITCH] Digite um mês: ");
                mes = Console.ReadLine();

            }


            bool temTrintaDias = mesesComTrinta.Contains(mes);

            switch (mes.ToUpper())
            {
                case "FEVEREIRO":
                    int CurrentYear = DateTime.Now.Year;
                    if (DateTime.IsLeapYear(CurrentYear))
                    {
                        Console.WriteLine("29 dias.");
                    }
                    else
                    {
                        Console.WriteLine("28 dias.");
                    }
                    break;
                default:
                    if (temTrintaDias)
                    {
                        Console.WriteLine("30 dias.");
                    }
                    else
                    {
                        Console.WriteLine("31 dias.");
                    }
                    break;

            }
        }

        private void MostraCondicionais()
        {
            Console.WriteLine("\n Condicionais: ");
            ChecaNota();
            ChecaDiasDoMes();
            Console.Write("\n Clique qualquer tecla para sair... ");
            Console.ReadLine();
        }
    }
}

To run it properly, I wrote the following in my interface and main classes:

# IFundamentos.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    public interface IFundamentos
    {
        public void run();
    }
}
# Program.cs
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    public class Program
    {
        public static void Main()
        {
            // See https://aka.ms/new-console-template for more information
            Console.WriteLine("Hello, World!");

            Tipos tp = Tipos.GetInstance();
            Operadores op = Operadores.GetInstance();
            Strings st = Strings.GetInstance();
            Condicionais cd = Condicionais.GetInstance();

            tp.run();
            op.run();
            st.run();
            cd.run();
        }
    }
}

As you can see, this part is still very repetitive, so my future refactor attempt will try to create a parent class for all the fundamentals, creating an 'ArrayList<parentClass>' to store them.

This way, I'm hoping to be able to iterate in each of the 'run( )' methods of each class. If anyone has any better suggestions on how I can tweak this part, feel free to hit me up on my socials or send me a pull request on the repo.

And that's it for now, I'm still taking the course, so I hope to be able to update the status of this project later, either here or in the repo's readme. Thanks for reading and until next time!


References studied:

https://refactoring.guru/design-patterns/singleton/csharp/example

https://rb.gy/m2tvkg

https://dirask.com/posts/C-NET-get-current-year-month-day-hour-minute-second-millisecond-Kj85L1

https://www.macoratti.net/11/05/c_dlg1.htm

https://www.geeksforgeeks.org/c-sharp-how-to-check-whether-a-list-contains-a-specified-element/

https://balta.io/blog/datetime-csharp-dotnet

https://www.linkedin.com/learning/c-sharp-formacao-basica/estrutura-de-repeticao-while-e-for?autoSkip=true&autoplay=true&resume=false