File size: 1,368 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
}