-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookshop.go
96 lines (83 loc) · 2.37 KB
/
bookshop.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
package main
import (
"fmt"
)
/*
One copy of any of the five books costs $8.
If, however, you buy two different books, you get a 5% discount on those two books.
If you buy 3 different books, you get a 10% discount.
If you buy 4 different books, you get a 20% discount.
If you buy all 5, you get a 25% discount.
Note: that if you buy four books, of which 3 are different titles, you get a 10% discount on the 3 that form part of a set, but the fourth book still costs $8.
*/
const singlebook = 8.0
const twobookdisc = 0.95
const threebookdisc = 0.9
const fourbookdisc = 0.8
const fivebookdisc = 0.75
const titles = 5
type bookinvs struct {
bookno int
bookcount int
}
func main() {
var books = []int{1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5}
fmt.Printf("Total cost = £%.2f\n", Cost(books))
}
func Cost(books []int) float64 {
var bookinv [titles]bookinvs
booknobought := 0
booktotbought := 0
totalcost := 0.0
bookmin := 99
for i := 0; i < titles; i++ {
bookinv[i].bookno = i + 1
}
for i := 0; i < len(books); i++ {
for x := 0; x < titles; x++ {
if books[i] == bookinv[x].bookno {
bookinv[x].bookcount++
break
}
}
}
fmt.Printf("books = %v\n", books)
fmt.Printf("bookinv = %v\n", bookinv)
for i := 0; i < len(bookinv); i++ {
if bookinv[i].bookcount > 0 {
booknobought++
booktotbought += bookinv[i].bookcount
}
}
fmt.Printf("Different Books bought %v\n", booknobought)
fmt.Printf("Total Books bought %v\n", booktotbought)
for i := 0; i < len(bookinv); i++ {
if bookinv[i].bookcount > 0 {
if bookinv[i].bookcount < bookmin {
bookmin = bookinv[i].bookcount
}
}
}
switch booknobought {
case 0:
case 1:
totalcost = singlebook * float64(booktotbought)
case 2:
bookmin *= 2
totalcost = float64(singlebook*float64(bookmin)) * twobookdisc
totalcost += float64(singlebook * (float64(booktotbought) - float64(bookmin)))
case 3:
bookmin *= 3
totalcost = float64(singlebook*float64(bookmin)) * threebookdisc
totalcost += float64(singlebook * (float64(booktotbought) - float64(bookmin)))
case 4:
bookmin *= 4
totalcost = float64(singlebook*float64(bookmin)) * fourbookdisc
totalcost += float64(singlebook * (float64(booktotbought) - float64(bookmin)))
case 5:
bookmin *= 5
totalcost = float64(singlebook*float64(bookmin)) * fivebookdisc
totalcost += float64(singlebook * (float64(booktotbought) - float64(bookmin)))
}
return totalcost
}