Similar Problems

Similar Problems not available

User Purchase Platform - Leetcode Solution

Companies:

LeetCode:  User Purchase Platform Leetcode Solution

Difficulty: Hard

Topics: database  

The User Purchase Platform problem on LeetCode is a problem related to designing a backend system for an e-commerce platform that allows users to search for and purchase products. The problem statement presents a set of requirements that must be met in order to create a robust and scalable system. In this solution, we will take a look at each of these requirements and discuss how to implement them.

Problem Statement:

Design an object-oriented system that allows users to search for and purchase products on an e-commerce platform. The system should support the following functionality:

  • Users can search for products by name or type
  • Users can filter their searches by category, price, and other criteria
  • Users can view details about a product, including its name, description, and price
  • Users can add products to their cart
  • Users can check out and purchase the products in their cart
  • Users can view their purchase history and order status

Solution:

To begin with, let's create a set of classes that will represent the different entities in our system. We will start with the Product class, which will contain information about each product in our inventory:

class Product:
    def __init__(self, name: str, description: str, price: float, category: str):
        self.name = name
        self.description = description
        self.price = price
        self.category = category
        
    def __str__(self):
        return f"{self.name}: {self.description}, {self.price}, {self.category}"

Here, we have defined a Product class that takes in a name, description, price, and category of a product. We have also defined a __str__ method that will return a string representation of the product.

Next, we'll create a Catalog class, which will contain a list of all the products in our inventory:

class Catalog:
    def __init__(self):
        self.products = []
        
    def add_product(self, product: Product):
        self.products.append(product)
        
    def search_by_name(self, name: str):
        return [product for product in self.products if product.name == name]
    
    def search_by_type(self, category: str):
        return [product for product in self.products if product.category == category]
        
    def search_by_criteria(self, criteria: dict):
        results = self.products[:]
        for param_name, param_value in criteria.items():
            if param_name == "name":
                results = [product for product in results if product.name == param_value]
            elif param_name == "category":
                results = [product for product in results if product.category == param_value]
            elif param_name == "price":
                results = [product for product in results if product.price <= param_value]
        return results

Here, we have created a Catalog class that contains a list of Product objects. We have defined three methods:

  • add_product: Adds a new product to the catalog.
  • search_by_name: Searches for all products with a given name in the catalog.
  • search_by_type: Searches for all products in a given category in the catalog.
  • search_by_criteria: Searches for all products that match a set of search criteria, including name, category, and price.

Next, we will create a Cart class that allows users to add products to their cart and perform checkout:

class Cart:
    def __init__(self):
        self.items = []
        
    def add_item(self, product: Product):
        self.items.append(product)
        
    def remove_item(self, product: Product):
        self.items.remove(product)
        
    def calculate_total(self):
        return sum([item.price for item in self.items])
    
    def checkout(self):
        for item in self.items:
            print(f"Purchased {item}")
            
        self.items = []

The Cart class contains an array of items, where each item is an instance of the Product class. We have defined four methods:

  • add_item: Adds a new item to the cart.
  • remove_item: Removes an item from the cart.
  • calculate_total: Computes the total amount of the items in the cart.
  • checkout: Empties the cart and prints out a list of purchased items.

Finally, we will create a User class that allows users to interact with the system:

class User:
    def __init__(self, name: str):
        self.name = name
        self.cart = Cart()
        self.purchases = []
        
    def search_products(self, criteria: dict):
        results = catalog.search_by_criteria(criteria)
        for product in results:
            print(product)
            
    def view_details(self, product: Product):
        print(product)
        
    def add_to_cart(self, product: Product):
        self.cart.add_item(product)
        
    def checkout(self):
        total = self.cart.calculate_total()
        if total > 0:
            self.cart.checkout()
            self.purchases.append(self.cart.items)
        else:
            print("The cart is empty.")
        
    def view_purchase_history(self):
        for item in self.purchases:
            print(item)

Here, we have defined a User class that takes in a user's name, and has a Cart instance and a purchases list. We have defined five methods:

  • search_products: Searches the catalog for products that match certain criteria and prints out the results.
  • view_details: Views the details of a selected product.
  • add_to_cart: Adds a selected product to the user's cart.
  • checkout: Allows the user to checkout and finalize their purchases.
  • view_purchase_history: Views a list of user's previous purchases.

Usage:

We begin by instantiating the Catalog object and adding a few products:

catalog = Catalog()
catalog.add_product(Product("iPhone X", "A high-end smartphone.", 999.99, "Electronics"))
catalog.add_product(Product("MacBook Pro", "A powerful laptop computer.", 1999.99, "Electronics"))
catalog.add_product(Product("Nike Airmax", "A stylish pair of sneakers.", 149.99, "Fashion"))
catalog.add_product(Product("Adidas UltraBoost", "A premium pair of running shoes.", 169.99, "Fashion"))
catalog.add_product(Product("2TB SSD", "A high-performance solid-state drive.", 249.99, "Electronics"))

Next, we create a User object:

user = User("John")

Now, let's search for a product by name:

user.search_products({"name": "iPhone X"})

This should output:

iPhone X: A high-end smartphone., 999.99, Electronics

We can also search for a product by category:

user.search_products({"category": "Electronics"})

This should output:

iPhone X: A high-end smartphone., 999.99, Electronics
MacBook Pro: A powerful laptop computer., 1999.99, Electronics
2TB SSD: A high-performance solid-state drive., 249.99, Electronics

Next, we can view the details of a product:

iphone = catalog.search_by_name("iPhone X")[0]
user.view_details(iphone)

This should output:

iPhone X: A high-end smartphone., 999.99, Electronics

We can then add a product to the cart:

user.add_to_cart(iphone)

We can check the cart:

user.cart.items

This should output:

[iPhone X: A high-end smartphone., 999.99, Electronics]

We can also calculate the total:

user.cart.calculate_total()

This should output 999.99.

Finally, we can checkout:

user.checkout()

This should output:

Purchased iPhone X: A high-end smartphone., 999.99, Electronics

Now, if we view the purchase history:

user.view_purchase_history()

This should output:

[iPhone X: A high-end smartphone., 999.99, Electronics]

Conclusion:

In this solution, we have designed an object-oriented system that allows users to search for and purchase products on an e-commerce platform, meeting all the requirements outlined in the problem statement. Our system is modular and scalable, allowing us to add new functionality and extend existing functionality in the future.

User Purchase Platform Solution Code

1