# (DD15) Business Logic — InputUtils.readInt() [10 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `util.InputUtils` |
| Layer | Utility |
| Module | `util` (Package: `util`) |

## 1. Role

### InputUtils.readInt()

`InputUtils.readInt(String prompt)` is a shared console input utility that accepts a caller-provided message, displays it to the user, and then repeatedly validates keyboard input until a numeric integer is entered. Its primary business role is to protect downstream screens from non-numeric user input by enforcing a simple read-and-validate interaction at the point of entry. This method implements a lightweight input-routing pattern: it shows the initial prompt, checks whether the next token can be parsed as an integer, discards invalid text lines, and re-prompts the user with a standard correction message when needed.

In the larger system, this method acts as a reusable input gate for console-driven UI classes, ensuring that menu selection and other integer-based choices are captured in a consistent way. It does not branch by business domain or transaction type; instead, it handles one service category only: integer input collection with validation and retry. The method also normalizes console state by consuming the trailing newline after successful parsing, which prevents subsequent reads from inheriting leftover line terminators. Because it is static and utility-based, it is designed for broad reuse across screens rather than for encapsulating a screen-specific business process.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["readInt(prompt)"])
    SHOW_PROMPT["Print the caller-supplied prompt"]
    CHECK_INT{"scanner.hasNextInt()"}
    DISCARD_LINE["scanner.nextLine() - discard invalid input line"]
    SHOW_RETRY["Print retry prompt - Please enter a number:"]
    READ_INT["scanner.nextInt() - read the numeric value"]
    CLEAR_LINE["scanner.nextLine() - consume trailing newline"]
    RETURN_VALUE["Return int value"]

    START --> SHOW_PROMPT
    SHOW_PROMPT --> CHECK_INT
    CHECK_INT -- "false" --> DISCARD_LINE
    DISCARD_LINE --> SHOW_RETRY
    SHOW_RETRY --> CHECK_INT
    CHECK_INT -- "true" --> READ_INT
    READ_INT --> CLEAR_LINE
    CLEAR_LINE --> RETURN_VALUE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `prompt` | `String` | A caller-defined console message that instructs the user what numeric value to enter. It can contain menu text, selection guidance, or any screen-specific instruction. The value does not change control flow directly, but it determines the initial user-facing prompt shown before validation begins. |

External state read by the method:
- `scanner` — shared console scanner used to validate and consume user input

## 4. CRUD Operations / Called Services

This method does not perform CRUD against persistent data stores and does not invoke application services, CBS components, or database entities. Its only executable dependencies are console I/O operations on the shared `scanner` instance.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| R | `scanner.hasNextInt()` | N/A | N/A | Checks whether the next console token can be parsed as an integer before allowing the read to proceed. |
| R | `scanner.nextLine()` | N/A | N/A | Consumes the remaining input line after invalid input to clear the console buffer. |
| C | `scanner.nextInt()` | N/A | N/A | Reads the validated integer value from the console input stream. |
| R | `scanner.nextLine()` | N/A | N/A | Consumes the trailing newline after a successful integer read so the next input operation starts cleanly. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen:ConsoleUI | `ConsoleUI` -> `InputUtils.readInt` | `scanner.nextInt() [C] N/A` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(method entry)` (L13)

> Reads a numeric value from the console after displaying a caller-supplied prompt.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `System.out.print(prompt);` // display the caller-provided instruction |
| 2 | WHILE | `while (!scanner.hasNextInt())` // continue until a valid integer token is available |

**Block 1.1** — [WHILE] `(!scanner.hasNextInt())` (L14)

> Handles invalid console input by discarding the bad line and re-prompting the user.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `scanner.nextLine();` // discard the invalid entry from the input buffer |
| 2 | EXEC | `System.out.print("Please enter a number: ");` // show the standard retry message |

**Block 2** — [SEQUENCE] `(valid integer available)` (L17)

> Reads the validated integer and cleans up the remaining newline character.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int value = scanner.nextInt();` // capture the validated integer from the console |
| 2 | EXEC | `scanner.nextLine();` // consume the trailing newline after numeric input |
| 3 | RETURN | `return value;` // return the parsed integer to the caller |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|-------------------|
| `prompt` | Field | Console instruction text shown to the user before input is read. |
| `scanner` | Technical term | Shared console input reader used to capture and validate keyboard input. |
| integer | Data type | Whole number used for menu selection or other numeric user choices. |
| line terminator | Technical term | Newline character left in the input buffer after reading typed data; consumed to keep the next read aligned. |
| console UI | Business term | Text-based user interface that interacts with the user through terminal input and output. |
| validation loop | Technical term | Repeated check that rejects invalid input until a valid integer is provided. |
