Welcome to our lesson on mastering data aggregation and data streams in C#. 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 C#.
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 identifiersaleIdand anamount. If a sale with the samesaleIdalready 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. Returnstrueif the sale was deleted andfalseif 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
SalesAggregatorclass initializes an empty dictionary to store sales records. - The
addSalemethod adds a new sale or updates the amount for an existing sale ID. - The
getSalemethod retrieves the amount for a given sale ID or returnsnullif the sale does not exist. - The
deleteSalemethod removes the sale record for the given sale ID or returnsfalseif 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, DateTime date)- Adds or updates a sale record with a unique identifiersaleId,amount, and adate.aggregateSales(double minAmount = 0)- 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 = 0)- Returns the sales data, filtered byminAmount, formatted as JSON. Includes aggregated sales statistics in the output.getSalesInDateRange(DateTime startDate, DateTime 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 System.Text.Json.
This function formats the 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 C#. 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!
