Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO;
- namespace Lesson_3
- {
- class MyProgram
- {
- static bool validInput;
- static int userSelection;
- static string[] userOptions = new string[3];
- static string file = @".\Users.txt";
- static public void Main(string[] args)
- {
- userOptions[0] = "Login";
- userOptions[1] = "Register";
- userOptions[2] = "Exit";
- Console.WriteLine("Grettings user! Welcome to the week 3 lesson exercise.");
- Start();
- }
- static void Login()
- {
- //Stores user input and checks against filestream and grants access
- string userName;
- string password;
- int index = 0;
- List<string> lines = new List<string>();
- StreamReader reader;
- //Check if File exists and catches error if it doesn't
- try
- {
- reader = new StreamReader(file);
- reader.Close();
- }
- catch (System.IO.FileNotFoundException)
- {
- Console.WriteLine("Authentication credentials unavailable. Contact your system administrator for assistance. Program will now exit.");
- Console.ReadLine();
- Exit();
- }
- //Stores File Stream contents and displays it for testing. Remove foreach loop before deployment.
- reader = new StreamReader(file);
- string contents = reader.ReadToEnd();
- string[] contentsArray = contents.Split(new string[] { "\r" }, StringSplitOptions.None);
- reader.Close();
- for (int i = 0; i < contentsArray.Length; i++)
- {
- contentsArray[i] = contentsArray[i].Trim();
- }
- foreach (var line in contentsArray)
- {
- Console.WriteLine(line);
- }
- //Records username and confirms it exists
- Console.WriteLine("Hello user. Enter your Username and press enter. . .");
- userName = $"username:{Console.ReadLine()}";
- if (contentsArray.Contains(userName))
- {
- Console.WriteLine("User account located.");
- }
- else
- {
- Console.WriteLine("User name not found. please try again or use the register option to create a new account.");
- Start();
- }
- //Locates password for username and asks for user to enter
- int v = Array.IndexOf(contentsArray, userName) + 1;
- index = v;
- Console.WriteLine(v);
- Console.WriteLine($"Please enter your password and press enter. . .");
- password = $"password:{Console.ReadLine()}";
- if (contentsArray[v] == password)
- {
- Console.WriteLine("Access granted.");
- }
- else
- {
- Console.WriteLine("Password incorrect. Please try again or use the register option to create a new account");
- }
- Console.ReadLine();
- Start();
- }
- //User landing interface. Gives user 3 options to choose from.
- static int Start()
- {
- Console.WriteLine("Please choose one of the following options. . .");
- foreach (var option in userOptions)
- {
- Console.WriteLine($"{Array.IndexOf(userOptions, option) + 1}). {option}");
- }
- validInput = int.TryParse(Console.ReadLine(), out int output);
- if ((validInput == true) && (output > 0) && (output < 4))
- {
- output--;
- userSelection = output;
- switch (userOptions[userSelection])
- {
- case "Login":
- Login();
- break;
- case "Register":
- Register();
- break;
- case "Exit":
- Exit();
- break;
- }
- }
- else
- {
- Console.WriteLine("Invalid Selection. Please try again.");
- Start();
- }
- return userSelection;
- }
- //Adds a new user account
- static void Register()
- {
- string userName;
- string password;
- List<string> lines = new List<string>();
- StreamReader reader;
- try
- {
- reader = new StreamReader(file);
- reader.Close();
- }
- catch (System.IO.FileNotFoundException)
- {
- Console.WriteLine("Authentication credentials unavailable. Contact your system administrator for assistance. Program will now exit.");
- Console.ReadLine();
- Exit();
- }
- reader = new StreamReader(file);
- string contents = reader.ReadToEnd();
- string[] contentsArray = contents.Split(new string[] { "\r" }, StringSplitOptions.None);
- reader.Close();
- for (int i = 0; i < contentsArray.Length; i++)
- {
- contentsArray[i] = contentsArray[i].Trim();
- }
- //displays user account info for debuging. remove foreach loops before deployment
- foreach (var line in contentsArray)
- {
- Console.WriteLine(line);
- }
- Console.WriteLine("Enter your desired Username and press enter. . .");
- userName = $"username:{Console.ReadLine()}";
- // checks if user already exists
- if (contentsArray.Contains(userName))
- {
- Console.WriteLine("User account already exists. Press enter and try again");
- Console.ReadLine();
- Register();
- }
- Console.WriteLine($"Please enter your desired password.");
- password = $"password:{Console.ReadLine()}";
- //writes new user account into file.
- using (StreamWriter writer = new StreamWriter(file))
- {
- foreach (var line in contentsArray)
- {
- writer.WriteLine(line);
- }
- writer.WriteLine(userName);
- writer.WriteLine(password);
- }
- Console.WriteLine("Account created! Press enter to return to main menu.");
- Console.ReadLine();
- Start();
- }
- static void Exit()
- {
- Console.WriteLine("Goodbye!");
- Console.ReadLine();
- Environment.Exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement