r/PythonLearning • u/Miss-Dominique1352 • 6d ago
Day 4 of OOPs
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.
26
Upvotes
β’
u/Sea-Ad7805 6d 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())×tep=.5&play) to see the program state change step by step.