-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgross_store.go
79 lines (60 loc) · 1.37 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
package gross
// Units stores the Gross Store unit measurements.
func Units() map[string]int {
units := make(map[string]int)
units["quarter_of_a_dozen"] = 3
units["half_of_a_dozen"] = 6
units["dozen"] = 12
units["small_gross"] = 120
units["gross"] = 144
units["great_gross"] = 1728
return units
}
// NewBill creates a new bill.
func NewBill() map[string]int {
return make(map[string]int)
}
// AddItem adds an item to customer bill.
func AddItem(bill, units map[string]int, item, unit string) bool {
_, itemExists := bill[item]
_, unitExists := units[unit]
if !unitExists {
return false
}
if !itemExists {
bill[item] = units[unit]
return true
} else {
bill[item] += units[unit]
return true
}
}
// RemoveItem removes an item from customer bill.
func RemoveItem(bill, units map[string]int, item, unit string) bool {
_, itemExists := bill[item]
_, unitExists := units[unit]
if !itemExists {
return false
}
if !unitExists {
return false
}
tmp := bill[item] - units[unit]
if tmp < 0 {
return false
} else if tmp == 0 {
delete(bill, item)
return true
} else {
bill[item] -= units[unit]
return true
}
}
// 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) {
_, itemExists := bill[item]
if !itemExists {
return 0, false
}
return bill[item], true
}