public static BankAccountNo CreateBankAccount(string accountNumber, IEnumerable <string> bankRegisterLines)
        {
            var cleaned = AccountNumberValidator.Clean(accountNumber) ?? "";

            if (cleaned.Length != 11)
            {
                throw new ArgumentException("A complete account number is 11 digits included clearing and check number.");
            }

            BankAccountNo     norwegianAccount          = null;
            ArgumentException norwegianAccountException = null;

            try
            {
                norwegianAccount = CreateBankAccount(cleaned.Substring(0, 4), cleaned.Substring(4), bankRegisterLines);
            }
            catch (ArgumentException ex)
            {
                norwegianAccountException = ex;
            }

            if (norwegianAccountException != null)
            {
                throw norwegianAccountException;
            }

            return(norwegianAccount);
        }
 public static bool TryParseBankAccount(string source, out BankAccountNo bankAccount, out string errMsg, IEnumerable <string> bankRegisterLines)
 {
     try
     {
         bankAccount = CreateBankAccount(source, bankRegisterLines);
         errMsg      = null;
         return(true);
     }
     catch (ArgumentException ex)
     {
         errMsg      = ex.Message;
         bankAccount = null;
         return(false);
     }
 }
        public static BankAccountNo CreateBankAccount(string clearingNumber, string accountNumber, IEnumerable <string> bankRegisterLines)
        {
            if (string.IsNullOrEmpty(clearingNumber))
            {
                throw new ArgumentException("clearingNumber must not be null or empty string.");
            }
            if (string.IsNullOrEmpty(accountNumber))
            {
                throw new ArgumentException("accountNumber must not be null or empty string.");
            }

            BankAccountNo bankAccount = new BankAccountNo();

            // Remove any redundant characters
            clearingNumber = AccountNumberValidator.Clean(clearingNumber);
            accountNumber  = AccountNumberValidator.Clean(accountNumber);

            // Assign clearing number
            AccountNumberValidator.CheckClearingNumber(clearingNumber);

            // Assign account type and bank
            var bankAndAccountNumberType = ClearingNumberData.GetBankAndAccountNumberType(clearingNumber, bankRegisterLines);

            if ((bankAccount.AccountNumberType = bankAndAccountNumberType.Item2) == AccountNumberType.Unknown)
            {
                throw new ArgumentException("Unknown clearingNumber. Could not match clearing number to a known bank.");
            }
            bankAccount.Bank = bankAndAccountNumberType.Item1;
            bankAccount.AccountNumberType = bankAndAccountNumberType.Item2;

            // Assign account number
            AccountNumberValidator.CheckAccountNumber(ref clearingNumber, ref accountNumber, bankAndAccountNumberType.Item2);
            bankAccount.AccountNumber  = accountNumber;
            bankAccount.ClearingNumber = clearingNumber;

            return(bankAccount);
        }