feat(curriculum): add discount calculator workshop#62644
feat(curriculum): add discount calculator workshop#62644Dario-DC merged 32 commits intofreeCodeCamp:mainfrom
Conversation
Dario-DC
left a comment
There was a problem hiding this comment.
Left comments to write the missing tests (they should work but I did not verify)
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a4.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e5293bd00d2fe134f58982.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e5293bd00d2fe134f58983.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e5293bd00d2fe134f58984.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e5293bd00d2fe134f58987.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a2.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e9207ddcba053c5cb24413.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e529226b0defdff26c4a89.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e529226b0defdff26c4a89.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e529226b0defdff26c4a8a.md
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e529226b0defdff26c4a8a.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a6.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a6.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a7.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a7.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a7.md
Outdated
Show resolved
Hide resolved
Dario-DC
left a comment
There was a problem hiding this comment.
We should add the return type to the methods that don't have one. -> None for __init__ methods and -> str for __str__.
curriculum/challenges/english/blocks/workshop-discount-calculator/68e529226b0defdff26c4a89.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e529226b0defdff26c4a8a.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e529226b0defdff26c4a8b.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e5293bd00d2fe134f58985.md
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec5309f.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a4.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a6.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a6.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a6.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a6.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e529226b0defdff26c4a8a.md
Show resolved
Hide resolved
|
@Dario-DC, before i make changes to the instructions and add tests, can you verify if this should be the final solution after adding return types to Code updatedfrom abc import ABC, abstractmethod
from typing import List
class Product:
def __init__(self, name: str, price: float) -> None:
self.name = name
self.price = price
class DiscountStrategy(ABC):
@abstractmethod
def is_applicable(self, product: Product, user_tier: str) -> bool:
pass
@abstractmethod
def apply_discount(self, product: Product) -> float:
pass
class PercentageDiscount(DiscountStrategy):
def __init__(self, percent: float) -> None:
self.percent = percent
def is_applicable(self, product: Product, user_tier: str) -> bool:
return self.percent <= 70
def apply_discount(self, product: Product) -> float:
return product.price * (1 - self.percent / 100)
class FixedAmountDiscount(DiscountStrategy):
def __init__(self, amount: float) -> None:
self.amount = amount
def is_applicable(self, product: Product, user_tier: str) -> bool:
return product.price*0.9 > self.amount
def apply_discount(self, product: Product) -> float:
return product.price - self.amount
class PremiumUserDiscount(DiscountStrategy):
def is_applicable(self, product: Product, user_tier: str) -> bool:
return user_tier.lower() == 'premium'
def apply_discount(self, product: Product) -> float:
return product.price * 0.80 # 20% off for premium users
class DiscountEngine:
def __init__(self, strategies: List[DiscountStrategy]) -> None:
self.strategies = strategies
def calculate_best_price(self, product: Product, user_tier: str) -> float:
prices = [product.price]
for strategy in self.strategies:
if strategy.is_applicable(product, user_tier):
discounted = strategy.apply_discount(product)
prices.append(discounted)
return min(prices)
if __name__ == '__main__':
product = Product('Wireless Mouse', 50.0)
user_tier = 'Premium'
strategies = [
PercentageDiscount(10),
FixedAmountDiscount(5),
PremiumUserDiscount()
]
engine = DiscountEngine(strategies)
best_price = engine.calculate_best_price(product, user_tier)
print(f'Best price for {product.name} for {user_tier} user: ${best_price:.2f}') |
@zairahira the def __str__(self) -> None:
return f'{self.name} - ${self.price}'I've just realized that |
curriculum/challenges/english/blocks/workshop-discount-calculator/68e5293bd00d2fe134f58985.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e7a24b2482bd5fa88774fa.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a6.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com>
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a7.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com>
curriculum/challenges/english/blocks/workshop-discount-calculator/68e79d91daf8fb4808f93c47.md
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e5293bd00d2fe134f5898a.md
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a1.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a6.md
Outdated
Show resolved
Hide resolved
curriculum/challenges/english/blocks/workshop-discount-calculator/68e52994bc7ea2e3dec530a7.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com>
Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com>
Checklist:
mainbranch of freeCodeCamp.Closes freeCodeCamp/CurriculumExpansion#989
Requires curriculum helpers to have the changes in freeCodeCamp/curriculum-helpers#509