Currency exchange is a REST API application for managing exchange rates and performing currency conversions. This project is based on the technical specification provided in the Java Backend Roadmap by Sergey Zhukov (link here).
- add currencies and exchange rates
- update exchange rates
- convert amounts of money between different currencies based on the latest exchange rates
- Database:
The project uses SQLite as the database, which allows embedding a file with populated tables into the project resources for the application's functionality.
Database diagram:
- REST API endpoints:
GET /currencies
- returns list of all currencies
Example of response:
[
{
"id": 0,
"name": "United States dollar",
"code": "USD",
"sign": "$"
},
{
"id": 0,
"name": "Euro",
"code": "EUR",
"sign": "€"
}
]
GET /currency/EUR
- returns a specific currency.
The currency code is specified in the query address
Example of response:
{
"id": 0,
"name": "Euro",
"code": "EUR",
"sign": "€"
}
POST /currencies
- adding a new currency to the database.The data is sent in the request body as form fields (x-www-form-urlencoded). The form fields are name, code, and sign.
Example of response: a JSON representation of the inserted record in the database, including its ID:
{
"id": 0,
"name": "Euro",
"code": "EUR",
"sign": "€"
}
GET /exchangeRates
- returns list of all exchange rates
Example of response:
[
{
"id": 0,
"baseCurrency": {
"id": 0,
"name": "United States dollar",
"code": "USD",
"sign": "$"
},
"targetCurrency": {
"id": 1,
"name": "Euro",
"code": "EUR",
"sign": "€"
},
"rate": 0.99
}
]
GET /exchangeRate/USDRUB
- returns a specific exchange rate.The currency pair is specified by consecutive currency codes in the request URL Example of response:
{
"id": 0,
"baseCurrency": {
"id": 0,
"name": "United States dollar",
"code": "USD",
"sign": "$"
},
"targetCurrency": {
"id": 1,
"name": "Euro",
"code": "EUR",
"sign": "€"
},
"rate": 0.99
}
POST /exchangeRates
- adding a new exchange rate to the database.The data is sent in the request body as form fields (x-www-form-urlencoded). The form fields are baseCurrencyCode, targetCurrencyCode, and rate.
Example form fields:
- baseCurrencyCode - USD
- targetCurrencyCode - EUR
- rate - 0.99
Example of response: a JSON representation of the inserted record in the database, including its ID:
{
"id": 0,
"baseCurrency": {
"id": 0,
"name": "United States dollar",
"code": "USD",
"sign": "$"
},
"targetCurrency": {
"id": 1,
"name": "Euro",
"code": "EUR",
"sign": "€"
},
"rate": 0.99
}
PATCH /exchangeRate/USDRUB
- updating an existing exchange rate in the database.The currency pair is specified by consecutive currency codes in the request URL. The data is sent in the request body as form fields (x-www-form-urlencoded).
The only form field is rate.
Example of response: a JSON representation of the inserted record in the database, including its ID:
{
"id": 0,
"baseCurrency": {
"id": 0,
"name": "United States dollar",
"code": "USD",
"sign": "$"
},
"targetCurrency": {
"id": 1,
"name": "Euro",
"code": "EUR",
"sign": "€"
},
"rate": 0.99
}
GET /exchange?from=BASE_CURRENCY_CODE&to=TARGET_CURRENCY_CODE&amount=$AMOUNT
- calculating the conversion of a specific amount of funds from one currency to another.
Example of request:
GET /exchange?from=USD&to=AUD&amount=10
Example of response:
{
"baseCurrency": {
"id": 0,
"name": "United States dollar",
"code": "USD",
"sign": "$"
},
"targetCurrency": {
"id": 1,
"name": "Australian dollar",
"code": "AUD",
"sign": "A€"
},
"rate": 1.45,
"amount": 10.00,
"convertedAmount": 14.50
}
Obtaining the exchange rate for conversion can occur through one of three scenarios. Let's assume we're making a transfer from currency A to currency B:
- There is a currency pair AB in the ExchangeRates table – we take its rate
- There is a currency pair BA in the ExchangeRates table – we take its rate and calculate the inverse to obtain AB
- There are currency pairs USD-A and USD-B in the ExchangeRates table – we calculate the rate for AB from these rates
To run the application, make sure that you have Java (JDK), Apache Maven and Apache Tomcat installed on your computer.
- Go to the configurations menu
- Select "Edit configurations"
- Click the plus sign
- Select Tomcat -> Local
- Select Tomcat as a Application server
- Click "Fix"
- Select CurrencyExchange:war exploded
- Remove "CurrencyExchange_war_exploded" from the application context
- Click "Run"
Apache Maven will build the project and run it using Tomcat.