Build a Discount Calculator - Step 30

Tell us what’s happening:

It’s expecting java syntax in my python program so it’s not accepting my code even though I believe it to be correct. It’s telling me it’s not being approved because I’m not using the new keyword and let or const when creating a class instance, but this is not a thing in Python

Your code so far

from abc import ABC, abstractmethod

class Product:
    def __init__(self, name: str, price: float) -> None:
        self.name = name
        self.price = price

    def __str__(self) -> str:
        return f'{self.name} - ${self.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: int) -> 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: int) -> 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.8

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()
    ]
# User Editable Region

engine = DiscountEngine(strategies) #This is where it's telling me to put const engine = new DiscountEngine(strategies)
best_price = engine.calculate_best_price(product, user_tier)    
# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.2 Safari/605.1.15

Challenge Information:

Build a Discount Calculator - Step 30

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-discount-calculator/68efb04d5ea87cf267192ed4.md at main · freeCodeCamp/freeCodeCamp · GitHub

it is not expecting Java syntax, why do you think so?

double check where you should write the new code. You are working in the body of if __name__ == '__main__':, aren’t you?

Same problem. The “Ask Socrates” keeps saying to use the new keyword. It seems like this step is expecting some other language than Python.

If I try to humor it and add new before DiscountEngine(strategies) on line 70, it gives me this:

Oh… it’s an indent error.

The Socrates hints were irrelevant and super misleading on this step. I don’t use them often, but when I do they’re usually spot on.