Skip to content

Instantly share code, notes, and snippets.

@acompa
Last active September 7, 2019 03:02
Show Gist options
  • Save acompa/ce587ed17bc654bec05cfaaf09ce9798 to your computer and use it in GitHub Desktop.
Save acompa/ce587ed17bc654bec05cfaaf09ce9798 to your computer and use it in GitHub Desktop.
package ccvalidation
import (
"fmt"
"strings"
)
type Card interface {
Validate(string ccNumber) bool
}
type amex struct {
name string
}
func (a *amex) Validate(string ccNumber) bool {
return (strings.HasPrefix(ccNumber, "34") || strings.HasPrefix(ccNumber, "37")) && len(ccNumber) == 15)
}
func NewAmex() amex {
return &amex{name: "American Express"}
}
type visa struct {
name string
}
func (v *visa) Validate(string ccNumber) bool {
return (strings.HasPrefix(ccNumber, "4") && len(ccNumber) == 16)
}
func NewVisa() visa {
return &visa{name: "Visa"}
}
type visaElectron struct {
name string
}
func (ve *visaElectron) Validate(string ccNumber) bool {
validPrefix := (strings.HasPrefix(ccNumber, "42") || strings.HasPrefix(ccNumber, "48017"))
validLength := (len(ccNumber) == 14 || len(ccNumber) == 16)
return validPrefix && validLength
}
func NewVisaElectron() visaElectron {
return &visaElectron{name: "Visa Electron"}
}
func main() {
// Make sure we got a number
_, err := strconv.ParseInt(ccNumber)
if (err != nil) {
return false
}
// TODO (acompa): Trie goes here.
// var validationMap map[string]Card = &map[string]Card{
// "42": NewVisaElectron(),
// "48017": NewVisaElectron(),
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment