A Java console application that allows currency conversion between several currencies using real-time exchange rates. The project follows clean architecture principles with a modular and extensible design.
- Convert between:
- USD ↔ MXN
- EUR ↔ MXN
- CAD ↔ MXN
- Interactive console menu
- User input validation
- Real-time currency exchange rate retrieval via API
- Modular architecture: separation of model, business logic, and UI
- Clean and maintainable codebase
dev.ghector6.currencyconverter/
├── app/
│ └── Main.java # Entry point of the application
│
├── model/
│ ├── Currency.java # Currency rate data model
│ └── ConversionOption.java # Enum for conversion options
│
├── service/
│ ├── CurrencyConversion.java # Business logic for conversions
│ └── CurrencyQuery.java # Retrieves exchange rates from API
│
└── ui/
└── ConsoleUI.java # Console-based user interface
- Java 17 or later
- An IDE (IntelliJ IDEA, Eclipse, VS Code, etc.)
- (Optional) Gradle or Maven for build automation
- Clone the repository:
git clone https://github.com/yourusername/currency-converter-java.git
cd currency-converter-java-
Open the project in your preferred IDE and compile it.
-
Run the
Main.javaclass located indev.ghector6.currencyconverter.app.
*******************************
Welcome to Currency Converter
1) USD => MXN
2) MXN => USD
3) EUR => MXN
4) MXN => EUR
5) CAD => MXN
6) MXN => CAD
7) Exit
*******************************
Select an option: 1
Enter amount to convert: 100
Result: 1700.00 MXN
This application retrieves live currency exchange rates from ExchangeRate API using Java's HttpClient.
-
Endpoint used:
https://v6.exchangerate-api.com/v6/YOUR_API_KEY/latest/USD -
How it works:
- A GET request is made using
HttpClient. - The JSON response is parsed into a
Currencyobject using the Gson library with theLOWER_CASE_WITH_UNDERSCORESnaming policy. - This ensures accurate mapping between the API's JSON and your Java model.
- A GET request is made using
-
Error Handling:
- If the API call fails (due to connection issues or an invalid key), the app throws a
RuntimeException. In the future, this could be improved with user-friendly error messages or fallback data.
- If the API call fails (due to connection issues or an invalid key), the app throws a
-
Important:
- Replace the demo API key with your own. You can get one for free by signing up at exchangerate-api.com.
URI uri = URI.create("https://v6.exchangerate-api.com/v6/YOUR_API_KEY/latest/USD");- Single Responsibility Principle (SRP)
- Safe and clean user input handling
- Object-Oriented Programming (OOP)
- Enum-based conversion modeling
- Real-world API integration
- Robust API error handling and retries
- Fallback to cached or default rates on failure
- Support for more currencies and base rates
- GUI version using JavaFX or Swing
- Automated tests
- Ghector6
GitHub