コード例 #1
0
        //TODO: Get rid of the exceptions used for logic and add tryparse instead
        public static BankAccountSe CreateBankAccount(string accountNumber)
        {
            var cleaned = AccountNumberValidator.Clean(accountNumber) ?? "";

            if (cleaned.Length < 6)
            {
                throw new ArgumentException("An account number is at least 6 digits");
            }
            BankAccountSe     a4          = null;
            ArgumentException a4Exception = null;

            try
            {
                a4 = CreateBankAccount(cleaned.Substring(0, 4), cleaned.Substring(4));
            }
            catch (ArgumentException ex)
            {
                a4Exception = ex;
            }
            BankAccountSe     a5          = null;
            ArgumentException a5Exception = null;

            try
            {
                //Assuming clearingnumber of length 5
                a5 = CreateBankAccount(cleaned.Substring(0, 5), cleaned.Substring(5));
                if (a5.Bank != ClearingNumberRange.SwebankName)
                {
                    throw new ArgumentException("5 digits clearing number are only allowed for Swedbank.");
                }
            }
            catch (ArgumentException ex)
            {
                a5Exception = ex;
            }

            //This should be impossible. Both length 4 and length 5 seem valid. This is an error
            if (a4Exception == null && a5Exception == null)
            {
                if (a4.Bank == ClearingNumberRange.SwebankName)
                {
                    //Both are valid and we have swedbank. We want a5 in this case
                    return(a5);
                }
                else
                {
                    throw new Exception("Bank account '" + accountNumber + "' seems to be valid both with 4 and 5 clearing digits. This is an error in the parsing logic.");
                }
            }

            //This is not a valid account number by either method. We pick one of the exceptions at random (may be improved by checking length or similar)
            if (a4Exception != null && a5Exception != null)
            {
                throw a4Exception;
            }

            return(a4Exception != null ? a5 : a4);
        }
コード例 #2
0
 public static bool TryParseBankAccount(string source, out BankAccountSe bankAccount, out string errMsg)
 {
     try
     {
         bankAccount = CreateBankAccount(source);
         errMsg      = null;
         return(true);
     }
     catch (ArgumentException ex)
     {
         errMsg      = ex.Message;
         bankAccount = null;
         return(false);
     }
 }
コード例 #3
0
        /// <summary>
        /// Creates a BankAccount object. Throws an argument exception if the clearing number or account number is malformatted.
        /// </summary>
        /// <param name="clearingNumber">The clearing number part of the bank account.</param>
        /// <param name="accountNumber">The account number part of the bank account.</param>
        /// <returns>A BankAccount object.</returns>
        public static BankAccountSe CreateBankAccount(string clearingNumber, string accountNumber)
        {
            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.");
            }

            BankAccountSe bankAccount = new BankAccountSe();

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

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

            // Assign account type and bank
            var bankAndAccountNumberType = ClearingNumberRange.GetBankAndAccountNumberType(clearingNumber);

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

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

            if (bankAccount.AccountNumberType == AccountNumberType.Type5 && bankAccount.Bank == ClearingNumberRange.SwebankName && bankAccount.ClearingNumber.Length == 5)
            {
                bankAccount.ClearingNumber = bankAccount.ClearingNumber.Substring(0, 4);
            }

            return(bankAccount);
        }