# (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 constructs and configures the three action buttons used by the currency conversion screen: Convert, Swap, and Clear. It is responsible for preparing the visual controls, registering the event handlers, and binding each button to its user-facing business action. The method follows a presentation-layer routing pattern: each button delegates to a different interaction path rather than performing all processing inline.

From a business perspective, the method supports three service categories. Convert submits a currency conversion request using the selected source and target currencies plus the entered amount; Swap exchanges the two selected currency codes to make reverse conversion quicker; Clear resets the input and output fields so the user can start a new conversion flow. The method is a shared UI factory for this screen and acts as the interaction hub between the form state and the external currency rate service.

The method also includes input validation and error handling for the conversion flow. If the amount field is empty or contains only a decimal point, it blocks the conversion and shows a user error dialog instead of calling the external API. If the API call fails due to malformed URL or URI syntax, the listener rethrows the exception as an unchecked runtime failure.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["button()"])
    CREATE_BUTTONS["Create JButton array: Convert, Swap, Clear"]
    LOOP_BUTTONS{"For each button in array"}
    SET_SIZE["Set preferred size to 200x35"]
    CREATE_RATE["Instantiate CurrencyRateAPI"]
    ADD_CONVERT["Attach Convert action listener"]
    ADD_SWAP["Attach Swap action listener"]
    ADD_CLEAR["Attach Clear action listener"]
    RETURN_NODE(["Return button array"])
    CONVERT_START{"Click Convert"}
    CHECK_INPUT{"Text field is not empty AND not '.'"}
    SHOW_ERROR["Show input error dialog"]
    CALL_RATE["Call CurrencyRateAPI.convert(from,to,amount)"]
    FORMAT_RESULT["Format numeric result with DecimalFormat #,###.###"]
    SET_LABEL["Update result label"]
    HANDLE_EXCEPTION["Catch MalformedURLException or URISyntaxException and rethrow as RuntimeException"]
    SWAP_START{"Click Swap"}
    SAVE_LEFT["Read selected item from comboBox1 into boxItem"]
    SET_LEFT["Set comboBox1 selection to comboBox2 selection"]
    SET_RIGHT["Set comboBox2 selection to saved boxItem"]
    CLEAR_START{"Click Clear"}
    CLEAR_TEXT["Clear text field"]
    CLEAR_LABEL["Clear label"]

    START --> CREATE_BUTTONS
    CREATE_BUTTONS --> LOOP_BUTTONS
    LOOP_BUTTONS --> SET_SIZE
    SET_SIZE --> LOOP_BUTTONS
    LOOP_BUTTONS --> CREATE_RATE
    CREATE_RATE --> ADD_CONVERT
    ADD_CONVERT --> ADD_SWAP
    ADD_SWAP --> ADD_CLEAR
    ADD_CLEAR --> RETURN_NODE
    ADD_CLEAR --> CONVERT_START
    CONVERT_START --> CHECK_INPUT
    CHECK_INPUT -->|true| CALL_RATE
    CHECK_INPUT -->|false| SHOW_ERROR
    CALL_RATE --> FORMAT_RESULT
    FORMAT_RESULT --> SET_LABEL
    SET_LABEL --> HANDLE_EXCEPTION
    HANDLE_EXCEPTION --> RETURN_NODE
    SHOW_ERROR --> RETURN_NODE
    RETURN_NODE --> SWAP_START
    SWAP_START --> SAVE_LEFT
    SAVE_LEFT --> SET_LEFT
    SET_LEFT --> SET_RIGHT
    SET_RIGHT --> RETURN_NODE
    RETURN_NODE --> CLEAR_START
    CLEAR_START --> CLEAR_TEXT
    CLEAR_TEXT --> CLEAR_LABEL
    CLEAR_LABEL --> RETURN_NODE
