-
Notifications
You must be signed in to change notification settings - Fork 2
/
listing30.go
42 lines (31 loc) · 973 Bytes
/
listing30.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
// Code listing 30: https://play.golang.org/p/ufYS79Dx4BO
package main
import (
"fmt"
)
type Discount struct {
percent float32
promotionId string
}
type ManagersSpecial struct {
Discount
extraoff float32
}
func main() {
normalPrice := float32(99.99)
januarySale := Discount{15.00, "January"}
managerSpecial := ManagersSpecial{januarySale, 10.00}
discountedPrice := januarySale.Calculate(normalPrice)
managerDiscount := managerSpecial.Calculate(normalPrice)
fmt.Printf("Original price: $%4.2f\n", normalPrice)
fmt.Printf("Discount percentage: %2.2f\n",
januarySale.percent)
fmt.Printf("Discounted price: $%4.2f\n", discountedPrice)
fmt.Printf("Manager's special: $%4.2f\n", managerDiscount)
}
func (d Discount) Calculate(originalPrice float32) float32 {
return originalPrice - (originalPrice / 100 * d.percent)
}
func (ms ManagersSpecial) Calculate(originalPrice float32) float32 {
return ms.Discount.Calculate(originalPrice) - ms.extraoff
}