Mastering Data Aggregation and Data Streams in C#

Mastering Data Aggregation and Data Streams in C#

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#.

Starter Task Methods

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 identifier saleId and an amount. If a sale with the same saleId already exists, it updates the amount.
  • getSale(int saleId) - Retrieves the sale amount associated with the saleId. If the sale does not exist, it returns null.
  • deleteSale(int saleId) - Deletes the sale record with the given saleId. Returns true if the sale was deleted and false if the sale does not exist.

Are these methods clear so far? Great! Let's now look at how we would implement them.

Starter Task Implementation

Here is the complete code for the starter task:

using System;
using System.Collections.Generic;

public class SalesAggregator
{
    private Dictionary<int, double> sales;

    public SalesAggregator()
    {
        sales = new Dictionary<int, double>();  // Initialize an empty dictionary to store sales records.
    }

    public void addSale(int saleId, double amount)
    {
        sales[saleId] = amount;  // Add or update the sale with the provided saleId and amount.
    }

    public double? getSale(int saleId)
    {
        // Retrieve and return the sale amount for the given saleId, or null if it doesn't exist.
        return sales.ContainsKey(saleId) ? sales[saleId] : (double?)null;
    }

    public bool deleteSale(int saleId)
    {
        return sales.Remove(saleId);  // Remove the sale record and return true if it existed, false otherwise.
    }
}

public class Program
{
    public static void Main()
    {
        SalesAggregator aggregator = new SalesAggregator();

        // Add sales
        aggregator.addSale(1, 100.50);
        aggregator.addSale(2, 200.75);

        // Get sale
        Console.WriteLine(aggregator.getSale(1));  // Output: 100.5

        // Delete sale
        Console.WriteLine(aggregator.deleteSale(2));  // Output: True
        Console.WriteLine(aggregator.getSale(2));  // Output: null
    }
}

Explanation:

  • The SalesAggregator class initializes an empty dictionary 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 returns null if the sale does not exist.
  • The deleteSale method removes the sale record for the given sale ID or returns false if the sale does not exist.

Now that we have our basic aggregator, let's extend it to include more advanced functionalities.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal