# (DD01) Business Logic — UI.button() [37 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `com.github.blaxk3.ui.UI` |
| Layer | Utility / Presentation Component |
| Module | `ui` (Package: `com.github.blaxk3.ui`) |

## 1. Role

### UI.button()

This method builds the three primary action buttons for the currency conversion screen: **Convert**, **Swap**, and **Clear**. It is responsible for wiring the user interface controls that drive the screen’s core business interactions: converting an entered amount from one currency to another, swapping the source and target currencies, and resetting the form for a new calculation.

From a business perspective, the method acts as a lightweight presentation-layer dispatcher. It does not own conversion logic itself; instead, it connects the UI events to the `CurrencyRateAPI` service for rate conversion and to local UI state for swapping and clearing values. The method also enforces a minimal input guard by preventing conversion when the amount field is empty or contains only a single dot, which avoids invalid requests and gives the user immediate feedback.

The method follows an event-binding pattern: each button is created, sized consistently, and then assigned an action listener that performs one distinct business function. In the larger system, this method is part of the reusable screen assembly logic used by the `UI` class to render the main application panel and keep the user interaction flow simple and predictable.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["button()"]
    CREATE["Create JButton array: Convert, Swap, Clear"]
    LOOP["For each button - set preferred size to 200x35"]
    API["Instantiate CurrencyRateAPI"]
    CONVERT_BTN["Attach Convert button action listener"]
    SWAP_BTN["Attach Swap button action listener"]
    CLEAR_BTN["Attach Clear button action listener"]
    COND["textField is not empty and not '.'"]
    CALL_CONVERT["Call rate.convert(sourceCurrency, targetCurrency, amount)"]
    FORMAT["Format converted amount with DecimalFormat '#,###.###'"]
    SET_LABEL["setLabel(formatted converted amount)"]
    ERR["Show error dialog - Please enter the amount you need"]
    SWAP_STORE["Store current source currency from comboBox1"]
    SWAP_SET1["Set comboBox1 to comboBox2 selection"]
    SWAP_SET2["Set comboBox2 to stored source currency"]
    CLEAR_SET1["setTextField(\"\")"]
    CLEAR_SET2["setLabel(\"\")"]
    RETURN["Return Component[]"]

    START --> CREATE
    CREATE --> LOOP
    LOOP --> API
    API --> CONVERT_BTN
    CONVERT_BTN --> SWAP_BTN
    SWAP_BTN --> CLEAR_BTN
    CLEAR_BTN --> RETURN
    CONVERT_BTN --> COND
    COND -- "true" --> CALL_CONVERT
    CALL_CONVERT --> FORMAT
    FORMAT --> SET_LABEL
    SET_LABEL --> RETURN
    COND -- "false" --> ERR
    ERR --> RETURN
    SWAP_BTN --> SWAP_STORE
    SWAP_STORE --> SWAP_SET1
    SWAP_SET1 --> SWAP_SET2
    SWAP_SET2 --> RETURN
    CLEAR_BTN --> CLEAR_SET1
    CLEAR_SET1 --> CLEAR_SET2
    CLEAR_SET2 --> RETURN
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method takes no parameters. It operates entirely on the UI state held by the `UI` instance, including the source currency combo box, target currency combo box, amount text field, and result label. |

External state read by the method:
- `textField` - the entered amount used for conversion and validation
- `label` - the result display updated after conversion or cleared
- `getComboBox1()` - source currency selector
- `getComboBox2()` - target currency selector

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `CurrencyRateAPI.convert` | CurrencyRateAPI | - | Calls `convert` in `CurrencyRateAPI` |
| R | `UI.getComboBox1` | UI | - | Calls `getComboBox1` in `UI` |
| R | `UI.getComboBox2` | UI | - | Calls `getComboBox2` in `UI` |
| R | `UI.getTextField` | UI | - | Calls `getTextField` in `UI` |
| - | `UI.setLabel` | UI | - | Calls `setLabel` in `UI` |
| - | `UI.setTextField` | UI | - | Calls `setTextField` in `UI` |

This method performs one external service read-like call to fetch a converted currency amount, then updates the presentation state of the screen. The service call is not CRUD against a database; it is an external API retrieval via `CurrencyRateAPI.convert`. The remaining operations are UI reads and writes that support input validation, swap behavior, and clearing the form.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `CurrencyRateAPI.convert` | CurrencyRateAPI | External exchange-rate API | Requests the conversion result for the selected source currency, target currency, and amount |
| R | `UI.getTextField` | UI | UI state | Reads the entered amount for validation and conversion |
| R | `UI.getComboBox1` | UI | UI state | Reads the source currency selection |
| R | `UI.getComboBox2` | UI | UI state | Reads the target currency selection |
| U | `UI.setLabel` | UI | UI state | Updates the result label with the converted amount or clears it |
| U | `UI.setTextField` | UI | UI state | Clears the input amount field |

## 5. Dependency Trace

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

