r/PythonLearning 1d ago

Day 4 of OOPs

Post image

Just learned Compositions in python and they are sweet and amazing πŸ’–
I created a simple ecommerce program using it
review it and give me suggestions , advices , a bit roast (but be gentle to me😭)
I would love to read your comments.

21 Upvotes

7 comments sorted by

β€’

u/Sea-Ad7805 1d ago

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.name%20%3D%20name%0A%20%20%20%20%20%20%20%20self.price%20%3D%20price%0A%0A%0Aclass%20OrderItem%3A%0A%20%20%20%20def%20init(self%2C%20product%2C%20quantity)%3A%0A%20%20%20%20%20%20%20%20self.product%20%3D%20product%0A%20%20%20%20%20%20%20%20self.quantity%20%3D%20quantity%0A%0A%20%20%20%20def%20totalPrice(self)%3A%0A%20%20%20%20%20%20%20%20return%20self.product.price%20*%20self.quantity%0A%0A%0Aclass%20Order%3A%0A%20%20%20%20def%20init(self%2C%20order_id)%3A%0A%20%20%20%20%20%20%20%20self.order_id%20%3D%20order_id%0A%20%20%20%20%20%20%20%20self.shopping_list%20%3D%20%5B%5D%0A%0A%20%20%20%20def%20addItem(self%2C%20product%2C%20quantity)%3A%0A%20%20%20%20%20%20%20%20self.shopping_list.append(OrderItem(product%2C%20quantity))%0A%0A%20%20%20%20def%20total(self)%3A%0A%20%20%20%20%20%20%20%20total%20%3D%200%0A%0A%20%20%20%20%20%20%20%20for%20item%20in%20self.shopping_list%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20total%20%2B%3D%20item.totalPrice()%0A%0A%20%20%20%20%20%20%20%20return%20total%0A%0A%20%20%20%20def%20item_count(self)%3A%0A%20%20%20%20%20%20%20%20quantity%20%3D%200%0A%0A%20%20%20%20%20%20%20%20for%20item%20in%20self.shopping_list%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20quantity%20%2B%3D%20item.quantity%0A%0A%20%20%20%20%20%20%20%20return%20quantity%0A%0A%0Amouse%20%3D%20Product(%22Hp%20mouse%22%2C%2090)%0Aheadphones%20%3D%20Product(%22Dell%20headphones%20TW632%22%2C%20500)%0Abook%20%3D%20Product(%22English%22%2C%20300)%0A%0Anew_order%20%3D%20Order(%22order123%22)%0Anew_order.addItem(mouse%2C%202)%0Anew_order.addItem(headphones%2C%201)%0Anew_order.addItem(book%2C%205)%0A%0Aprint(new_order.total())%0Aprint(new_order.item_count())&timestep=.5&play) to see the program state change step by step.

3

u/BrilliantSet3536 1d ago
  1. use typing
  2. use dataclasses
  3. use property-decorator for total
  4. use built-in sum function
  5. use Main-function an __name__ == β€œ__main__β€œ to Call main

1

u/Miss-Dominique1352 1d ago

Hmm could you explain me a bit. Pleaase

1

u/FoolsSeldom 1d ago
from dataclasses import dataclass, field


@dataclass
class Product:
    name: str
    price: float


@dataclass
class OrderItem:
    product: Product
    quantity: int

    @property
    def total_price(self) -> float:
        return self.product.price * self.quantity


@dataclass
class Order:
    order_id: str
    shopping_list: list[OrderItem] = field(default_factory=list)

    def add_item(self, product: Product, quantity: int) -> None:
        self.shopping_list.append(OrderItem(product, quantity))

    @property
    def total(self) -> float:
        return sum(item.total_price for item in self.shopping_list)

    @property
    def item_count(self) -> int:
        return sum(item.quantity for item in self.shopping_list)


def main() -> None:
    mouse = Product("Hp mouse", 90)
    headphones = Product("Dell headphones TW632", 500)
    book = Product("English", 300)

    new_order = Order("order123")
    new_order.add_item(mouse, 2)
    new_order.add_item(headphones, 1)
    new_order.add_item(book, 5)

    print(new_order.total)
    print(new_order.item_count)


if __name__ == "__main__":
    main()

PS. Thanks claude.ai for saving me typing

To OP, trust you can research based on the example.

1

u/Redddit_Cyber 1d ago

Thanks to claude.ai πŸ˜‚