-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
55 lines (44 loc) · 1.17 KB
/
generator.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
package drivinglicence
import (
"errors"
"fmt"
)
//go:generate counterfeiter -o mock . Applicant
type Applicant interface {
IsOver17() bool
HoldsLicence() bool
GetInitials() string
GetDOB() string
}
//go:generate counterfeiter -o mock . Logger
type Logger interface {
LogStuff(v string)
}
//go:generate counterfeiter -o mock . RandomNumbersGenerator
type RandomNumbersGenerator interface {
GetRandomNumbers(len int) string
}
type NumberGenerator struct {
l Logger
r RandomNumbersGenerator
}
func (g NumberGenerator) Generate(a Applicant) (string, error) {
if a.HoldsLicence() {
g.l.LogStuff("Duplicate Applicant, you can only hold one licence")
return "", errors.New("Duplicate Applicant, you can only hold one licence")
}
if !a.IsOver17() {
g.l.LogStuff("Underaged Applicant, you must be 17 for applicant licence")
return "", errors.New("Underaged Applicant, you must be 17 for applicant licence")
}
n := fmt.Sprintf(
"%s%s",
a.GetInitials(),
a.GetDOB(),
)
num := 16 - len(n)
return fmt.Sprintf("%s%s", n, g.r.GetRandomNumbers(num)), nil
}
func NewNumberGenerator(l Logger, r RandomNumbersGenerator) NumberGenerator {
return NumberGenerator{l, r}
}