Welcome to our lesson on mastering data aggregation and data streams in Java. In this lesson, we'll start by building a basic sales records aggregator. Then, we'll extend its functionality to handle more complex operations such as filtering, data aggregation, and formatted outputs. By the end of this session, you'll be able to manage and format data streams efficiently using Java.
To begin, we'll implement a basic sales record aggregator. Here are the methods we'll be focusing on:
addSale(int saleId, double amount)
- Adds a sale record with a unique identifiersaleId
and anamount
. If a sale with the samesaleId
already exists, it updates the amount.getSale(int saleId)
- Retrieves the sale amount associated with thesaleId
. If the sale does not exist, it returnsnull
.deleteSale(int saleId)
- Deletes the sale record with the givensaleId
. Returnstrue
if the sale was deleted andfalse
if the sale does not exist.
Are these methods clear so far? Great! Let's now look at how we would implement them.
Here is the complete code for the starter task:
Explanation:
- The
SalesAggregator
class initializes an emptyHashMap
to store sales records. - The
addSale
method adds a new sale or updates the amount for an existing sale ID. - The
getSale
method retrieves the amount for a given sale ID or returnsnull
if the sale does not exist. - The
deleteSale
method removes the sale record for the given sale ID or returnsfalse
if the sale does not exist.
Now that we have our basic aggregator, let's extend it to include more advanced functionalities.
To increase the complexity and usefulness of our sales aggregator, we'll adjust some existing methods and introduce some new methods and functionalities involving formatted outputs.
addSale(int saleId, double amount, LocalDateTime date)
- Adds or updates a sale record with a unique identifiersaleId
,amount
, and adate
.aggregateSales(double minAmount)
- Returns an object with the total number of sales and the total amount of sales where the sale amount is aboveminAmount
. The object format looks like this:formatSales(double minAmount)
- Returns the sales data, filtered byminAmount
, formatted as JSON. Includes aggregated sales statistics in the output.getSalesInDateRange(LocalDateTime startDate, LocalDateTime endDate)
- Retrieves all sales that occurred within the given date range, inclusive. Each sale includessaleId
,amount
, anddate
.
Let's implement these methods step-by-step.
We'll first modify the addSale
method to accept a date.
This ensures that each sale record includes a date in addition to the amount.
Now, we create the aggregateSales
method:
This method iterates through the sales and sums up those that exceed the minAmount
.
Next, we'll create the formatSales
method to output data in JSON format using Gson.
This function utilizes Gson, a Java library from Google that converts Java objects to JSON representations, to format sales data as JSON, and includes aggregated statistics.
Finally, let's implement the getSalesInDateRange
method:
This method retrieves all sales within the specified date range.
Congratulations! You've now extended a basic sales aggregator to an advanced one capable of filtering, aggregating, and providing formatted outputs in Java. These skills are crucial for handling data streams efficiently, especially when dealing with large datasets. Feel free to experiment with similar challenges to reinforce your understanding. Well done, and see you in the next lesson!
