// Get weights to use for a bank account following the exception 2 or 9. func WeightsForException2Or9(b m.BankAccount, sc m.SortCodeData) (weights []int) { if !(sc.IsException(2) || sc.IsException(9)) { panic("Expected exception 2 or exception 9 sort code") } a := b.NumberAtPosition("a") g := b.NumberAtPosition("g") // Default weights weights = sc.Weights switch { case a != 0 && g != 9: weights = []int{ 0, 0, 1, 2, 5, 3, 6, 4, 8, 7, 10, 9, 3, 1, } case a != 0 && g == 9: weights = []int{ 0, 0, 0, 0, 0, 0, 0, 0, 8, 7, 10, 9, 3, 1, } } return }
// Check if we follow the criteria of the exception 3 func FollowsException3(b m.BankAccount, scData m.SortCodeData) bool { c := b.NumberAtPosition("c") hasNextAnd3 := scData.HasNext() && scData.Next.IsException(3) return (scData.IsException(3) || hasNextAnd3) && (c == 6 || c == 9) }
// Determine if the checker is able to validate the bank account func (e Exception10Checker) Handles(account m.BankAccount, sc m.SortCodeData, attempt int) bool { if !sc.IsException(10) { return false } // if ab = 09 or ab = 99 and g = 9 a := account.NumberAtPosition("a") b := account.NumberAtPosition("b") g := account.NumberAtPosition("g") return (a == 0 || a == 9) && b == 9 && g == 9 }
// Perform the check for a bank account and a given attempt. func (r Resolver) checkForSortCodeData(b m.BankAccount, scData m.SortCodeData, attempt int) bool { // If the sort code follows an exception if scData.HasException() { // Try to find a checker for this specific exception if checkerFound, hasKey := r.exceptionCheckers[scData.ExceptionValue]; hasKey { // Check that this checker can actually handle this case if checkerFound.Handles(b, scData, attempt) { // Validate the bank account number return checkerFound.IsValid(b, scData, attempt) } } } // General case return checkers.GeneralChecker{}.IsValid(b, scData, attempt) }
// Tell if the bank account is valid func (e Exception2Checker) IsValid(b m.BankAccount, sc m.SortCodeData, attempt int) bool { if !e.Handles(b, sc, attempt) { panic("Should be exception of type 2") } sc.Weights = WeightsForException2Or9(b, sc) return GeneralChecker{}.IsValid(b, sc, attempt) }
// Tell if the bank account is valid func (e Exception10Checker) IsValid(b m.BankAccount, sc m.SortCodeData, attempt int) bool { if !e.Handles(b, sc, attempt) { panic("Should be exception of type 10") } // Put 8 zeros at the beginning of the weights // and keep the original weights after zeros := []int{0, 0, 0, 0, 0, 0, 0, 0} sc.Weights = append(zeros, sc.Weights[8:]...) return GeneralChecker{}.IsValid(b, sc, attempt) }
// Tell if the bank account is valid func (e Exception9Checker) IsValid(b m.BankAccount, sc m.SortCodeData, attempt int) bool { if !e.Handles(b, sc, attempt) { panic("Should be exception of type 9") } sc.Weights = WeightsForException2Or9(b, sc) if (GeneralChecker{}.IsValid(b, sc, attempt)) { return true } // Try to replace the sort code b.SortCode = "309634" return GeneralChecker{}.IsValid(b, e.Weights[b.SortCode], attempt) }
// Determine if the checker is able to validate the bank account func (e Exception6Checker) Handles(b m.BankAccount, sc m.SortCodeData, attempt int) bool { return sc.IsException(6) }
// Determine if the checker is able to validate the bank account func (e Exception7Checker) Handles(account m.BankAccount, sc m.SortCodeData, attempt int) bool { // If g=9 g := account.NumberAtPosition("g") return sc.IsException(7) && g == 9 }