Advertisement
bayangarkov

BasicLoginForm

Mar 29th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.ExceptionServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _00_Testing
  9. {
  10.     class usingClasses
  11.     {
  12.         static void Main()
  13.         {
  14.             Console.WriteLine("Login or Register?");
  15.            
  16.             var inputLine = Console.ReadLine();
  17.  
  18.             var allUsers = new users();
  19.  
  20.             int counter = 0;
  21.  
  22.             allUsers.Name = new List<string>();
  23.             allUsers.Pass = new List<string>();
  24.  
  25.             // registration form
  26.  
  27.             if (inputLine == "register")
  28.             {
  29.                 Console.WriteLine("Enter User and Pass and type login:");
  30.                 inputLine = Console.ReadLine();
  31.  
  32.                 while (inputLine != "login")
  33.                 {
  34.                     var splitted = inputLine.Split(' ');
  35.  
  36.                     var inputName = splitted[0];
  37.                     var inputPass = splitted[1];
  38.                    
  39.                     allUsers.Name.Add(inputName);
  40.                     allUsers.Pass.Add(inputPass);
  41.  
  42.                     Console.WriteLine("Enter Username and Password:");
  43.                     inputLine = Console.ReadLine();
  44.                 }
  45.             }
  46.  
  47.             // validation logic for checking if user exist and password for current user is same
  48.  
  49.             if (inputLine == "login")
  50.             {
  51.                 loginForm:
  52.  
  53.                 inputLine = Console.ReadLine();
  54.  
  55.                 while (true)
  56.                 {
  57.                     var splitted = inputLine.Split(' ');
  58.  
  59.                     var inputName = splitted[0];
  60.                     var inputPass = splitted[1];
  61.  
  62.                     if (allUsers.Name.Contains(inputName)) // if username exist?
  63.                     {
  64.                         counter = checkForPosition(allUsers, counter, inputName);
  65.  
  66.                         // if pass on specific user exist?
  67.  
  68.                         if (allUsers.Name.Contains(inputName) && allUsers.Pass[counter] == inputPass)
  69.                         {
  70.                             Console.WriteLine(Environment.NewLine + "Login successfull!");
  71.                             break;
  72.                         }
  73.                         else
  74.                         {
  75.                             Console.WriteLine("Wrong Username or Password");
  76.                             goto loginForm;
  77.                         }
  78.                     }
  79.  
  80.                     inputLine = Console.ReadLine();
  81.                 }
  82.             }
  83.  
  84.             // printing some output
  85.             printWelcomeMessage(allUsers, counter);
  86.  
  87.         }
  88.  
  89.         private static void printWelcomeMessage(users allUsers, int counter)
  90.         {
  91.             Console.WriteLine($"Hello {allUsers.Name[counter]} !!!");
  92.             Console.WriteLine("Welcome to Home Page!");
  93.  
  94.             DateTime dt = DateTime.Now;
  95.  
  96.             Console.WriteLine($"Today is: {dt}");
  97.  
  98.             Console.WriteLine("---------------------");
  99.         }
  100.  
  101.         // method for checking on witch position are username and pass
  102.         private static int checkForPosition(users allUsers, int counter, string inputName)
  103.         {
  104.             foreach (var name in allUsers.Name)
  105.             {
  106.                 if (inputName != name)
  107.                 {
  108.                     counter++;
  109.                 }
  110.                 else
  111.                 {
  112.                     break;
  113.                 }
  114.             }
  115.  
  116.             return counter;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement