From 997b3765d012980b04cd0f2a29608844fd4e14b6 Mon Sep 17 00:00:00 2001 From: Felix Mucha <3016498@stud.hs-mannheim.de> Date: Sat, 1 Feb 2025 19:06:30 +0100 Subject: [PATCH] removed dataclass, added docstrings to entities --- src/entities/cart.py | 57 +++++++++++++++++++++++++++++++++------ src/entities/cart_item.py | 49 +++++++++++++++++++++++++++------ src/entities/product.py | 44 +++++++++++++++++++++++------- 3 files changed, 124 insertions(+), 26 deletions(-) diff --git a/src/entities/cart.py b/src/entities/cart.py index dcb6d08..e02a694 100644 --- a/src/entities/cart.py +++ b/src/entities/cart.py @@ -1,28 +1,69 @@ #python imports from typing import List -from dataclasses import dataclass, field #dependency imports from .cart_item import CartItem from .product import Product -@dataclass class Cart: - items: List[CartItem] - total_price: float = field(init=False, default=0.0) + """ + Represents a shopping cart containing multiple cart items. + """ + + def __init__(self): + """ + Initializes a new shopping cart. + """ + self.items: List[CartItem] = [] + self.total_price: float = 0.0 def add_item(self, product: Product, quantity: int): + """ + Adds a product to the cart or updates the quantity if it already exists. + + Args: + product (Product): The product to add to the cart. + quantity (int): The quantity of the product to add. + """ for item in self.items: - if item.product.id == product.id: + if item.product_id == product.id: item.quantity += quantity return - self.items.append(CartItem(product=product, quantity=quantity)) + self.items.append(CartItem(product_id=product.id, name=product.name, quantity=quantity, price=product.price)) def remove_item(self, product_id: str): - self.items = [item for item in self.items if item.product.id != product_id] + """ + Removes a product from the cart by its ID. + + Args: + product_id (str): The unique identifier of the product to remove. + """ + self.items = [item for item in self.items if item.product_id != product_id] def calculate_total_price(self) -> float: + """ + Calculates the total price of all items in the cart. + + Returns: + float: The total price of all items in the cart. + """ self.total_price = sum(item.calculate_total_price() for item in self.items) + return self.total_price def list_items(self) -> List[str]: - return [f"{item.quantity} x {item.product.name} - {item.calculate_total_price()} EUR" for item in self.items] \ No newline at end of file + """ + Lists all items in the cart with their quantities and total prices. + + Returns: + List[str]: A list of strings representing the items in the cart. + """ + return [f"{item.quantity} x {item.name} - {item.calculate_total_price()} EUR" for item in self.items] + + def __repr__(self): + """ + Returns a string representation of the cart. + + Returns: + str: A string representation of the cart. + """ + return f"Cart(items={self.items}, total_price={self.total_price})" \ No newline at end of file diff --git a/src/entities/cart_item.py b/src/entities/cart_item.py index 3278b03..b81faf1 100644 --- a/src/entities/cart_item.py +++ b/src/entities/cart_item.py @@ -1,17 +1,50 @@ #python imports from typing import List -from dataclasses import dataclass -@dataclass class CartItem: - product_id: int - name: str - quantity: int - price: float + """ + Represents an item in the cart with a product ID, name, quantity, and price. + """ + def __init__(self, product_id: int, name: str, quantity: int, price: float): + """ + Initializes a new cart item. + + Args: + product_id (int): The unique identifier for the product. + name (str): The name of the product. + quantity (int): The quantity of the product in the cart. + price (float): The price of the product. + """ + self.product_id = product_id + self.name = name + self.quantity = quantity + self.price = price + self.post_init() def post_init(self): + """ + Validates the cart item's quantity. + + Raises: + ValueError: If the quantity is less than or equal to 0. + """ if self.quantity <= 0: - raise ValueError("Quantity has to be atleast 1") + raise ValueError("Quantity has to be at least 1") def calculate_total_price(self) -> float: - return self.quantity * self.price \ No newline at end of file + """ + Calculates the total price of the cart item. + + Returns: + float: The total price of the cart item. + """ + return self.quantity * self.price + + def __repr__(self): + """ + Returns a string representation of the cart item. + + Returns: + str: A string representation of the cart item. + """ + return f"CartItem(product_id={self.product_id}, name={self.name}, quantity={self.quantity}, price={self.price})" \ No newline at end of file diff --git a/src/entities/product.py b/src/entities/product.py index 0dba882..8797683 100644 --- a/src/entities/product.py +++ b/src/entities/product.py @@ -1,14 +1,38 @@ -#python imports -from dataclasses import dataclass - -@dataclass class Product: - id: str - name: str - description: str - price: float - quantity: int + """ + Represents a product with an ID, name, description, price, and quantity. + """ + + def __init__(self, id: str, name: str, description: str, price: float, quantity: int): + """ + Initializes a new product. + + Args: + id (str): The unique identifier for the product. + name (str): The name of the product. + description (str): A brief description of the product. + price (float): The price of the product. + quantity (int): The available quantity of the product. + """ + self.id = id + self.name = name + self.description = description + self.price = price + self.quantity = quantity + self.post_init() def post_init(self): + """ + Validates the product's price. + + Raises: + ValueError: If the price is negative. + """ if self.price < 0: - raise ValueError("Der Preis darf nicht negativ sein.") \ No newline at end of file + raise ValueError("Der Preis darf nicht negativ sein.") + + def __repr__(self): + """ + Returns a string representation of the product. + """ + return f"Product(id={self.id}, name={self.name}, description={self.description}, price={self.price}, quantity={self.quantity})" \ No newline at end of file