Guest User

Untitled

a guest
Mar 19th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace LoginFunction
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int numberOfUsers;
  12.  
  13. Console.WriteLine("How many users will you register? ");
  14.  
  15. // Makes sure the output is both valid and a number over zero.
  16. while (!int.TryParse(Console.ReadLine(), out numberOfUsers) || numberOfUsers <= 0)
  17. Console.WriteLine("Enter a valid number");
  18.  
  19. // Made it a dict for easier comparing
  20. // The key will be the username, the value will be the pass. In practice this will be a database record ID and a model with a collection of values
  21. var users = new Dictionary<string, string>();
  22.  
  23. // Use the actual length of the user dictionary to better reflect the amount of users we have
  24. // as we sometimes skip a user if we already have them registered.
  25. while(users.Count < numberOfUsers)
  26. {
  27. // Ask for the inputs
  28. Console.WriteLine("Please enter the user's name: ");
  29. var username = Console.ReadLine();
  30.  
  31. Console.WriteLine("Please enter the user's password:");
  32. var password = Console.ReadLine();
  33.  
  34. // Check if we already have a user with that name
  35. if (users.Keys.Contains(username))
  36. {
  37. Console.WriteLine("User already exists. Skipping.");
  38. }
  39. else
  40. {
  41. users.Add(username, password);
  42. Console.WriteLine("User {0} has been registered.", username);
  43. }
  44. }
  45.  
  46. Console.WriteLine("Please test a few of the user accounts?");
  47.  
  48. // default value of bool is true
  49. bool keepChecking;
  50.  
  51. // Showing off a different type of while loop. Doesn't add much value but it's cool for you to see anyways.
  52. do
  53. {
  54. Console.WriteLine("Please enter the user's name: ");
  55. var username = Console.ReadLine();
  56.  
  57. Console.WriteLine("Please enter the user's password:");
  58. var password = Console.ReadLine();
  59.  
  60. // Short-hand expression.
  61. // value = expression ? valueIfTrue : ValueIfFalse
  62. // I use the return of theh check password function.
  63. // For now we say the pass is wrong if we don't have the username stored. You could check that seperately.
  64. Console.WriteLine(Program.CheckPassword(users, username, password) ? "Valid pass" : "Invalid pass");
  65.  
  66. // Ask if we want to continue (uppercase value should be the default in console apps)
  67. Console.WriteLine("Keep checking? (Y:n)");
  68. // Hence we check if the value is "n" and only stop when it is instead of checking if the value is "Y"
  69. keepChecking = Console.ReadLine() != "n";
  70. } while (keepChecking);
  71.  
  72. }
  73.  
  74. public static bool CheckPassword(Dictionary<string, string> users, string username, string password)
  75. {
  76. // See if we have the user at all. You'll get a key error when this isn't the case and you try to read the password anyways.
  77. if (!users.Keys.Contains(username))
  78. {
  79. return false;
  80. }
  81.  
  82. // Check if the password matches
  83. if (users[username] == password)
  84. {
  85. return true;
  86. }
  87.  
  88. // Always return false if we haven't hit the other cases.
  89. return false;
  90. }
  91. }
  92. }
Add Comment
Please, Sign In to add comment