Managing Product Reviews and Data Aggregation in Kotlin

Introduction

Welcome to today’s lesson! We’ll explore the fascinating world of using Kotlin for managing product reviews and putting data aggregation into practice. We’ll begin with a straightforward Starter Task to establish our foundation, then gradually progress to a more advanced solution involving data aggregation. Let’s get started!

Starter Task: Methods and Their Definitions

For our starter task, we will lay the foundation by implementing basic operations for managing product reviews. These are the methods we will need to implement:

  • addReview(productId: String, reviewId: String, reviewText: String, rating: Int): Boolean — adds a review to the product specified by productId. If a review with reviewId already exists, it updates the existing review. Returns true if the review was added or updated successfully, false otherwise.

  • getReview(productId: String, reviewId: String): Map<String, Any>? — returns the review details (reviewText, rating, and flagged fields) for the review specified by reviewId under the given productId. If the review or product does not exist, returns null.

  • deleteReview(productId: String, reviewId: String): Boolean — deletes the review specified by reviewId under the given productId. Returns true if the review was deleted, false otherwise.

Starter Task Implementation

Let's look at the code that implements these functionalities:

class ReviewManager {
    private val products = HashMap<String, MutableMap<String, Map<String, Any>>>()

    fun addReview(productId: String, reviewId: String, reviewText: String, rating: Int): Boolean {
        if (rating !in 1..5) return false  // Invalid rating
        val productReviews = products.getOrPut(productId) { mutableMapOf() }
        productReviews[reviewId] = mapOf("text" to reviewText, "rating" to rating, "flagged" to false)
        return true
    }

    fun getReview(productId: String, reviewId: String): Map<String, Any>? {
        return products[productId]?.get(reviewId)
    }

    fun deleteReview(productId: String, reviewId: String): Boolean {
        val productReviews = products[productId]
        if (productReviews != null && productReviews.containsKey(reviewId)) {
            productReviews.remove(reviewId)
            if (productReviews.isEmpty()) {
                products.remove(productId)  // Remove product if no reviews left
            }
            return true
        }
        return false
    }
}


fun main() {
  // Instantiate the ReviewManager
  val reviewManager = ReviewManager()

  // Adding some reviews
  reviewManager.addReview("p1", "r1", "Great product!", 5)
  reviewManager.addReview("p1", "r2", "Not bad", 3)

  // Testing getReview method
  println(reviewManager.getReview("p1", "r1"))  // Expected: {text=Great product!, rating=5, flagged=false}
  println(reviewManager.getReview("p1", "r3"))  // Expected: null

  // Testing deleteReview method
  println(reviewManager.deleteReview("p1", "r2"))  // Expected: true
  println(reviewManager.getReview("p1", "r2"))  // Expected: null  
}

This code establishes the foundational methods needed for managing product reviews within a ReviewManager class. The addReview method allows for adding a new review or updating an existing one, ensuring each review contains valid rating values between 1 and 5. It utilizes the getOrPut method to retrieve or initialize a map of reviews for a given product, ensuring smooth addition or updates without requiring separate checks for initialization. The getReview method retrieves the review details for a specific product, including the review text and rating, returning null if the product or review doesn't exist. The deleteReview method facilitates the removal of a specific review, and if no reviews are left for a product, the product itself is removed from the product list. Together, these methods form the basic operations required to manage a collection of product reviews efficiently.

Now, let's extend this with new features.

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