# (DD17) Business Logic — LoanService.returnBook() [9 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `service.LoanService` |
| Layer | Service |
| Module | `service` (Package: `service`) |

## 1. Role

### LoanService.returnBook()

This method performs the core business operation for returning an active loan record in the library domain. It receives a loan identifier, searches the in-memory loan collection for a matching loan that has not yet been returned, and then applies the return workflow to that loan. The workflow transitions the loan from an active borrowing state to a returned state and also makes the associated book available again for future borrowing.

From a design perspective, the method implements a simple routing-and-delegation pattern: it routes by loan identity and returned status, then delegates the state transition to the domain object `Loan`. It serves as a service-layer entry point used by the user interface to complete the return transaction. The method has two business branches: one for the case where no active loan is found, and one for the case where the loan exists and can be returned successfully.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["returnBook(loanId)"]
    FIND["Filter loans by matching loanId and returned = false"]
    CHECK{ "Active loan found?" }
    NOT_FOUND["Return 'Active loan not found: ' + loanId"]
    CALL_RETURN["loan.returnBook()"]
    SET_RETURN_DATE["Set returnDate = LocalDate.now()"]
    SET_RETURNED["Set returned = true"]
    SET_BOOK_AVAILABLE["Set book.available = true"]
    SUCCESS["Return 'Book returned successfully.'"]
    END_NODE["END"]
    START --> FIND
    FIND --> CHECK
    CHECK -- "No" --> NOT_FOUND
    CHECK -- "Yes" --> CALL_RETURN
    CALL_RETURN --> SET_RETURN_DATE
    SET_RETURN_DATE --> SET_RETURNED
    SET_RETURNED --> SET_BOOK_AVAILABLE
    SET_BOOK_AVAILABLE --> SUCCESS
    NOT_FOUND --> END_NODE
    SUCCESS --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `loanId` | `String` | Loan identifier supplied by the operator or UI to specify which borrowing record should be returned. It must match an active loan entry in the service's loan list; if no active record matches, the method returns a not-found message and performs no state change. |

External state read by the method: the service's loan collection (`loans`), each loan's `id`, each loan's returned flag, and the selected loan's associated book object.

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `Loan.returnBook` | Loan | - | Calls `returnBook` in `Loan` |
| - | `LoanService.returnBook` | LoanService | Loan | Calls `returnBook` in `LoanService` |
| - | `ConsoleUI.returnBook` | ConsoleUI | - | Calls `returnBook` in `ConsoleUI` |

Analyze all method calls within this method and classify each as a CRUD operation.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `stream().filter().findFirst()` | LoanService | `loans` collection | Searches the in-memory loan list for an active loan whose ID matches the input `loanId`. |
| C | `Loan.returnBook()` | Loan | Loan / Book state | Delegates the return business action to the domain object, which sets the return date, marks the loan returned, and makes the book available. |
| U | `Loan.returnBook()` | Loan | Loan / Book state | Updates the selected loan and its associated book from active/borrowed state to returned/available state. |

## 5. Dependency Trace

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

No screen/batch entry points found within 8 hops. Direct callers found: 2 methods.
Terminal operations from this method: `returnBook` [-], `returnBook` [-], `returnBook` [-]

Trace who calls this method and what this method ultimately calls.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen:ConsoleUI | `ConsoleUI.returnBook()` -> `LoanService.returnBook(String)` | `stream().filter().findFirst() [R] loans collection` |
| 2 | Screen:ConsoleUI | `ConsoleUI.returnBook()` -> `LoanService.returnBook(String)` -> `Loan.returnBook()` | `Loan.returnBook() [U] Loan and Book state` |

## 6. Per-Branch Detail Blocks

**Block 1** — [IF] `(loan.isEmpty())` (L41)

> This branch handles the business case where the requested loan is not currently active or does not exist.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return "Active loan not found: " + loanId;` |

**Block 2** — [ELSE] `(loan.isPresent())` (L42-43)

> This branch handles the successful return workflow for an active loan.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `loan.get().returnBook();` |
| 2 | RETURN | `return "Book returned successfully.";` |

**Block 2.1** — [METHOD CALL] `Loan.returnBook()` (L43)

> The domain object updates its own return metadata and makes the associated book available again.

| # | Type | Code |
|---|------|------|
| 1 | SET | `this.returnDate = LocalDate.now();` |
| 2 | SET | `this.returned = true;` |
| 3 | EXEC | `this.book.setAvailable(true);` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|-------------------|
| `loanId` | Field | Loan identifier used to select the borrowing record to be returned. |
| `loans` | Field | In-memory collection of loan records managed by the service. |
| `id` | Field | Unique identifier of a loan record. |
| `returned` | Field | Status flag indicating whether the loan has already been returned. |
| `returnDate` | Field | Date the loan was returned. |
| `book` | Field | The book associated with the loan. |
| `available` | Field | Book availability flag indicating whether it can be borrowed again. |
| `Loan` | Domain object | Loan record representing a borrowed book and its borrower. |
| `Book` | Domain object | Library item that can be borrowed and returned. |
| `LocalDate` | Technical term | Java date type used to record the return date. |
| `ConsoleUI` | UI class | Console user interface that collects the loan ID and invokes the service. |
| Service | Layer | Business logic layer that coordinates domain operations. |