7
\$\begingroup\$

I'm beginner at programming (I guess it's quite obvious) and this is my small "app" I tried to make, that takes username and password from the user. Could you give me some advice on how can I improve it?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


namespace Login
{
    class Program
    {

        static void Main(string[] args)
        {
            Initialization();
        }

        public static void Initialization()
        {
            int x = 0;
            int choice = 0;

            while (x < 2)
            {
                Console.Clear();

                Console.Write("Username: ");
                string username = Console.ReadLine();

                Console.Write("\nPassword: ");
                string password = Console.ReadLine();

                Console.WriteLine();

                string option1 = "Login";
                string option2 = "Exit";

                Console.WriteLine(option1 + "\n" + option2);

                while (x < 2)
                {
                    ConsoleKeyInfo info = Console.ReadKey();
                    if (info.Key == ConsoleKey.UpArrow)
                    {
                        Console.Clear();
                        Console.ResetColor();

                        Console.WriteLine("Username: {0}", username);
                        Console.WriteLine("\nPassword: {0}\n", password);

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

                        Console.ResetColor();
                        Console.WriteLine(option2);

                        choice = 1;
                    }
                    else if (info.Key == ConsoleKey.DownArrow)
                    {
                        Console.Clear();
                        Console.ResetColor();

                        Console.WriteLine("Username: {0}", username);
                        Console.WriteLine("\nPassword: {0}\n", password);

                        Console.ResetColor();
                        Console.WriteLine(option1);

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(option2);
                        choice = 2;

                    }

                    if (info.Key == ConsoleKey.Enter && choice == 1)
                    {
                        if (Login(username, password) == true)
                        {
                            Console.WriteLine("Welcome");
                            x = 2;
                        }
                        else
                        {
                            Console.WriteLine("Wrong password!");
                            Console.ReadLine();
                            break;
                        }

                    }
                    else if (info.Key == ConsoleKey.Enter && choice == 2)
                    {
                        Shutdown();
                    }
                }
            }


        }
        public static void Shutdown()
        {
            Console.ResetColor();
            System.Environment.Exit(0);
        }
        public static bool Login(string username, string password)
        {
            if(username == "asd" && password == "asd")
            {
                Console.WriteLine("Welcome");
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
\$\endgroup\$
2
5
\$\begingroup\$

Just a few things:

  • Don't use \n as a literal, we use Environment.NewLine instead.
  • Don't use "string concatenation" (string + string), instead use string.Join, string.Format or a StringBuilder. I.e.:

    Console.WriteLine(option1 + "\n" + option2);
    

    Should be:

    Console.WriteLine("{0}{1}{2}", option1, Environment.NewLine, option2);
    

    Just as well, you should, ideally, switch this to two Console.WriteLine statements:

    Console.WriteLine(option1);
    Console.WriteLine(option2);
    
  • A method should have on responsibility (SRP):

    public static bool Login(string username, string password)
    {
        if(username == "asd" && password == "asd")
        {
            Console.WriteLine("Welcome");
            return true;
        }
        else
        {
            return false;
        }
    }
    

    The Console.WriteLine statement does not belong there, Login should simply return true or false in this case.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Whilst I agree about string concatenation in this specific case, I disagree with the blanket statement Don't use "string concatenation". String concatenation with a few strings is more efficient than calling string.Format or using a StringBuilder, and depending on its use can be more or less legible than calling a method. \$\endgroup\$ – 404 Dec 18 '16 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.