-
Notifications
You must be signed in to change notification settings - Fork 1
/
fedex.go
157 lines (132 loc) · 3.82 KB
/
fedex.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package kargo
import (
"regexp"
"strconv"
"strings"
)
// FedExGround96 defines a struct for Fedex Ground packages
type FedExGround96 struct {
*Package
}
// FedExExpress defines a struct for Fedex Express packages
type FedExExpress struct {
*Package
}
// NewFedExGround96 initializes a new FedExGround96 struct with package value
func NewFedExGround96(p *Package) *FedExGround96 {
return &FedExGround96{Package: p}
}
// GetCarrierName Implements the CarrierFactory interface method
// Retuns the name of carrier
func (f *FedExGround96) GetCarrierName() string {
return "FedEx"
}
// Match Implements the CarrierFactory interface method
// Retuns the name of carrier struct with package value
func (f *FedExGround96) Match() bool {
if m, _ := regexp.MatchString(`^96\d{20}$`, f.Package.TrackingNumber); m == false {
return false
}
f.Package.Carrier = f.GetCarrierName()
return true
}
// GetPackage Implements the CarrierFactory interface method
// Returns the package that carrier holds
func (f *FedExGround96) GetPackage() *Package {
return f.Package
}
// formatTrackingNumber formats the fedex barcode tracking number to
// normal tracking number
func (f *FedExGround96) formatTrackingNumber() {
f.Package.TrackingNumber = f.Package.TrackingNumber[7:]
}
// Validate Implements the CarrierFactory interface method
// Checks whether is a package belongs to that carrier
func (f *FedExGround96) Validate() bool {
f.formatTrackingNumber()
chars := f.Package.TrackingNumber[:len(f.Package.TrackingNumber)-1]
checkDigit, err := strconv.Atoi(f.Package.TrackingNumber[len(f.Package.TrackingNumber)-1:])
if err != nil {
return false
}
odd, even := 0, 0
reversed := Reverse(chars)
for i, char := range reversed {
t := (string(char))
num, err := strconv.Atoi(t)
if err != nil {
return false
}
if i%2 == 0 {
even += num
continue
}
odd += num
}
check := ((even * 3) + odd) % 10
if check != 0 {
check = 10 - check
}
if check != checkDigit {
return false
}
f.Package.Carrier = f.GetCarrierName()
f.Package.IsValid = true
return true
}
// NewFedExExpress initializes a new FedExExpress struct with package value
func NewFedExExpress(p *Package) *FedExExpress {
return &FedExExpress{Package: p}
}
// GetCarrierName Implements the CarrierFactory interface method
// Retuns the name of carrier
func (f *FedExExpress) GetCarrierName() string {
return "FedEx"
}
// Match Implements the CarrierFactory interface method
// Retuns the name of carrier struct with package value
func (f *FedExExpress) Match() bool {
if m, _ := regexp.MatchString(`^\d{34}$`, f.Package.TrackingNumber); m == false {
return false
}
f.Package.Carrier = f.GetCarrierName()
return true
}
// GetPackage Implements the CarrierFactory interface method
// Returns the package that carrier holds
func (f *FedExExpress) GetPackage() *Package {
return f.Package
}
// formatTrackingNumber formats the fedex barcode tracking number to
// normal tracking number
func (f *FedExExpress) formatTrackingNumber() {
left := strings.TrimLeft(f.Package.TrackingNumber[20:22], "0")
right := f.Package.TrackingNumber[22:]
f.Package.TrackingNumber = left + right
}
// Validate Implements the CarrierFactory interface method
// Checks whether is a package belongs to that carrier
func (f *FedExExpress) Validate() bool {
f.formatTrackingNumber()
chars := f.Package.TrackingNumber[:len(f.Package.TrackingNumber)-1]
checkDigit, err := strconv.Atoi(f.Package.TrackingNumber[len(f.Package.TrackingNumber)-1:])
if err != nil {
return false
}
total := 0
factors := [3]int{1, 3, 7}
reversed := Reverse(chars)
for i, char := range reversed {
num, err := strconv.Atoi(string(char))
if err != nil {
return false
}
total += num * factors[i%3]
}
if total%11%10 != checkDigit {
return false
}
f.Package.Carrier = f.GetCarrierName()
f.Package.IsValid = true
return true
}