# (DD01) Business Logic — ConsoleUI.start() [21 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `ui.ConsoleUI` |
| Layer | UI / Presentation |
| Module | `ui` (Package: `ui`) |

## 1. Role

### ConsoleUI.start()

`start()` is the interactive entry point for the console-based library management application. It initializes the user-facing session, prints the application title, and then repeatedly presents the main menu until the operator chooses to exit. From a business perspective, this method is responsible for routing user intent into the correct library operation category: book registration, book inquiry, title search, member registration, member inquiry, borrowing, returning, and loan inquiry.

The method implements a classic menu-driven dispatch pattern. It does not perform domain persistence or business calculations itself; instead, it coordinates the UI loop, captures the operator's selection, and delegates to specialized screen actions such as `addBook()`, `listBooks()`, `searchBooks()`, `registerMember()`, `listMembers()`, `borrowBook()`, `returnBook()`, and `listLoans()`. A `running` flag controls the lifecycle of the session, and the loop continues until the user explicitly selects the exit option. When the session ends, the input utility is closed as part of UI cleanup.

In the larger system, this method acts as the top-level console controller for the application. It is the method invoked from `Main.main()` and serves as the single entry point for operator interaction after the application services are initialized. Its role is therefore to connect the application bootstrap layer to the service-backed library operations through a simple and predictable command routing mechanism.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["start()"]
    PRINT_TITLE["Print application title - === Library Management System ==="]
    SET_RUNNING["Set running = true"]
    LOOP_CHECK{"running is true"}
    PRINT_MENU["Call printMenu()"]
    READ_CHOICE["Read choice via InputUtils.readInt(\"Enter choice: \")"]
    SWITCH_CHOICE{"Switch on choice"}
    CASE_1["choice = 1 - addBook()"]
    CASE_2["choice = 2 - listBooks()"]
    CASE_3["choice = 3 - searchBooks()"]
    CASE_4["choice = 4 - registerMember()"]
    CASE_5["choice = 5 - listMembers()"]
    CASE_6["choice = 6 - borrowBook()"]
    CASE_7["choice = 7 - returnBook()"]
    CASE_8["choice = 8 - listLoans()"]
    CASE_0["choice = 0 - set running = false and print Goodbye!"]
    DEFAULT["Print Invalid choice."]
    CLOSE_INPUT["Call InputUtils.close()"]
    END_NODE["Return"]
    START --> PRINT_TITLE
    PRINT_TITLE --> SET_RUNNING
    SET_RUNNING --> LOOP_CHECK
    LOOP_CHECK -->|true| PRINT_MENU
    LOOP_CHECK -->|false| CLOSE_INPUT
    PRINT_MENU --> READ_CHOICE
    READ_CHOICE --> SWITCH_CHOICE
    SWITCH_CHOICE --> CASE_1
    SWITCH_CHOICE --> CASE_2
    SWITCH_CHOICE --> CASE_3
    SWITCH_CHOICE --> CASE_4
    SWITCH_CHOICE --> CASE_5
    SWITCH_CHOICE --> CASE_6
    SWITCH_CHOICE --> CASE_7
    SWITCH_CHOICE --> CASE_8
    SWITCH_CHOICE --> CASE_0
    SWITCH_CHOICE --> DEFAULT
    CASE_1 --> LOOP_CHECK
    CASE_2 --> LOOP_CHECK
    CASE_3 --> LOOP_CHECK
    CASE_4 --> LOOP_CHECK
    CASE_5 --> LOOP_CHECK
    CASE_6 --> LOOP_CHECK
    CASE_7 --> LOOP_CHECK
    CASE_8 --> LOOP_CHECK
    CASE_0 --> LOOP_CHECK
    DEFAULT --> LOOP_CHECK
    CLOSE_INPUT --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | - |

This method has no parameters. Its behavior is driven by internal session state (`running`) and external operator input read from the console. The method also depends on the instance-level services passed into `ConsoleUI` at construction time, because the delegated menu actions use those services to perform library operations.

## 4. CRUD Operations / Called Services

### Pre-computed evidence from code analysis graph:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `ConsoleUI.printMenu` | ConsoleUI | - | Displays the main menu to the operator |
| C | `ConsoleUI.addBook` | ConsoleUI | Book | Opens the book registration flow from the console menu |
| R | `ConsoleUI.listBooks` | ConsoleUI | Book | Opens the book list inquiry flow from the console menu |
| R | `ConsoleUI.searchBooks` | ConsoleUI | Book | Opens the book search flow from the console menu |
| C | `ConsoleUI.registerMember` | ConsoleUI | Member | Opens the member registration flow from the console menu |
| R | `ConsoleUI.listMembers` | ConsoleUI | Member | Opens the member list inquiry flow from the console menu |
| - | `ConsoleUI.borrowBook` | ConsoleUI | Loan | Opens the borrowing flow from the console menu |
| - | `ConsoleUI.returnBook` | ConsoleUI | Loan | Opens the return flow from the console menu |
| R | `ConsoleUI.listLoans` | ConsoleUI | Loan | Opens the loan list inquiry flow from the console menu |

