-
Notifications
You must be signed in to change notification settings - Fork 0
/
cost.go
47 lines (37 loc) · 863 Bytes
/
cost.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
package tisp
import (
"errors"
"strings"
)
type Cost struct {
Units string
Value float64
}
func (c *Cost) IsRatio() bool {
return strings.Contains(c.Units, "/")
}
func (c *Cost) Upper() (string, error) {
if !c.IsRatio() {
return "", errors.New("the unit isn't a ratio, it's fixed")
}
chunks := strings.Split(c.Units, "/")
return chunks[0], nil
}
func (c *Cost) Lower() (string, error) {
if !c.IsRatio() {
return "", errors.New("the unit isn't a ratio, it's fixed")
}
chunks := strings.Split(c.Units, "/")
return chunks[1], nil
}
func (c *Cost) By() (string, error) {
if !c.IsRatio() {
return "", errors.New("the unit isn't a ratio, it's fixed")
}
chunks := strings.Split(c.Units, "/")
return chunks[1], nil
}
func (c *Cost) IsValid() bool {
// TODO: Add standard units validation ($, h, PEN, USD, etc...)
return c.Units != ""
}