Reputation: 33
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.
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();
}
}
}
Upvotes: 0
Views: 1041
Reputation: 1861
A username must have at least one letter but a pin must have zero letters so you could use this answer from stackoverflow
bool isIntString = "your string".All(char.IsDigit)
Will return true if the string is a number
bool isIntString = !"your string".All(char.IsDigit)
Will return true if the string contains at least one non digit
https://stackoverflow.com/a/18251942/2826614
Upvotes: 1