Solution: Secure ATM PIN Validation

Review the implementation of loop-based state management and early exit strategies to build a robust security validation flow.

Solution: Secure ATM PIN Validation

Review the implementation of loop-based state management and early exit strategies to build a robust security validation flow.
const int correctPin = 1234;
int attempts = 0;
const int maxAttempts = 3;
bool isAuthenticated = false;

Console.WriteLine("=== ATM PIN Validation ===");

while (attempts < maxAttempts)
{
    attempts++;
    Console.Write($"Attempt {attempts}/{maxAttempts}. Enter PIN: ");
    string input = Console.ReadLine();

    if (int.TryParse(input, out int enteredPin))
    {
        if (enteredPin == correctPin)
        {
            isAuthenticated = true;
            break; // Exit the loop immediately on success
        }
        else
        {
            Console.WriteLine("Incorrect PIN.");
        }
    }
    else
    {
        Console.WriteLine("Invalid input. Please enter numbers only.");
    }
}

if (isAuthenticated)
{
    Console.WriteLine("Access Granted. Welcome back!");
}
else
{
    Console.WriteLine("Too many failed attempts. Your account is locked.");
}
The complete solution of the ATM PIN validation logic using a while loop and break statement