```

## 3. Parameter Analysis

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

External state read by this method:
- `textField` instance field: source amount entered by the user.
- `comboBox1` instance field: source currency selection.
- `comboBox2` instance field: target currency selection.
- `label` instance field: destination for converted amount or cleared output.
- `CurrencyRateAPI rate`: external API wrapper used for exchange-rate conversion.

## 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` |

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `UI.getTextField` | UI | - | Reads the amount field to validate user input and parse the entered value |
| R | `UI.getComboBox1` | UI | - | Reads the source currency selection for conversion or swap |
| R | `UI.getComboBox2` | UI | - | Reads the target currency selection for conversion or swap |
| R | `CurrencyRateAPI.convert` | CurrencyRateAPI | External exchange-rate API | Requests a converted currency amount using source currency, target currency, and amount |
| U | `UI.setLabel` | UI | - | Updates the result label with the formatted converted amount |
| U | `UI.setTextField` | UI | - | Clears the input field when the Clear button is used |
| U | `JComboBox.setSelectedItem` | Swing component | - | Swaps the displayed source and target currency selections |

Notes:
- `CurrencyRateAPI.convert` is the only external business call in this method.
- The UI helper methods are reads or updates against local screen state rather than database CRUD.
- No database tables or persistence entities are accessed in this method.

## 5. Dependency Trace

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

No screen/batch entry points found within 8 hops. Direct callers found: 1 methods.
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: `com.github.blaxk3.ui.UI` | `panel()` -> `button()` -> action listener callbacks | `CurrencyRateAPI.convert [R] External exchange-rate API`, `UI.getComboBox1 [R]`, `UI.getComboBox2 [R]`, `UI.getTextField [R]`, `UI.setLabel [U]`, `UI.setTextField [U]` |

## 6. Per-Branch Detail Blocks

**Block 1** — [FOR] `(for each JButton in button array)` (L119)

> Sets the button display size consistently for the full action bar.

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

**Block 2** — [CALL] `(create CurrencyRateAPI and attach Convert handler)` (L124)

> Prepares the external rate service and binds the conversion workflow to the first button.

| # | Type | Code |
|---|------|------|
| 1 | SET | `CurrencyRateAPI rate = new CurrencyRateAPI();` |
| 2 | EXEC | `button[0].addActionListener(convert -> { ... });` |

**Block 2.1** — [IF] `(amount is not empty AND amount is not ".")` (L126)

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `getTextField().getText()` |
| 2 | EXEC | `getTextField().getText().isEmpty()` |
| 3 | EXEC | `getTextField().getText().equals(".")` |

**Block 2.1.1** — [THEN] `(valid amount entered)` (L127)

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

**Block 2.1.2** — [ELSE] `(invalid amount entered)` (L128)

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

**Block 2.2** — [CATCH] `(MalformedURLException or URISyntaxException)` (L130)

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

**Block 3** — [CALL] `(attach Swap handler)` (L134)

> Exchanges the source and target currency selections to speed up reverse conversion.

| # | 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** — [CALL] `(attach Clear handler)` (L139)

> Resets the screen so a user can start a new conversion request.

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

**Block 5** — [RETURN] `(return buttons)` (L143)

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

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|-------------------|
| `button` | UI component group | The screen action bar containing Convert, Swap, and Clear controls |
| `textField` | Field | Amount input field where the user enters the source currency value |
| `comboBox1` | Field | Source currency selector |
| `comboBox2` | Field | Target currency selector |
| `label` | Field | Result display area showing the converted amount |
| `CurrencyRateAPI` | Service | External service wrapper used to retrieve or compute exchange-rate conversion |
| `convert` | Action | Currency conversion operation from one currency to another |
| `Swap` | UI action | Switches the selected source and target currencies |
| `Clear` | UI action | Clears entered amount and displayed output |
| `Convert` | UI action | Requests a converted value and displays it on the screen |
| `DecimalFormat` | Technical term | Number formatter used to present the converted amount with grouping and precision |
| `MalformedURLException` | Technical exception | Indicates the constructed request URL is invalid |
| `URISyntaxException` | Technical exception | Indicates the request URI syntax is invalid |
| `JButton` | Swing component | Clickable button control in the Java desktop UI |
| `JComboBox` | Swing component | Drop-down selector for choosing currencies |
| `JTextField` | Swing component | Single-line text input for entering the amount |
| `JOptionPane` | Swing component | Modal dialog used to show validation errors to the user |
