Managing Product Reviews and Data Aggregation in C++

Introduction

Hello, and welcome to today's lesson! Today, we are going to dive into the world of managing product reviews and applying data aggregation in practice. We will start with a relatively simple Starter Task to set up our base, and then gradually build up to a more complex solution involving data aggregation. Let's jump in!

Starter Task: Methods and Their Definitions

For our starter task, we will lay the foundation by implementing basic operations for managing product reviews. The Review struct encapsulates the details of a product review, consisting of the following fields:

  • std::string text — The textual content of the review.
  • int rating — The rating score of the review, ranging from 1 to 5.
  • bool flagged — A boolean indicating whether the review is flagged as inappropriate.

These are the methods we will need to implement in a ReviewManager class:

  • bool add_review(std::string product_id, std::string review_id, std::string review_text, int rating) — adds a review to the product specified by product_id. If a review with review_id already exists, it updates the existing review. Returns true if the review was added or updated successfully, false otherwise. For newly added items, the flagged attribute is set to false initially.

  • std::optional<Review> get_review(std::string product_id, std::string review_id) — returns the review details (text, rating, and flagged fields) for the review specified by review_id under the given product_id. If the review or product does not exist, returns std::nullopt.

  • bool delete_review(std::string product_id, std::string review_id) — deletes the review specified by review_id under the given product_id. Returns true if the review was deleted, false otherwise.

Starter Task Implementation

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

#include <iostream>
#include <string>
#include <unordered_map>
#include <map>
#include <optional>

struct Review {
    std::string text;
    int rating;
    bool flagged;
};

class ReviewManager {
private:
    std::unordered_map<std::string, std::unordered_map<std::string, Review>> products;

public:
    bool add_review(const std::string& product_id, const std::string& review_id, const std::string& review_text, int rating) {
        if (rating < 1 || rating > 5) {
            return false; // Invalid rating
        }
        products[product_id][review_id] = {review_text, rating, false};
        return true;
    }

    std::optional<Review> get_review(const std::string& product_id, const std::string& review_id) {
        if (products.count(product_id) && products[product_id].count(review_id)) {
            return products[product_id][review_id];
        }
        return std::nullopt;
    }

    bool delete_review(const std::string& product_id, const std::string& review_id) {
        if (products.count(product_id) && products[product_id].count(review_id)) {
            products[product_id].erase(review_id);
            if (products[product_id].empty()) {
                products.erase(product_id); // Remove product if no reviews left
            }
            return true;
        }
        return false;
    }
};

int main() {
    ReviewManager review_manager;

    // Adding some reviews
    review_manager.add_review("p1", "r1", "Great product!", 5);
    review_manager.add_review("p1", "r2", "Not bad", 3);

    // Testing get_review method
    auto review_r1 = review_manager.get_review("p1", "r1");
    if (review_r1) {
        std::cout << "Review r1 for p1: {";
        std::cout << "text: " << review_r1->text << ", ";
        std::cout << "rating: " << review_r1->rating << ", ";
        std::cout << "flagged: " << review_r1->flagged << "}\n";
    } else {
        std::cout << "Review r1 for p1 not found\n";
    }

    auto review_r3 = review_manager.get_review("p1", "r3");
    if (review_r3) {
        std::cout << "Review r3 for p1 found\n";
    } else {
        std::cout << "Review r3 for p1 not found\n";
    }

    // Testing delete_review method
    if (review_manager.delete_review("p1", "r2")) {
        std::cout << "Review r2 for p1 deleted successfully\n";
    } else {
        std::cout << "Failed to delete review r2 for p1\n";
    }

    auto review_r2 = review_manager.get_review("p1", "r2");
    if (review_r2) {
        std::cout << "Review r2 for p1 found\n";
    } else {
        std::cout << "Review r2 for p1 not found\n";
    }

    return 0;
}

This code establishes the foundational methods needed for managing product reviews within a ReviewManager class. The add_review method allows for adding a new review or updating an existing one, ensuring each review contains valid rating values between 1 and 5. The get_review method retrieves the review details for a specific product, including the review text and rating, returning std::nullopt if the product or review doesn't exist. The delete_review 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