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.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace NEA
- {
- class Program
- {
- static void Main()
- {
- //Choose login or register
- Console.WriteLine("Please choose an option:\n1.Login\n2.Register");
- string choice = Console.ReadLine();
- switch(choice)
- {
- case "1":
- Login();
- break;
- case "2":
- Register();
- break;
- }
- }
- //Register method
- static void Register()
- {
- string usernameToFile = getUsername();
- string passwordToFile = getPassword();
- writeAccountToFile(usernameToFile,passwordToFile);
- //Local Function getUsername retrieves name, age and year group and forms the username
- string getUsername()
- {
- //Input name
- Console.Write("Enter name: ");
- string name = Console.ReadLine();
- //Input age
- Console.Write("Enter age: ");
- string age = Console.ReadLine();
- //Enter year group
- Console.Write("Enter year group: ");
- string yearGroup = Console.ReadLine();
- //Does the name contain less than 3 characters?
- string usernamePrefix;
- if (name.Length > 3)
- {
- usernamePrefix = name.Substring(0, 3);
- }
- else
- {
- usernamePrefix = name;
- }
- //Creating username
- string username = usernamePrefix + age;
- //Is the username a duplicate? CANNOT CREATE UNTIL ABLE TO WRITE TO FILE FOR FORMATTING REASONS
- int iterIfDupe = 0;
- bool isADupe = true;
- while (isADupe == true)
- {
- string[] lines = File.ReadAllLines(@"C:\Users\maxto\Desktop\accounts.txt");
- foreach (string line in lines)
- {
- if (username == line.Split(',')[0])
- {
- iterIfDupe++;
- username = iterIfDupe + usernamePrefix + age;
- }
- else
- {
- isADupe = false;
- }
- }
- }
- return username;
- }
- //Local Function getPassword retrieves the password from the user and checks that it is longer than 7 chars
- string getPassword()
- {
- //Input password
- Console.Write("\nEnter your new password: ");
- string password = Console.ReadLine();
- //Does the password contain 8 or more characters?
- if(password.Length < 8)
- {
- Console.WriteLine("Password must be at least 8 characters");
- getPassword();
- }
- return password;
- }
- //Local Function writeAccount will write the username and password to a file
- void writeAccountToFile(string username,string password)
- {
- using (StreamWriter accountWriter = new StreamWriter(@"C:\Users\maxto\Desktop\accounts.txt",true))
- {
- accountWriter.WriteLine($"{username},{password}");
- }
- }
- Main();
- }
- //Login method
- static void Login()
- {
- bool gotUsername = false;
- while (gotUsername == false)
- {
- //Getting username guess
- Console.Write("\nEnter username: ");
- string usernameGuess = Console.ReadLine();
- //Getting password guess
- Console.Write("Enter password: ");
- string passwordGuess = Console.ReadLine();
- //Checking if username is in account file
- string[] lines = File.ReadAllLines(@"C:\Users\maxto\Desktop\accounts.txt");
- foreach (string line in lines)
- {
- if (usernameGuess == line.Split(',')[0] && passwordGuess == line.Split(',')[1])
- {
- Console.WriteLine("username correct");
- gotUsername = true;
- Globals.loggedIn = true;
- }
- }
- }
- Main();
- }
- }
- public class Globals
- {
- public static bool loggedIn = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement