Skip to content

YuriiJavaDev/JavaBasics_Task_53_V0.1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

ATM PIN Validator (JavaBasics_Task_53_V0.1)

📖 Description

This project simulates an ATM user interface for PIN-code validation. It utilizes the Do-While Loop to repeatedly request input from the user until a valid four-digit positive integer (between 1000 and 9999) is provided.

The goal is to master complex range validation within post-condition loops.

📋 Requirements Compliance

Task 53: Request a PIN code until the user enters a four-digit positive number.

Requirements:

  • Initialize Scanner for standard input.
  • Use a do-while loop to guarantee at least one prompt.
  • Read integer input using scanner.nextInt().
  • Set the loop condition to repeat as long as the input is outside the range 1000 to 9999.
  • Print "PIN accepted." only after a valid four-digit PIN is received.

🚀 Architectural Stack

  • Input Stream Management: Utilizing java.util.Scanner for interactive CLI.
  • Range Validation: Implementing a "Repeat-Until-InRange" pattern for secure input.
  • Resource Control: Managing scanner lifecycle (close) to ensure memory efficiency.

🏗️ Implementation Details

  • Logic Flow: The do block immediately displays the prompt. After pin = scanner.nextInt(), the condition pin < 1000 || pin > 9999 is checked. If the number is too small (e.g., 1) or too large (e.g., 10000), the loop repeats.
  • Condition Strategy: The logical OR (||) ensures that any value falling outside the valid boundaries triggers another iteration.
  • ⚠️ Clean Code Warning: This implementation assumes the user enters integers. For a production environment, one should handle InputMismatchException to prevent crashes when non-numeric data is entered.

🛠 Features

  • Strict Range Enforcement: Only accepts integers with exactly 4 digits.
  • Interactive Interface: Real-time communication with the user via console.
  • Explicit Success Feedback: Confirms when the requirement is finally met.

📋 Expected result

Please enter your 4-digit PIN (1000-9999):
-42
Please enter your 4-digit PIN (1000-9999):
1
Please enter your 4-digit PIN (1000-9999):
10000
Please enter your 4-digit PIN (1000-9999):
1234
PIN accepted.

💻 Code Example

Project Structure:

src/com/yurii/pavlenko/
                └── AtmPinValidator.java # Main Entry Point & Logic

Code

package com.yurii.pavlenko;

import java.util.Scanner;

public class AtmPinValidator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int pin;
        do {
            System.out.println("Please enter your 4-digit PIN (1000-9999):");
            pin = scanner.nextInt();
        } while (pin < 1000 || pin > 9999);
        System.out.println("PIN accepted.");
        scanner.close();
    }
}

⚖️ License

This project is licensed under the MIT License.

Copyright (c) 2026 Yurii Pavlenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...

License: MIT

About

This is a tutorial project. JavaBasics_Task_53_V0.1 Implementing an ATM PIN validator with 4-digit range enforcement using a do-while loop. 270126_0854

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages