I'm trying to implement a little of everything I have learned so far in two weeks of C# into a mock ATM type of app. So keep in mind while there are better ways of doing my code or simplifying it, I'm trying to stick to what I know for now so I can understand it better.
- So here's what I'm trying to do at the moment: User enters a username (userName) and I need to check if it matches correctUserName, which I know to do with an if statement, however, I need it to also be able to recognize if the user has entered a string and if anything else it will give an error. With an int I know I can try int.TryParse, I don't know what the string equivalent of that is.
- User inputs pin (pinCode) and similar to above, but I know how to check if that's a number and not a string and compare it to correctPinCode; I don't understand how to display two different error messages based on that condition. Where one error message if the user didn't enter a numerical value(pinNumericalError) and the other if the pin just didn't match(pinError). I'm assuming two "if statements" in the else statement (is that even possible?), but I don't know what the syntax looks like with the TryParse condition above it.
- Then once both userName and pinCode are correct (based on the conditions above) I can proceed to a menu or whatever it is I plan to do next. Do both go nested in a parent while loop or something?
namespace ATM
{
class AtmLogin
{
static void Main()
{
//User input
string userName;
int pinCode;
//Correct values to proceed
string correctUserName = "Chad Warden";
int correctPinCode = 6969;
//Error messages
string userNameError = "Enter a valid username";
string pinError = "Enter a valid pin";
string pinNumericalError = "Enter a valid numerical pin";
string namePinError = "Either your username or pin was invalid, try again.";
//Welcome message
Console.WriteLine("Welcome to PSTriple Banking!");
//Username prompt - checks if userName is a string and not a number or anything else and compares it to correctUserName
Console.WriteLine("User name:");
userName = Console.ReadLine();
//Pin prompt - checks if pinCode is a number and not a string or anything else and compares it to correctPinCode
while (true)
{
Console.WriteLine("Pin:");
if (int.TryParse(Console.ReadLine(), out pinCode) && pinCode == correctPinCode) {
break;
}
else
{
Console.WriteLine(pinError);
}
}
Console.ReadKey();
}
}
}