Our professor gave us a problem of adding a "cashback" mechanism for an online store setting. We are tasked to use a parent class and a child class to use inheritance.
Goal: to add 50 after every third purchase
What I have so far:
Parent:
class Shop:
def __init__(self):
self.__balance = 0
def getBalance(self):
return self.__balance
def loadWallet(self, amount):
self.__balance = amount
def purchase(self, amount_val):
if amount_val <= self.__balance:
self.__balance -= amount_val
else:
print("Insufficient Funds")
Child:
class ShopCashBack(Shop):
def __init__(self, base_val=0):
self.__numpurchase = base_val
Shop.__init__(self)
def getPurch(self):
return self.__numpurchase
def purchase(self, amount_val):
self.__numpurchase = 1
Shop.purchase(self, amount_val)
if self.__numpurchase % 3 == 0:
result = self.getBalance() 50
print(result)
Input:
user = ShopCashBack()
user.loadWallet(5000)
user.purchase(299)
print(user.getBalance())
user.loadWallet(3000)
print(user.getBalance())
user.purchase(4990)
print(user.getBalance())
user.purchase(1607)
print(user.getBalance())
Result:
4701
7701
2711
1154
1104
Desired Result: where 1154 is 1104 50 (the balance after the third purchase)
4701
7701
2711
1154
CodePudding user response:
you need to change child perchase method
from this:
if self.__numpurchase % 3 == 0:
result = self.getBalance() 50
print(result)
to this:
if self.__numpurchase % 3 == 0:
self.loadWallet(50)
because you don't do anything to balance with result = self.getBalance() 50
balance stays the same after this line