---

# (DD12) Business Logic — Loan.toString() [8 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `model.Loan` |
| Layer | Utility / Domain Model |
| Module | `model` (Package: `model`) |

## 1. Role

### Loan.toString()

`Loan.toString()` provides the human-readable presentation of a loan transaction record. It combines the loan identifier, the borrowing member’s display name, the borrowed book title, and a status phrase that indicates whether the item has been returned. In business terms, this method turns the raw loan entity into a concise summary suitable for logging, console output, UI display, or debugging support.

The method implements a simple presentation/formatting pattern rather than any workflow orchestration. It does not mutate state, persist data, or invoke downstream services; instead, it reads the current in-memory loan state and renders one of two narratives: an active borrowed item or a completed returned item. This makes the method a shared display helper for any component that needs to show loan status in a single line.

The branching logic is driven by the `returned` flag. If the loan has been returned, the output uses the return date and labels the loan as returned. Otherwise, the output uses the borrow date and explicitly marks the loan as not returned. The larger system uses this method as a lightweight domain formatter for loan visibility.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START(["toString()"])
COND1{"returned?"}
STEP1["Build status text from returnDate or borrowDate"]
STEP2["Format Loan summary using id, member name, book title, and status"]
END_NODE(["Return String"])
START --> COND1
COND1 -->|Yes| STEP1
COND1 -->|No| STEP1
STEP1 --> STEP2
STEP2 --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method has no parameters. It renders the current loan instance using instance fields: `returned`, `returnDate`, `borrowDate`, `id`, `member`, and `book`. |

## 4. CRUD Operations / Called Services

`Loan.toString()` does not perform CRUD operations and does not call external services. It only performs read-only access to object state and formats a string.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `getName()` | - | `Member` | Reads the member name for display in the loan summary. |
| R | `getTitle()` | - | `Book` | Reads the book title for display in the loan summary. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | `LoanService` | `LoanService.getActiveLoans` or `LoanService.borrowBook` / `returnBook` -> `Loan.toString()` | `getName() [R] Member`, `getTitle() [R] Book` |
| 2 | `Book`-adjacent presentation usage | Any caller rendering a `Loan` instance -> `Loan.toString()` | `getName() [R] Member`, `getTitle() [R] Book` |

## 6. Per-Branch Detail Blocks

**Block 1** — [IF/ELSE] `(returned)` (L33)

> Determines whether the loan is still active or has already been closed.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String status = returned ? ... : ...;` |

**Block 1.1** — [THEN] `(returned == true)` (L34-L35)

> Builds the returned-loan status message using the return date.

| # | Type | Code |
|---|------|------|
| 1 | SET | `status = "Returned on " + returnDate;` |

**Block 1.2** — [ELSE] `(returned == false)` (L36-L37)

> Builds the active-loan status message using the borrow date and the not-returned marker.

| # | Type | Code |
|---|------|------|
| 1 | SET | `status = "Borrowed on " + borrowDate + " (not returned)";` |

**Block 2** — [RETURN] `(always)` (L38-L39)

> Formats the final loan summary string from the loan ID, member name, book title, and the computed status.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return String.format("Loan[%s] %s -> \"%s\" | %s", id, member.getName(), book.getTitle(), status);` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `returned` | Field | Loan completion flag — indicates whether the borrowed book has been returned. |
| `returnDate` | Field | Return date — the date/time when the loan was closed. |
| `borrowDate` | Field | Borrow date — the date/time when the loan started. |
| `id` | Field | Loan identifier — unique business key for the loan transaction. |
| `member` | Field | Borrowing member reference — the customer who checked out the book. |
| `book` | Field | Borrowed book reference — the item being loaned. |
| `Loan` | Business term | Loan transaction — the record representing a book checkout lifecycle. |
| `Member` | Business term | Library member — the person who borrows books. |
| `Book` | Business term | Library book — the item being borrowed and returned. |
