removed dataclass, added docstrings to entities
parent
0be0187062
commit
997b3765d0
|
|
@ -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]
|
||||
"""
|
||||
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})"
|
||||
|
|
@ -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
|
||||
"""
|
||||
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})"
|
||||
|
|
@ -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.")
|
||||
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})"
|
||||
Loading…
Reference in New Issue