No screen/batch entry points found within 8 hops. Direct callers found: 1 method. Terminal operations from this method: `setLabel` [-], `setTextField` [-], `getComboBox2` [R], `getComboBox2` [R], `getComboBox1` [R], `getComboBox1` [R], `setLabel` [-], `convert` [-], `getComboBox2` [R], `getComboBox1` [R], `getTextField` [R], `getTextField` [R]

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen:UI | `UI.button()` -> `UI` panel assembly (`panelFramePanel2.add(button()[0])`, `button()[1]`, `button()[2]`) | `CurrencyRateAPI.convert [R] External exchange-rate API` |

The method is not invoked from another screen or batch class within the detected search scope; instead, it is self-contained inside the `UI` class and called during UI construction. Its downstream dependency chain ends in an external API call for conversion, plus local UI state updates for display and reset behavior.

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENTIAL] `(button creation and initialization)` (L111-L116)

> Creates the three user actions that drive the screen and applies a consistent button size.

| # | Type | Code |
|---|------|------|
| 1 | SET | `JButton[] button = new JButton[] { new JButton("Convert"), new JButton("Swap"), new JButton("Clear") };` |
| 2 | EXEC | `buttons.setPreferredSize(new Dimension(200, 35));` |
| 3 | SET | `CurrencyRateAPI rate = new CurrencyRateAPI();` |

**Block 2** — [ACTION LISTENER] `(Convert button listener)` (L118-L127)

> Handles the conversion request path, including input validation, remote rate retrieval, formatting, and result display.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `button[0].addActionListener(convert -> { ... });` |
| 2 | IF | `if (!getTextField().getText().isEmpty() && !getTextField().getText().equals("."))` |

**Block 2.1** — [IF] `(amount field is not empty and not '.')` (L119)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `rate.convert(Objects.requireNonNull(getComboBox1().getSelectedItem()).toString(), Objects.requireNonNull(getComboBox2().getSelectedItem()).toString(), BigDecimal.valueOf(Double.parseDouble(textField.getText())))` |
| 2 | SET | `new DecimalFormat("#,###.###").format((Number) Double.parseDouble(...))` |
| 3 | CALL | `setLabel(...)` |

**Block 2.2** — [ELSE] `(invalid amount input)` (L122)

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `javax.swing.JOptionPane.showMessageDialog(null, "Please enter the amount you need", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);` |

**Block 2.3** — [CATCH] `(MalformedURLException | URISyntaxException)` (L124-L126)

| # | Type | Code |
|---|------|------|
| 1 | THROW | `throw new RuntimeException(ex);` |

**Block 3** — [ACTION LISTENER] `(Swap button listener)` (L129-L132)

> Exchanges the selected source and target currencies so the user can reverse the conversion direction quickly.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `button[1].addActionListener(swap -> { ... });` |
| 2 | SET | `String boxItem = Objects.requireNonNull(getComboBox1().getSelectedItem()).toString();` |
| 3 | EXEC | `getComboBox1().setSelectedItem(getComboBox2().getSelectedItem());` |
| 4 | EXEC | `getComboBox2().setSelectedItem(boxItem);` |

**Block 4** — [ACTION LISTENER] `(Clear button listener)` (L134-L137)

> Resets the form so the user can start a new conversion from a clean state.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `button[2].addActionListener(clear -> { ... });` |
| 2 | CALL | `setTextField("");` |
| 3 | CALL | `setLabel("");` |

**Block 5** — [RETURN] `(return button array)` (L139)

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return button;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `button` | Technical term | The set of action buttons rendered on the currency conversion screen. |
| Convert | Business term | The user action that converts the entered amount from the selected source currency into the selected target currency. |
| Swap | Business term | The user action that reverses the source and target currency selections. |
| Clear | Business term | The user action that resets the amount and result fields. |
| `textField` | Field | The amount entry field where the user types the numeric value to convert. |
| `label` | Field | The result display area that shows the converted amount. |
| `getComboBox1()` | Technical term | Returns the source currency selector. |
| `getComboBox2()` | Technical term | Returns the target currency selector. |
| `CurrencyRateAPI` | Technical term | Service wrapper that retrieves currency exchange information from an external API. |
| `convert` | Service operation | Requests a conversion result for a source currency, target currency, and amount. |
| `DecimalFormat` | Technical term | Java formatter used to present the converted amount with thousands separators and up to three decimal places. |
| `BigDecimal` | Technical term | Precise numeric type used to send the amount to the rate conversion service. |
| `MalformedURLException` | Technical term | Exception indicating an invalid URL was built for the external API request. |
| `URISyntaxException` | Technical term | Exception indicating an invalid URI was built for the external API request. |
| `JOptionPane` | Technical term | Swing dialog utility used to show a validation error to the user. |
| `Objects.requireNonNull` | Technical term | Null-safety check ensuring the selected currency exists before conversion or swap. |
| `JButton` | Technical term | Swing button component used for user actions. |
| `Component[]` | Technical term | The array of UI components returned by the method so the caller can place them into the screen layout. |
