Klasse MainController
java.lang.Object
com.currencycalc.currencycalculatorjavafx.MainController
The controller class for the Currency Calculator JavaFX application.
This class manages user interactions, handles input validation, and processes
currency conversions using data fetched from the FreeCurrencyAPI.
- Version:
- 1.0.0
- Autor:
- Tim Platzer
-
Feldübersicht
FelderModifizierer und TypFeldBeschreibungprivate javafx.scene.control.TextFieldThe input field where the user enters the amount to be converted.private javafx.scene.control.ButtonThe button that triggers the currency conversion process.private javafx.scene.control.TextFieldThe output field where the converted amount is displayed.private final CurrencyClientTheCurrencyClientinstance responsible for communicating with the FreeCurrencyAPI.A map of currency codes to their corresponding full names.private javafx.scene.control.ChoiceBox<String> The dropdown menu for selecting the source currency.private javafx.scene.control.ChoiceBox<String> The dropdown menu for selecting the target currency. -
Konstruktorübersicht
Konstruktoren -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungvoidconvert()Handles the conversion of the entered amount from the source currency to the target currency.javafx.scene.control.TextFieldRetrieves the input field for the amount to be converted.javafx.scene.control.ButtonRetrieves the convert button.javafx.scene.control.TextFieldRetrieves the output field for the converted amount.Retrieves theCurrencyClientfor API communication.private StringgetCurrencyCode(String currencyName) Retrieves the currency code (e.g., "USD") for a given currency name.Retrieves the map of currency codes and names.private DoublegetCurrencyRate(Data data, String currency) Retrieves the exchange rate for a specific currency using reflection.javafx.scene.control.ChoiceBox<String> Retrieves the source currency dropdown.javafx.scene.control.ChoiceBox<String> Retrieves the target currency dropdown.voidInitializes the controller.
-
Felddetails
-
convertButton
private javafx.scene.control.Button convertButtonThe button that triggers the currency conversion process. When clicked, it validates the input, fetches exchange rates, and displays the converted amount. -
targetCurrencyDropdown
The dropdown menu for selecting the target currency. This allows users to choose the currency into which they want to convert the source amount. -
sourceCurrencyDropdown
The dropdown menu for selecting the source currency. This allows users to choose the currency from which they want to convert the amount. -
amountInputField
private javafx.scene.control.TextField amountInputFieldThe input field where the user enters the amount to be converted. The value is validated to ensure it is a numeric input and supports both dots and commas as decimal separators. -
convertedAmountField
private javafx.scene.control.TextField convertedAmountFieldThe output field where the converted amount is displayed. After the conversion process is completed, the result is formatted to two decimal places and shown here. -
currencyClient
TheCurrencyClientinstance responsible for communicating with the FreeCurrencyAPI. This client is used to fetch real-time exchange rate data. -
currencyMap
A map of currency codes to their corresponding full names. This map is used to populate dropdown menus with user-friendly currency names while maintaining the ability to work with currency codes internally.Example:
"AUD": "Australian Dollar""EUR": "Euro""USD": "US Dollar"
The map is implemented as a
LinkedHashMapto preserve the order of insertion.
-
-
Konstruktordetails
-
MainController
public MainController()
-
-
Methodendetails
-
initialize
public void initialize()Initializes the controller. Populates the dropdown menus with currency names, sets up input validation, and configures button states. -
convert
Handles the conversion of the entered amount from the source currency to the target currency.- Löst aus:
IOException- If an error occurs during the API request.InterruptedException- If the request is interrupted.
-
getCurrencyRate
Retrieves the exchange rate for a specific currency using reflection.- Parameter:
data- TheDataobject containing all exchange rates.currency- The currency code (e.g., "USD").- Gibt zurück:
- The exchange rate for the given currency, or
nullif not found.
-
getCurrencyCode
Retrieves the currency code (e.g., "USD") for a given currency name.- Parameter:
currencyName- The full name of the currency (e.g., "US Dollar").- Gibt zurück:
- The currency code, or
nullif not found.
-
getTargetCurrencyDropdown
Retrieves the target currency dropdown.- Gibt zurück:
- The
ChoiceBoxfor selecting the target currency.
-
getSourceCurrencyDropdown
Retrieves the source currency dropdown.- Gibt zurück:
- The
ChoiceBoxfor selecting the source currency.
-
getAmountInputField
public javafx.scene.control.TextField getAmountInputField()Retrieves the input field for the amount to be converted.- Gibt zurück:
- The
TextFieldfor entering the amount.
-
getConvertedAmountField
public javafx.scene.control.TextField getConvertedAmountField()Retrieves the output field for the converted amount.- Gibt zurück:
- The
TextFielddisplaying the converted amount.
-
getCurrencyClient
Retrieves theCurrencyClientfor API communication.- Gibt zurück:
- The
CurrencyClientinstance.
-
getConvertButton
public javafx.scene.control.Button getConvertButton()Retrieves the convert button.- Gibt zurück:
- The
Buttonused to trigger the conversion.
-
getCurrencyMap
Retrieves the map of currency codes and names.- Gibt zurück:
- A
Mapcontaining currency codes as keys and names as values.
-