My objective: Override the process_sale method so that 2% of each sale is added to rewards.
My problem: How did I get 0 in the output?
Exercise 5:
Use the parent classes to the left to help you solve this problem. The first parent is the Person class with some very generic information. The second class, CardHolder, is the class for a credit card holder. Create the child class PlantinumClient. This class inherits all of the attributes of both parent classes. In addition, the child class has the attributes cash_back and rewards. cash_back should be set to 0.02 and rewards should be set to 0. Override the process_sale method so that 2% of each sale is added to rewards.
class Person:
def __init__(self, name, address):
self.name = name
self.address = address
def get_info(self):
return f"{self.name} lives at {self.address}."
class CardHolder:
def __init__(self, account_number):
self.account_number = account_number
self.balance = 0
self.credit_limit = 5000
def process_sale(self, price):
self.balance = price
def make_payment(self, amount):
self.balance -= amount
# declare child class here
class PlatinumClient(Person, CardHolder):
def __init__(self, name, address, account_number):
super().__init__(name, address)
super(Person, self).__init__(account_number)
self.cash_back = 0.02
self.rewards = 0
def process_sale(self, price):
self.rewards = int( (price * 0.02) )
platinum = PlatinumClient("Sarah", "101 Main Street", 123364)
# Task: Expected output:
platinum.process_sale(100) # n/a
print(platinum.rewards) # 2
print(platinum.balance) # 100
platinum.make_payment(50) # n/a
print(platinum.balance) # 50
print(platinum.get_info()) # Sarah lives at 101 Main Street.
CodePudding user response:
You're overriding the process_sale
, but are not calling the original method. So no balance is added. You can see this by running the expected output (printing platinum.balance
yields 0, but should yield 100
).