-
Notifications
You must be signed in to change notification settings - Fork 7
/
voter.go
53 lines (45 loc) · 1.23 KB
/
voter.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
package brdocs
import (
"fmt"
"github.com/brazanation/go-documents/internal"
"github.com/brazanation/go-documents/internal/calculator"
"strconv"
)
const VoterType internal.DocumentType = "Voter"
const (
voterLength int = 12
voterNumberOfDigits int = 2
voterValidatorRegex string = `^([\d]{12})$`
voterFormatterRegex string = "$1"
)
type voter struct {
}
// NewVoter is the constructor of voter
func NewVoter(number string) (internal.Document, error) {
d, err := internal.NewDocument(
VoterType,
number,
voterLength,
voterNumberOfDigits,
voterFormatterRegex,
voterValidatorRegex,
voter{},
)
return d, err
}
func (v voter) CalculateDigit(baseNumber string) string {
firstDigit := v.calculateFirstDigit(baseNumber[:len(baseNumber)-2])
bn := baseNumber+strconv.Itoa(firstDigit)
secondDigit := v.calculateSecondDigit(bn[len(bn)-3:])
return fmt.Sprintf("%d%d", firstDigit, secondDigit)
}
func (v voter) calculateFirstDigit(baseNumber string) int {
m := calculator.NewModule11(baseNumber)
m.WithMultipliers(9, 8, 7, 6, 5, 4, 3, 2)
return m.Calculate()
}
func (v voter) calculateSecondDigit(baseNumber string) int {
m := calculator.NewModule11(baseNumber)
m.WithMultipliers(9, 8, 7)
return m.Calculate()
}