`start()` itself does not directly perform database CRUD. Instead, it routes to downstream UI actions that are categorized by business intent: create registrations for books and members, read inquiries for books, members, and loans, and operational flows for borrowing and returning. The method also performs console I/O control by reading operator selection and closing the input stream when the session ends.

## 5. Dependency Trace

### Pre-computed evidence from code analysis graph:

No screen/batch entry points found within 8 hops. Direct callers found: 1 methods.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | `Main` | `Main.main()` -> `new ConsoleUI(libraryService, loanService).start()` -> `ConsoleUI.start()` | `ConsoleUI.addBook [C] Book`, `ConsoleUI.listBooks [R] Book`, `ConsoleUI.searchBooks [R] Book`, `ConsoleUI.registerMember [C] Member`, `ConsoleUI.listMembers [R] Member`, `ConsoleUI.borrowBook [-] Loan`, `ConsoleUI.returnBook [-] Loan`, `ConsoleUI.listLoans [R] Loan`, `ConsoleUI.printMenu [-] -` |

`Main.main()` is the only discovered caller and it invokes `start()` immediately after service initialization and seed-data setup. From there, the method becomes the interactive dispatcher that reaches the listed terminal UI operations depending on the operator's selection.

## 6. Per-Branch Detail Blocks

**Block 1** — Sequence initialization `(L21-L22)`

> Initializes the console session banner and the loop guard that controls the lifetime of the UI.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `System.out.println("=== Library Management System ===");` // prints the application title |
| 2 | SET | `boolean running = true;` // opens the main loop |

**Block 2** — `WHILE` `(running)` `(L23-L38)`

> Continues to present the menu until the operator selects exit.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `printMenu();` // displays the menu options |
| 2 | EXEC | `int choice = InputUtils.readInt("Enter choice: ");` // reads the operator's command |
| 3 | SWITCH | `switch (choice)` // dispatches to the selected business flow |

**Block 2.1** — `CASE` `(choice = 1)` `(L26)`

> Opens the book registration flow.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `addBook();` // navigates to book registration |

**Block 2.2** — `CASE` `(choice = 2)` `(L27)`

> Opens the book list inquiry flow.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `listBooks();` // navigates to book inquiry |

**Block 2.3** — `CASE` `(choice = 3)` `(L28)`

> Opens the book search flow.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `searchBooks();` // navigates to title search |

**Block 2.4** — `CASE` `(choice = 4)` `(L29)`

> Opens the member registration flow.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `registerMember();` // navigates to member registration |

**Block 2.5** — `CASE` `(choice = 5)` `(L30)`

> Opens the member list inquiry flow.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `listMembers();` // navigates to member inquiry |

**Block 2.6** — `CASE` `(choice = 6)` `(L31)`

> Opens the borrowing flow.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `borrowBook();` // navigates to book borrowing |

**Block 2.7** — `CASE` `(choice = 7)` `(L32)`

> Opens the return flow.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `returnBook();` // navigates to book return |

**Block 2.8** — `CASE` `(choice = 8)` `(L33)`

> Opens the loan list inquiry flow.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `listLoans();` // navigates to loan inquiry |

**Block 2.9** — `CASE` `(choice = 0)` `(L34)`

> Ends the console session.

| # | Type | Code |
|---|------|------|
| 1 | SET | `running = false;` // stops the main loop |
| 2 | EXEC | `System.out.println("Goodbye!");` // confirms exit to the operator |

**Block 2.10** — `DEFAULT` `(choice != 0..8)` `(L35)`

> Handles invalid menu selections.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `System.out.println("Invalid choice.");` // informs the operator that the selection is not supported |

**Block 3** — Session cleanup `(L38)`

> Closes shared console input resources after the loop finishes.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `InputUtils.close();` // releases the input stream |
| 2 | RETURN | `return;` // method ends |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `ConsoleUI` | Component | Console-based user interface layer that handles operator interaction for the library application |
| `start` | Method | Main console session entry point that starts the menu loop and dispatches user actions |
| `printMenu` | Method | Displays the available library operations to the operator |
| `addBook` | Method | Launches the book registration flow |
| `listBooks` | Method | Launches the book inquiry flow |
| `searchBooks` | Method | Launches the book title search flow |
| `registerMember` | Method | Launches the member registration flow |
| `listMembers` | Method | Launches the member inquiry flow |
| `borrowBook` | Method | Launches the book borrowing flow |
| `returnBook` | Method | Launches the book return flow |
| `listLoans` | Method | Launches the loan inquiry flow |
| `running` | Field | Session control flag that keeps the console menu active until exit is chosen |
| `choice` | Variable | Operator-selected menu command that determines which business function runs next |
| `InputUtils` | Utility | Console input helper used to read integers and manage input lifecycle |
| `Book` | Domain term | Cataloged library item available for registration, search, and listing |
| `Member` | Domain term | Registered library customer or patron |
| `Loan` | Domain term | Borrowing transaction that links a member to a book |
| `Main` | Entry point | Application bootstrap class that creates services and starts the console UI |
| CRUD | Acronym | Create, Read, Update, Delete — standard classification for data operations |
| UI | Acronym | User Interface — presentation layer used by operators to interact with the system |
| Service | Layer term | Business logic layer invoked by the UI to execute domain actions |
| Console | Business term | Terminal-based interaction mode where commands are entered through standard input |
