Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update exercise-2.py #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions exercise-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,47 @@ def __init__(self, first_purchase_date, birth_date, is_veteran):
self.is_veteran = is_veteran


def calculate_discount_percentage(customer):
discount = 0
now = datetime.datetime.now()
year = datetime.timedelta(days=365)
if customer.birth_date <= now - 65*year:
# senior discount
discount = 5
if customer.first_purchase_date is not None:
if customer.first_purchase_date <= now - year:
# after one year, loyal customers get 10%
discount = 10
if customer.first_purchase_date <= now - 5*year:
# after five years, 12%
discount = 12
if customer.first_purchase_date <= now - 10*year:
# after ten years, 20%
discount = 20
else:
# first time purchase ==> 15% discount
discount = 15
if customer.is_veteran:
discount = max(discount, 10)
return discount

def senior_discount(value=5):
def calculate_discount(customer):
discount = 0
now = datetime.datetime.now()
year = datetime.timedelta(days=365)
if customer.birth_date <= now - 65*year:
discount = value
return discount
return calculate_discount

def loyal_discount(first_time=15, one_year=10, five_years=12, ten_years=20):
def calculate_discount(customer):
discount = 0
now = datetime.datetime.now()
year = datetime.timedelta(days=365)
if customer.first_purchase_date is not None:
if customer.first_purchase_date <= now - year:
# after one year, loyal customers get 10%
discount = one_year
if customer.first_purchase_date <= now - 5*year:
# after five years, 12%
discount = five_years
if customer.first_purchase_date <= now - 10*year:
# after ten years, 20%
discount = ten_years
else:
# first time purchase ==> 15% discount
discount = first_time
return discount
return calculate_discount

def veteran_discount(value=10):
def calculate_discount(customer):
discount = 0
if customer.is_veteran:
discount = value
return discount
return calculate_discount

def calculate_discount_percentage(customer, discounts=[senior_discount(), loyal_discount(), veteran_discount()]):
return max([discount(customer) for discount in discounts])

class CalculateDiscountPercentageTests(unittest.TestCase):
def setUp(self):
Expand Down