-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookStore_test.go
101 lines (96 loc) · 1.9 KB
/
BookStore_test.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package bookstroe
import (
"testing"
)
func Test_calculatePrice(t *testing.T) {
type args struct {
books []int
}
tests := []struct {
name string // test name
args args
want float64 // expected value
}{
{
name: "Basic : zero book",
args: args{books: []int{1}},
want: 8,
},
{
name: "Basic : Ep1",
args: args{books: []int{1}},
want: 8,
},
{
name: "SimpleDiscounts : two different book",
args: args{books: []int{1, 2}},
want: 2 * 8 * 0.95,
},
{
name: "SimpleDiscounts : three different book",
args: args{books: []int{1, 2, 4}},
want: 3 * 8 * 0.9,
},
{
name: "SimpleDiscounts : five different book",
args: args{books: []int{1, 2, 3, 4, 5}},
want: 5 * 8 * 0.75,
},
{
name: "SeveralDiscounts : 2_EP1&1_EP2",
args: args{books: []int{1, 1, 2}},
want: 8 + (8 * 2 * 0.95),
},
{
name: "SeveralDiscounts : 2_EP1&1_EP2&2_EP3&1_EP4",
args: args{books: []int{1, 1, 2, 3, 3, 4}},
want: (8 * 4 * 0.8) + (8 * 2 * 0.95),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := calculatePrice(tt.args.books); got != tt.want {
t.Errorf("calculatePrice() should got %v, but got %v", tt.want, got)
}
})
}
}
func calculatePrice(books []int) float64 {
epMap := make(map[int]int)
for i := 0; i < len(books); i++ {
epMap[books[i]]++
}
total := float64(0)
mapLen := len(epMap)
for mapLen > 0 {
temp := 0
for key, element := range epMap {
if element > 0 {
temp += 1
temp_element := element - 1
epMap[key] = temp_element
if temp_element == 0 {
mapLen--
}
}
}
total += calculateDiscount(temp)
}
return total
}
func calculateDiscount(len int) float64 {
switch len {
case 1:
return 8
case 2:
return float64(len) * 8 * 0.95
case 3:
return float64(len) * 8 * 0.9
case 4:
return float64(len) * 8 * 0.8
case 5:
return float64(len) * 8 * 0.75
default:
return 0
}
}