Created
March 10, 2018 08:55
-
-
Save Miesvanderlippe/5e2cfa17e375f328a5ed657459b53b93 to your computer and use it in GitHub Desktop.
Solution 1: https://www.reddit.com/r/csharp/comments/83dvub/can_someone_help_me_on_this_homework_i_have/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace LoginFunction | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int numberOfUsers; | |
Console.WriteLine("How many users will you register? "); | |
// Makes sure the output is both valid and a number over zero. | |
while (!int.TryParse(Console.ReadLine(), out numberOfUsers) || numberOfUsers <= 0) | |
Console.WriteLine("Enter a valid number"); | |
// Made it a dict for easier comparing | |
// 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 | |
var users = new Dictionary<string, string>(); | |
// Use the actual length of the user dictionary to better reflect the amount of users we have | |
// as we sometimes skip a user if we already have them registered. | |
while(users.Count < numberOfUsers) | |
{ | |
// Ask for the inputs | |
Console.WriteLine("Please enter the user's name: "); | |
var username = Console.ReadLine(); | |
Console.WriteLine("Please enter the user's password:"); | |
var password = Console.ReadLine(); | |
// Check if we already have a user with that name | |
if (users.Keys.Contains(username)) | |
{ | |
Console.WriteLine("User already exists. Skipping."); | |
} | |
else | |
{ | |
users.Add(username, password); | |
Console.WriteLine("User {0} has been registered.", username); | |
} | |
} | |
Console.WriteLine("Please test a few of the user accounts?"); | |
// default value of bool is true | |
bool keepChecking; | |
// Showing off a different type of while loop. Doesn't add much value but it's cool for you to see anyways. | |
do | |
{ | |
Console.WriteLine("Please enter the user's name: "); | |
var username = Console.ReadLine(); | |
Console.WriteLine("Please enter the user's password:"); | |
var password = Console.ReadLine(); | |
// Short-hand expression. | |
// value = expression ? valueIfTrue : ValueIfFalse | |
// I use the return of theh check password function. | |
// For now we say the pass is wrong if we don't have the username stored. You could check that seperately. | |
Console.WriteLine(Program.CheckPassword(users, username, password) ? "Valid pass" : "Invalid pass"); | |
// Ask if we want to continue (uppercase value should be the default in console apps) | |
Console.WriteLine("Keep checking? (Y:n)"); | |
// Hence we check if the value is "n" and only stop when it is instead of checking if the value is "Y" | |
keepChecking = Console.ReadLine() != "n"; | |
} while (keepChecking); | |
} | |
public static bool CheckPassword(Dictionary<string, string> users, string username, string password) | |
{ | |
// 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. | |
if (!users.Keys.Contains(username)) | |
{ | |
return false; | |
} | |
// Check if the password matches | |
if (users[username] == password) | |
{ | |
return true; | |
} | |
// Always return false if we haven't hit the other cases. | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment