Advertisement
Guest User

Untitled

a guest
Feb 7th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace NEA
  9. {
  10. class Program
  11. {
  12. static void Main()
  13. {
  14. //Choose login or register
  15.  
  16. Console.WriteLine("Please choose an option:\n1.Login\n2.Register");
  17. string choice = Console.ReadLine();
  18.  
  19. switch(choice)
  20. {
  21. case "1":
  22. Login();
  23. break;
  24. case "2":
  25. Register();
  26. break;
  27. }
  28. }
  29.  
  30. //Register method
  31. static void Register()
  32. {
  33. string usernameToFile = getUsername();
  34. string passwordToFile = getPassword();
  35. writeAccountToFile(usernameToFile,passwordToFile);
  36.  
  37. //Local Function getUsername retrieves name, age and year group and forms the username
  38. string getUsername()
  39. {
  40. //Input name
  41. Console.Write("Enter name: ");
  42. string name = Console.ReadLine();
  43.  
  44. //Input age
  45. Console.Write("Enter age: ");
  46. string age = Console.ReadLine();
  47.  
  48. //Enter year group
  49. Console.Write("Enter year group: ");
  50. string yearGroup = Console.ReadLine();
  51.  
  52. //Does the name contain less than 3 characters?
  53. string usernamePrefix;
  54. if (name.Length > 3)
  55. {
  56. usernamePrefix = name.Substring(0, 3);
  57. }
  58. else
  59. {
  60. usernamePrefix = name;
  61. }
  62.  
  63. //Creating username
  64. string username = usernamePrefix + age;
  65.  
  66. //Is the username a duplicate? CANNOT CREATE UNTIL ABLE TO WRITE TO FILE FOR FORMATTING REASONS
  67. int iterIfDupe = 0;
  68. bool isADupe = true;
  69. while (isADupe == true)
  70. {
  71. string[] lines = File.ReadAllLines(@"C:\Users\maxto\Desktop\accounts.txt");
  72. foreach (string line in lines)
  73. {
  74. if (username == line.Split(',')[0])
  75. {
  76. iterIfDupe++;
  77. username = iterIfDupe + usernamePrefix + age;
  78. }
  79. else
  80. {
  81. isADupe = false;
  82. }
  83. }
  84. }
  85.  
  86. return username;
  87. }
  88.  
  89. //Local Function getPassword retrieves the password from the user and checks that it is longer than 7 chars
  90. string getPassword()
  91. {
  92. //Input password
  93. Console.Write("\nEnter your new password: ");
  94. string password = Console.ReadLine();
  95.  
  96. //Does the password contain 8 or more characters?
  97. if(password.Length < 8)
  98. {
  99. Console.WriteLine("Password must be at least 8 characters");
  100. getPassword();
  101. }
  102. return password;
  103. }
  104.  
  105. //Local Function writeAccount will write the username and password to a file
  106. void writeAccountToFile(string username,string password)
  107. {
  108. using (StreamWriter accountWriter = new StreamWriter(@"C:\Users\maxto\Desktop\accounts.txt",true))
  109. {
  110. accountWriter.WriteLine($"{username},{password}");
  111. }
  112. }
  113. Main();
  114. }
  115.  
  116. //Login method
  117. static void Login()
  118. {
  119. bool gotUsername = false;
  120.  
  121. while (gotUsername == false)
  122. {
  123. //Getting username guess
  124. Console.Write("\nEnter username: ");
  125. string usernameGuess = Console.ReadLine();
  126.  
  127. //Getting password guess
  128. Console.Write("Enter password: ");
  129. string passwordGuess = Console.ReadLine();
  130.  
  131. //Checking if username is in account file
  132. string[] lines = File.ReadAllLines(@"C:\Users\maxto\Desktop\accounts.txt");
  133. foreach (string line in lines)
  134. {
  135. if (usernameGuess == line.Split(',')[0] && passwordGuess == line.Split(',')[1])
  136. {
  137. Console.WriteLine("username correct");
  138. gotUsername = true;
  139. Globals.loggedIn = true;
  140. }
  141. }
  142. }
  143. Main();
  144. }
  145. }
  146. public class Globals
  147. {
  148. public static bool loggedIn = false;
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement