-
Notifications
You must be signed in to change notification settings - Fork 0
/
gross_store.go
89 lines (70 loc) · 1.61 KB
/
gross_store.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
package gross
// Units stores the Gross Store unit measurements.
func Units() map[string]int {
units := map[string]int{
"quarter_of_a_dozen": 3,
"half_of_a_dozen": 6,
"dozen": 12,
"small_gross": 120,
"gross": 144,
"great_gross": 1728,
}
return units
}
// NewBill creates a new bill.
func NewBill() map[string]int {
bill := map[string]int{}
return bill
}
// inMap returns true if key is in map.
func inMap(dict map[string]int, key string) bool {
_, found := dict[key]
return found
}
// AddItem adds an item to customer bill.
func AddItem(bill, units map[string]int, item, unit string) bool {
var found bool = inMap(bill, item)
var valid bool = inMap(units, unit)
var result bool
if valid {
result = true
if found {
bill[item] += units[unit]
} else {
bill[item] = units[unit]
}
} else {
result = false
}
return result
}
// RemoveItem removes an item from customer bill.
func RemoveItem(bill, units map[string]int, item, unit string) bool {
var result bool
if inMap(bill, item) && inMap(units, unit) {
result = true
newQuantity := bill[item] - units[unit]
if newQuantity < 0 {
result = false
} else if newQuantity > 0 {
bill[item] -= units[unit]
} else {
delete(bill, item)
}
} else {
result = false
}
return result
}
// GetItem returns the quantity of an item that the customer has in his/her bill.
func GetItem(bill map[string]int, item string) (int, bool) {
var result bool
var quantity int
if inMap(bill, item) {
result = true
quantity = bill[item]
} else {
result = false
}
return quantity, result
}