Hone logo
Hone
Problems

Data Class Delight: Representing Product Information

Dataclasses in Python provide a concise and convenient way to create classes primarily intended to store data. This challenge will guide you in using dataclasses to represent product information, demonstrating their ability to automatically generate methods like __init__, __repr__, and __eq__. Successfully completing this challenge will solidify your understanding of dataclasses and their benefits for data-centric classes.

Problem Description

You are tasked with creating a Product dataclass that stores information about a product. The dataclass should have the following attributes:

  • name: A string representing the product's name.
  • price: A float representing the product's price.
  • quantity: An integer representing the quantity in stock.
  • sku: A string representing the product's Stock Keeping Unit (SKU). This should be a unique identifier.

The dataclass should automatically generate the standard methods for initialization, string representation, and equality comparison. You should also add a method called calculate_total_value that returns the total value of the product in stock (price * quantity).

Key Requirements:

  • Use the @dataclass decorator from the dataclasses module.
  • Define the attributes name, price, quantity, and sku with appropriate type hints.
  • Implement the calculate_total_value method within the Product class.
  • Ensure the dataclass handles potential errors gracefully (e.g., invalid price or quantity). While explicit error handling isn't required, consider how the dataclass would behave with unexpected input.

Expected Behavior:

  • Instances of the Product dataclass should be created easily using the automatically generated __init__ method.
  • The __repr__ method should provide a clear and informative string representation of the object.
  • The __eq__ method should correctly compare two Product instances based on all their attributes.
  • The calculate_total_value method should return the correct total value.

Examples

Example 1:

Input: Product(name="Laptop", price=1200.00, quantity=5, sku="LP-1234")
Output: Product(name='Laptop', price=1200.0, quantity=5, sku='LP-1234')
Explanation: Creates a Product instance with the given attributes. The output shows the string representation generated by __repr__.

Example 2:

Input: product1 = Product(name="Mouse", price=25.00, quantity=100, sku="MS-5678")
product2 = Product(name="Mouse", price=25.00, quantity=100, sku="MS-5678")
print(product1 == product2)
Output: True
Explanation:  Demonstrates the equality comparison using __eq__. Two products with identical attributes are considered equal.

Example 3:

Input: product = Product(name="Keyboard", price=75.00, quantity=20, sku="KB-9012")
print(product.calculate_total_value())
Output: 1500.0
Explanation: Calculates the total value of the product (75.00 * 20 = 1500.0).

Constraints

  • price must be a non-negative float.
  • quantity must be a non-negative integer.
  • name and sku must be non-empty strings.
  • The calculate_total_value method should return a float.

Notes

  • Consider using type hints to improve code readability and maintainability.
  • The dataclass decorator automatically handles many common tasks, reducing boilerplate code.
  • Think about how you might extend the Product dataclass with additional methods or attributes in the future.
  • While explicit error handling isn't strictly required, consider how the dataclass would behave with invalid input values. For example, what happens if price is negative? The default behavior is acceptable for this challenge.
Loading editor...
python