Conteúdo do Curso
Multithreading in Java
Multithreading in Java
Challenge CompletableFuture
Task
Your task is to build a system that handles orders and carries out additional calculations like tax and shipping costs. You need to process multiple orders asynchronously, perform the necessary calculations, and display the total amount for each order once all calculations are finished.
Each order has a unique identifier and a price associated with it. (InitMap class)
The ID and their sums are stored in a Map
within the InitMap
class. The keys of this Map
(which are the order ID) are the ones we loop through in the processOrders()
method.
Fetching order data is done asynchronously and returns the order amount. This is handled by the OrderService
class, specifically the fetchOrderAmount(String orderId)
method.
Tax is calculated as 15% of the order amount. This is managed by the CalculationService
class, using the calculateTax()
method.
Shipping cost is calculated as 10% of the order amount. This is also handled by the CalculationService
class, using the calculateShipping()
method.
Note
Your main task is to implement the logic step-by-step in the package
task
classOrderProcessingExample
in theprocessOrders()
method.
Implementation Steps
1. Receive the order amount asynchronously using the order service:
- Create a
CompletableFuture
object to fetch the order amount asynchronously; - Call the
fetchOrderAmount()
method fromOrderService
passing the order id.Note
The
fetchOrderAmount()
method usesCompletableFuture.supplyAsync()
to perform the task asynchronously and returns the order amount, by its id.
2. After fetching the order amount, asynchronously calculate the tax using the calculation service:
- Use
thenCompose()
to execute the task sequentially after getting the order amount. Call thecalculateTax()
method fromCalculationService
passing the order amount.Note
The
calculateTax()
method usesCompletableFuture.supplyAsync()
to execute the task asynchronously and returns a tax equal to 15% of the order amount.
3. After receiving the order amount, asynchronously calculate the shipping cost using the calculate service:
- Use
thenCompose()
to execute the task sequentially after receiving the order amount. Call thecalculateShipping
method fromCalculationService
passing the order amount.Note
The
calculateShipping()
method usesCompletableFuture.supplyAsync()
to execute the task asynchronously and returns a shipping cost equal to 10% of the order amount.
4. Combine asynchronous tax and shipping cost calculations to get the total incremental cost:
- Use
thenCombine()
to combine twoCompletableFuture
results of tax and shipping to get the total additional cost (tax + shipping cost).
5. Combine the order amount and the total additional cost to get the total order amount:
- Use
thenCombine()
to combine the twoCompletableFuture
results of additional cost and total additional cost and total order amount to get the total order amount.
6. After calculating the total, output it to the console:
- Use
thenAccept()
to process and output the result; - After completing all calculations, output the order total to the console.
To output the result to the console, you can use this pattern:
If you follow everything correctly, you will get this output to the console:
Once you are sure that everything is working, run the verification tests on the path /src/test/java/OrderProcessingExampleTest.java
.
Obrigado pelo seu feedback!