|
| 1 | +''' |
| 2 | +However, the solution of hardcoding the value in the attribute is not a good one. |
| 3 | +For example, since this is a limited time discount we should be able to programmatically |
| 4 | +enable and disable the discount using functions like this: |
| 5 | +''' |
| 6 | + |
| 7 | + |
| 8 | +class Mobile: |
| 9 | + def __init__(self, price, brand): |
| 10 | + self.price = price |
| 11 | + self.brand = brand |
| 12 | + self.discount = 0 |
| 13 | + |
| 14 | + def purchase(self): |
| 15 | + total = self.price - self.price * self.discount / 100 |
| 16 | + print (self.brand, "mobile with price", self.price, "is available after discount at", total) |
| 17 | + |
| 18 | +def enable_discount(list_of_mobiles): |
| 19 | + for mobile in list_of_mobiles: |
| 20 | + mobile.discount=50 |
| 21 | + |
| 22 | +def disable_discount(list_of_mobiles): |
| 23 | + for mobile in list_of_mobiles: |
| 24 | + mobile.discount=0 |
| 25 | + |
| 26 | +mob1=Mobile(20000, "Apple") |
| 27 | +mob2=Mobile(30000, "Apple") |
| 28 | +mob3=Mobile(5000, "Samsung") |
| 29 | +mob4=Mobile(6000, "Samsung") |
| 30 | + |
| 31 | +list_of_mobiles=[mob1,mob2,mob3,mob4] |
| 32 | + |
| 33 | +mob1.purchase() |
| 34 | + |
| 35 | +enable_discount(list_of_mobiles) |
| 36 | + |
| 37 | +mob2.purchase() |
| 38 | +mob3.purchase() |
| 39 | + |
| 40 | +disable_discount(list_of_mobiles) |
| 41 | + |
| 42 | +mob4.purchase() |
0 commit comments