Mastering Data Aggregation and Streams with Data Formatting in Kotlin

Introduction

In modern software development, the ability to handle, aggregate, and format data efficiently is a crucial skill. Whether you're building APIs, data analytics tools, or any data-driven application, understanding how to manipulate data streams effectively can significantly impact your application's performance and maintainability. In this lesson, we'll explore advanced data aggregation techniques in Kotlin, focusing on data formatting and stream operations. We'll start with a basic sales record aggregator and gradually enhance it with more sophisticated features like date-based filtering, statistical aggregation, and multiple output formats including JSON and CSV. Let's dive into the implementation details and explore these concepts hands-on.

Starter Task Methods and Their Definitions

To begin, we'll implement a basic sales record aggregator. Here are the methods we'll be focusing on:

  • addSale(saleId: String, amount: Double): Unit - 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(saleId: String): Double? - Retrieves the sale amount associated with the saleId. If the sale does not exist, it returns null.
  • deleteSale(saleId: String): Boolean - Deletes the sale record with the given saleId. Returns true if the sale was deleted and false if the sale does not exist.

Let's now look at how we would implement them.

Starter Task Solution

Here is the complete code for the starter task:

class SalesAggregator {
    private val sales = mutableMapOf<String, Double>()

    fun addSale(saleId: String, amount: Double) {
        sales[saleId] = amount
    }

    fun getSale(saleId: String): Double? {
        return sales[saleId]
    }

    fun deleteSale(saleId: String): Boolean {
        return if (sales.containsKey(saleId)) {
            sales.remove(saleId)
            true
        } else {
            false
        }
    }
}

// Example Usage
fun main() {
    val aggregator = SalesAggregator()

    // Add sales
    aggregator.addSale("001", 100.50)
    aggregator.addSale("002", 200.75)

    // Get sale
    println(aggregator.getSale("001"))  // Output: 100.5

    // Delete sale
    println(aggregator.deleteSale("002"))  // Output: true
    println(aggregator.getSale("002"))  // Output: null
}

Explanation:

  • The sales property is initialized as a mutable map 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