Download task_046/solution/code/promotion.go from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_046/solution/code/promotion.go
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_046/solution/code/promotion.go
-
curl -L -o promotion.go https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_046/solution/code/promotion.go
1.37 kB
| package promotion | |
| type Order struct { | |
| OriginalAmount float64 | |
| UserLevel string | |
| Category string | |
| } | |
| type Strategy interface { | |
| Discount(Order) float64 | |
| } | |
| type FullReductionStrategy struct{} | |
| func (FullReductionStrategy) Discount(order Order) float64 { | |
| switch { | |
| case order.OriginalAmount >= 1000: | |
| return 100 | |
| case order.OriginalAmount >= 500: | |
| return 50 | |
| case order.OriginalAmount >= 200: | |
| return 20 | |
| default: | |
| return 0 | |
| } | |
| } | |
| type UserLevelDiscountStrategy struct{} | |
| func (UserLevelDiscountStrategy) Discount(order Order) float64 { | |
| rates := map[string]float64{"silver": 0.05, "gold": 0.10, "platinum": 0.15} | |
| return order.OriginalAmount * rates[order.UserLevel] | |
| } | |
| type CategoryDiscountStrategy struct{} | |
| func (CategoryDiscountStrategy) Discount(order Order) float64 { | |
| rates := map[string]float64{"electronics": 0.08, "clothing": 0.12, "books": 0.20} | |
| return order.OriginalAmount * rates[order.Category] | |
| } | |
| type PromotionCalculator struct { | |
| strategies []Strategy | |
| } | |
| func NewPromotionCalculator(strategies ...Strategy) PromotionCalculator { | |
| return PromotionCalculator{strategies: strategies} | |
| } | |
| func (c PromotionCalculator) FinalAmount(order Order) float64 { | |
| discount := 0.0 | |
| for _, strategy := range c.strategies { | |
| discount += strategy.Discount(order) | |
| } | |
| final := order.OriginalAmount - discount | |
| if final < 0 { | |
| return 0 | |
| } | |
| return final | |
| } | |