/
vladimir_p
/
practic3_python
Обзор
Документация
Войти
/
vladimir_p
/
practic3_python
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
3.py
165 строк
6 KB
Vladimir P.
upload files
26 окт 2025, 21:47
26 окт 2025, 21:47
084c9a4
Код
Авторство
О чём код?
from datetime import datetime, date class Product: """Represents a single product in the online store.""" def __init__(self, name: str, category: str, price: float, availability: bool) -> None: """ Initializes a Product instance. Args: name (str): The name of the product. category (str): The category of the product. price (float): The price of the product per unit. availability (bool): The availability status of the product. """ self.name = name self.price = price self.category = category self.availability = availability class Order: """Represents an order for a specific product, including quantity and discounts.""" def __init__(self, product: Product, quantity: int, discount: float, tax: float = 13): """ Initializes an Order instance. Args: product (Product): The product being ordered. quantity (int): The quantity of the product. discount (float): The discount percentage for the order. tax (float, optional): The tax percentage. Defaults to 13. """ self.product = product self.quantity = quantity self.tax = tax self.discount = discount @property def tax_sum(self) -> float: """Calculates the total tax amount for the order.""" return self.product.price * self.quantity * (self.tax / 100) @property def price(self) -> float: """Calculates the final price of the order including tax and discount.""" base_price_with_tax = self.product.price * self.quantity + self.tax_sum final_price = base_price_with_tax * (1 - self.discount / 100) return round(final_price, 2) def info(self) -> str: """Returns a string with formatted information about the order.""" return f"Name: {self.product.name}, Quantity: {self.quantity}, Price: {self.price}" class Customer: """Represents a customer with personal info and order history.""" def __init__(self, full_name: str, age: int, registration_date: date, order_history: list) -> None: """ Initializes a Customer instance. Args: full_name (str): The full name of the customer. age (int): The age of the customer. registration_date (date): The date of registration. order_history (list): A list of the customer's past orders. """ self.full_name = full_name self.age = age self.registration_date = registration_date self.order_history = order_history def add_order(self, order: Order) -> None: """Adds a completed order to the customer's history.""" self.order_history.append(order) def info(self) -> str: """Returns a string with formatted information about the customer.""" return f"Full name: {self.full_name}, age: {self.age}, registration date: {self.registration_date}" def __str__(self) -> str: """Returns the full name of the customer.""" return self.full_name class ShoppingCart: """Manages a collection of orders before checkout.""" def __init__(self) -> None: """Initializes an empty shopping cart.""" self.shopping_cart: list[Order] = [] def add_order(self, order: Order) -> None: """ Adds an order to the shopping cart. If the product is already in the cart, its quantity is updated. """ if order.product.availability: for existing_order in self.shopping_cart: if existing_order.product is order.product: existing_order.quantity += order.quantity print(f"Updated quantity for '{order.product.name}'. New quantity: {existing_order.quantity}") return self.shopping_cart.append(order) print(f"Product '{order.product.name}' added to the cart.") else: print(f"Product '{order.product.name}' is not available.") def remove_order(self, product: Product) -> None: """Removes an order from the shopping cart based on the product.""" order_to_remove = None for order in self.shopping_cart: if order.product is product: order_to_remove = order break if order_to_remove: self.shopping_cart.remove(order_to_remove) print(f"Product '{product.name}' removed from the shopping cart.") else: print(f"Product '{product.name}' not found to remove.") def change_quantity(self, product: Product, new_quantity: int) -> None: """Changes the quantity of a product in the shopping cart.""" for order in self.shopping_cart: if order.product is product: order.quantity = new_quantity print(f"Quantity for '{product.name}' changed to {new_quantity}.") return print(f"Product '{product.name}' not found in the shopping cart.") def info(self) -> None: """Prints formatted information about the contents of the shopping cart.""" print("\n--- Shopping Cart Info ---") if not self.shopping_cart: print("The shopping cart is empty.") print("--------------------------\n") return total_price = 0 for order in self.shopping_cart: print(order.info()) total_price += order.price print("--------------------------") print(f"Total Cart Price: {round(total_price, 2)}") print("--------------------------\n") my_cart = ShoppingCart() test_customer = Customer("Ivan", 27, datetime(2020, 1, 1).date(), []) product1 = Product("Bananas", "Fruits", 250, True) product2 = Product("Tomatoes", "Vegetable", 130, True) order1 = Order(product1, 5, 5, 13) order2 = Order(product2, 10, 5, 13) test_customer.add_order(order1) test_customer.add_order(order2) my_cart.add_order(order1) my_cart.add_order(order2) my_cart.info() my_cart.change_quantity(product2, 25) my_cart.info()