Назад к задачам
Junior — Senior
6

Определение нарушений принципов GRASP в представленном коде

Получайте помощь с лайвкодингом в реальном времени с Sobes Copilot
Условие задачи

Определите, какие из принципов GRASP нарушаются в следующем фрагменте кода.


class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def calculate_discount(self, discount_percentage):
        return self.price * (1 - discount_percentage / 100)

class ShoppingCart:
    def __init__(self):
        self.products =
        self.total_price = 0

    def add_product(self, product):
        self.products.append(product)
        self.total_price += product.price

    def remove_product(self, product):
        if product in self.products:
            self.products.remove(product)
            self.total_price -= product.price

    def apply_discount(self, discount_percentage):
        for product in self.products:
            self.total_price -= product.price - product.calculate_discount(discount_percentage)

    def checkout(self, customer):
        order = Order(customer, self.products)
        order.place_order()

class Order:
    def __init__(self, customer, products):
        self.customer = customer
        self.products = products

    def place_order(self):
        print("Order placed successfully!")

class Customer:
    def __init__(self, name, email):
        self.name = name
        self.email = email

    def add_product_to_cart(self, product, shopping_cart):
        shopping_cart.add_product(product)

    def remove_product_from_cart(self, product, shopping_cart):
        shopping_cart.remove_product(product)

    def checkout(self, shopping_cart):
        shopping_cart.checkout(self)

# Main program
def main():
    customer = Customer("John Doe", "john@example.com")
    product1 = Product("Phone", 500)
    product2 = Product("Laptop", 1000)

    shopping_cart = ShoppingCart()
    customer.add_product_to_cart(product1, shopping_cart)
    customer.add_product_to_cart(product2, shopping_cart)
    customer.checkout(shopping_cart)

if __name__ == "__main__":
    main()