Advertisement
dopefish2112

csharp zero to hero week 3

Apr 21st, 2021
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5.  
  6.  
  7.  
  8.  
  9. namespace Lesson_3
  10. {
  11.     class MyProgram
  12.     {
  13.      
  14.         static bool validInput;
  15.         static int userSelection;
  16.         static string[] userOptions = new string[3];
  17.         static string file = @".\Users.txt";
  18.         static public void Main(string[] args)
  19.         {
  20.             userOptions[0] = "Login";
  21.             userOptions[1] = "Register";
  22.             userOptions[2] = "Exit";
  23.  
  24.             Console.WriteLine("Grettings user! Welcome to the week 3 lesson exercise.");
  25.  
  26.             Start();
  27.  
  28.         }
  29.        
  30.         static void Login()
  31.         {
  32.             //Stores user input and checks against filestream and grants access
  33.             string userName;
  34.             string password;
  35.             int index = 0;
  36.             List<string> lines = new List<string>();
  37.             StreamReader reader;
  38.  
  39.             //Check if File exists and catches error if it doesn't
  40.             try
  41.             {
  42.                 reader = new StreamReader(file);
  43.                 reader.Close();
  44.             }
  45.                        
  46.             catch (System.IO.FileNotFoundException)
  47.             {            
  48.                 Console.WriteLine("Authentication credentials unavailable. Contact your system administrator for assistance. Program will now exit.");
  49.                 Console.ReadLine();
  50.                 Exit();
  51.             }
  52.  
  53.             //Stores File Stream contents and displays it for testing. Remove foreach loop before deployment.
  54.             reader = new StreamReader(file);
  55.             string contents = reader.ReadToEnd();
  56.             string[] contentsArray = contents.Split(new string[] { "\r" }, StringSplitOptions.None);
  57.             reader.Close();
  58.  
  59.             for (int i = 0; i < contentsArray.Length; i++)
  60.             {
  61.                 contentsArray[i] = contentsArray[i].Trim();
  62.             }
  63.             foreach (var line in contentsArray)
  64.             {
  65.                 Console.WriteLine(line);
  66.             }
  67.        
  68.             //Records username and confirms it exists
  69.             Console.WriteLine("Hello user. Enter your Username and press enter. . .");
  70.             userName = $"username:{Console.ReadLine()}";
  71.  
  72.                 if (contentsArray.Contains(userName))
  73.                 {
  74.                     Console.WriteLine("User account located.");
  75.                 }
  76.                 else
  77.                 {
  78.                     Console.WriteLine("User name not found. please try again or use the register option to create a new account.");
  79.                     Start();
  80.                 }
  81.  
  82.  
  83.             //Locates password for username and asks for user to enter
  84.             int v = Array.IndexOf(contentsArray, userName) + 1;
  85.             index = v;
  86.             Console.WriteLine(v);
  87.             Console.WriteLine($"Please enter your password and press enter. . .");
  88.             password = $"password:{Console.ReadLine()}";
  89.  
  90.             if (contentsArray[v] == password)
  91.             {
  92.                 Console.WriteLine("Access granted.");
  93.             }
  94.             else
  95.             {
  96.                 Console.WriteLine("Password incorrect. Please try again or use the register option to create a new account");
  97.             }
  98.  
  99.          
  100.             Console.ReadLine();
  101.             Start();
  102.            
  103.         }
  104.  
  105.         //User landing interface. Gives user 3 options to choose from.
  106.         static int Start()
  107.         {
  108.             Console.WriteLine("Please choose one of the following options. . .");
  109.             foreach (var option in userOptions)
  110.             {
  111.                 Console.WriteLine($"{Array.IndexOf(userOptions, option) + 1}). {option}");
  112.             }
  113.  
  114.             validInput = int.TryParse(Console.ReadLine(), out int output);
  115.  
  116.             if ((validInput == true) && (output > 0) && (output < 4))
  117.             {
  118.                 output--;
  119.                 userSelection = output;
  120.                 switch (userOptions[userSelection])
  121.                 {
  122.                     case "Login":
  123.                         Login();
  124.                         break;
  125.                     case "Register":
  126.                         Register();
  127.                         break;
  128.                     case "Exit":
  129.                         Exit();
  130.                         break;
  131.                 }
  132.             }
  133.  
  134.             else
  135.             {
  136.                 Console.WriteLine("Invalid Selection. Please try again.");
  137.                 Start();
  138.             }
  139.  
  140.             return userSelection;
  141.  
  142.  
  143.  
  144.         }
  145.  
  146.         //Adds a new user account
  147.         static void Register()
  148.         {
  149.             string userName;
  150.             string password;
  151.  
  152.             List<string> lines = new List<string>();
  153.             StreamReader reader;
  154.             try
  155.             {
  156.                 reader = new StreamReader(file);
  157.                 reader.Close();
  158.             }
  159.  
  160.             catch (System.IO.FileNotFoundException)
  161.             {
  162.                 Console.WriteLine("Authentication credentials unavailable. Contact your system administrator for assistance. Program will now exit.");
  163.                 Console.ReadLine();
  164.                 Exit();
  165.             }
  166.  
  167.             reader = new StreamReader(file);
  168.             string contents = reader.ReadToEnd();
  169.             string[] contentsArray = contents.Split(new string[] { "\r" }, StringSplitOptions.None);
  170.             reader.Close();
  171.  
  172.             for (int i = 0; i < contentsArray.Length; i++)
  173.             {
  174.                 contentsArray[i] = contentsArray[i].Trim();
  175.             }
  176.  
  177.             //displays user account info for debuging. remove foreach loops before deployment
  178.             foreach (var line in contentsArray)
  179.             {
  180.                 Console.WriteLine(line);
  181.             }
  182.  
  183.             Console.WriteLine("Enter your desired Username and press enter. . .");
  184.             userName = $"username:{Console.ReadLine()}";
  185.  
  186.             // checks if user already exists
  187.             if (contentsArray.Contains(userName))
  188.             {
  189.                 Console.WriteLine("User account already exists. Press enter and try again");
  190.                 Console.ReadLine();
  191.                 Register();
  192.             }
  193.            
  194.  
  195.             Console.WriteLine($"Please enter your desired password.");
  196.             password = $"password:{Console.ReadLine()}";
  197.  
  198.             //writes new user account into file.
  199.             using (StreamWriter writer = new StreamWriter(file))
  200.             {
  201.                 foreach (var line in contentsArray)
  202.                 {
  203.                     writer.WriteLine(line);
  204.                 }
  205.                 writer.WriteLine(userName);
  206.                 writer.WriteLine(password);
  207.             }
  208.  
  209.             Console.WriteLine("Account created! Press enter to return to main menu.");
  210.             Console.ReadLine();
  211.             Start();
  212.         }
  213.  
  214.         static void Exit()
  215.         {
  216.             Console.WriteLine("Goodbye!");
  217.             Console.ReadLine();
  218.             Environment.Exit(0);
  219.         }
  220.     }
  221. }
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement