73.2k views
22 votes
Robert has opened his own pet supply store so he can help himself to treats and toys whenever he wishes. In order to encourage customers to shop at his store more, he is implementing a customer loyalty program. For every $100 spent, the customer earns a 10% discount on a future purchase. If the customer has earned a discount, that discount will be automatically applied whenever they make a purchase. Only one discount can be applied per purchase. Implement a class Customer that represents a customer in Robert's store.

1 Answer

9 votes

Answer:

Step-by-step explanation:

The following code is written in Python and creates a class called customer which holds the customers name, purchase history amount, and current total. It also has 3 functions, a constructor that takes the customer name as a parameter. The add_to_cart function which increases the amount of the current total. And finally the checkout function which applies the available coupon and resets the variables if needed, as well as prints the info the to screen.

class Customer():

customer = ""

purchased_history = 0

current_total = 0

def __init__(self, name):

self.customer = name

def add_to_cart(self, amount):

self.current_total += amount

def checkout(self):

if self.purchased_history >= 100:

self.current_total *= 0.90

self.purchased_history = 0

else:

self.purchased_history += self.current_total

print(self.customer + " current total is: $" + str(self.current_total))

self.current_total = 0

User Sid Malani
by
3.2k points