-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscounttype.go
46 lines (38 loc) · 942 Bytes
/
discounttype.go
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
package receipt
import "math"
type DiscountType interface {
satisfied(*item) bool
process(*item)
}
type DiscountPercentage struct {
description string
percentage float64
threshold float64
}
type DiscountFree struct {
description string
total float64
free float64
}
func (p *DiscountPercentage) satisfied(item *item) bool {
if item.Paid >= p.threshold {
return true
}
return false
}
func (p *DiscountPercentage) process(item *item) {
item.DiscountPrice = item.Total * (1 - p.percentage)
item.Discount += item.DiscountPrice
item.Paid -= item.Discount
}
func (p *DiscountFree) satisfied(item *item) bool {
if item.Quantity >= p.total {
return true
}
return false
}
func (p *DiscountFree) process(item *item) {
item.DiscountQuantity = math.Floor(item.Quantity * p.free / p.total)
item.Discount += item.DiscountQuantity * item.Product.Price
item.Paid -= item.DiscountQuantity * item.Product.Price
}