A Spring Boot microservice to calculate the final bill after applying discounts and converting currency based on real-time exchange rates.
- Calculates percentage and flat discounts based on user type and purchase.
- Converts currency using a third-party API (
https://v6.exchangerate-api.com/v6/). - Basic authentication via Spring Security.
- Caching with
@Cacheablefor exchange rate calls. - Unit tests with mocking for all components.
- Maven build with code quality tools and test coverage.
- SonarQube integration-ready.
- Java 21
- Spring Boot
- Spring Security
- Spring Web
- Spring Test
- Maven
- Jacoco (code coverage)
- SonarQube (code quality)
- ExchangeRate-API
# Clone the repo
git clone https://github.com/danyalafzaal786/bill-service.git
cd bill-service
# Build the project
mvn clean install
# Run the application
mvn spring-boot:runThe service exposes the following API endpoint to calculate the final payable amount based on the provided bill details:
| Method | Path | Description |
|---|---|---|
| POST | /api/calculate | Returns discounted & converted bill |
The request should be sent as a JSON payload with the following structure:
{
"items": [
{
"name": "item1",
"price": 450,
"category": "GROCERY"
},
{
"name": "item2",
"price": 1050,
"category": "NON_GROCERY"
}
],
"userType": "EMPLOYEE",
"customerTenure": 1,
"originalCurrency": "USD",
"targetCurrency": "PKR"
}{
"totalBill": 1500,
"totalDiscount": 390.0,
"conversionRate": 280.8215,
"totalPayableBillInTargetCurrency": 311711.86500,
"targetCurrency": "PKR"
}To run the unit tests, execute the following command:
mvn testTo generate test coverage reports using Jacoco, use the following:
mvn clean verifyor
mvn jacoco:reportOnce the tests are completed, a report will be generated in target/site/jacoco/. Open the index.html file to view the code coverage summary.
open target/site/jacoco/index.html # On macOS or Linux
start target/site/jacoco/index.html # On WindowsTo run static code analysis and check for code style issues, use the following command:
# Run static analysis (Checkstyle)
mvn checkstyle:check(Optional) To run a SonarQube scan for code quality, run:
# Run SonarQube scan (make sure SonarQube is running locally)
mvn sonar:sonar \
-Dsonar.projectKey=billing-service \
-Dsonar.host.url=http://localhost:9000 \
-Dsonar.token=sonarqube-token- Currency conversion via ExchangeRate-API
- Add your API key to application.properties:
currency.exchange.api.key=your-api-key
This section outlines some design decisions and areas that can be extended in future iterations:
The current implementation handles basic request flow. Advanced and centralized exception handling (e.g., using @ControllerAdvice or ErrorResponse models) can be added to gracefully manage invalid inputs and system errors.
The project can be extended to integrate OpenAPI/Swagger using Springdoc OpenAPI for auto-generated and interactive API documentation.
Additional validation annotations (e.g., @NotNull, @Min, @Valid) can be added to DTOs to enforce data integrity.
The current basic authentication can be enhanced with role-based access controls for securing different API operations.
Currently, the application operates entirely in-memory. A future extension could involve storing user and transaction data in a database (e.g., PostgreSQL, MySQL).
External configuration (e.g., exchange API keys) can be managed using Spring Profiles or a configuration server for production-readiness.