• 信用卡号 验证


    using System;
    
    namespace CreditCards
    {
        /// <summary>
        /// Summary description for CreditCardValidator.
        /// </summary>
        public class CreditCardValidator
        {
            public CreditCardValidator()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            public bool ValidateCC(string creditNo)
            {
                int indicator = 1;        //-- will be indicator for every other number
                int firstNumToAdd = 0;  //-- will be used to store sum of first set of numbers
                int secondNumToAdd = 0; //-- will be used to store second set of numbers
                string num1;            //-- will be used if every other number added is greater than 10, store the left-most integer here
                string num2;            //-- will be used if ever yother number added is greater than 10, store the right-most integer here
    
                //-- Convert our creditNo string to a char array
                char[] ccArr = creditNo.ToCharArray();
    
                for (int i=ccArr.Length-1;i>=0;i--)
                {
                    char ccNoAdd = ccArr[i];
                    int ccAdd = Int32.Parse(ccNoAdd.ToString());
                    if (indicator == 1)
                    {
                        //-- If we are on the odd number of numbers, add that number to our total
                        firstNumToAdd += ccAdd;
                        //-- set our indicator to 0 so that our code will know to skip to the next piece
                        indicator = 0;
                    }
                    else
                    {
                        //-- if the current integer doubled is greater than 10
                        //-- split the sum in to two integers and add them together
                        //-- we then add it to our total here
                        if ((ccAdd + ccAdd) >= 10)
                        {
                            int temporary = (ccAdd + ccAdd);
                            num1 = temporary.ToString().Substring(0,1);
                            num2 = temporary.ToString().Substring(1,1);
                            secondNumToAdd += (Convert.ToInt32(num1) + Convert.ToInt32(num2));
                        }
                        else
                        {
                            //-- otherwise, just add them together and add them to our total
                            secondNumToAdd += ccAdd + ccAdd;
                        }
                        //-- set our indicator to 1 so for the next integer we will perform a different set of code
                        indicator = 1;
                    }
                }
                //-- If the sum of our 2 numbers is divisible by 10, then the card is valid. Otherwise, it is not
                bool isValid = false;
                if ((firstNumToAdd + secondNumToAdd) % 10 == 0)
                {
                    isValid = true;
                }
                else
                {
                    isValid = false;
                }
                return isValid;
            }
    
            /// <summary>
            /// Get and check to make sure that the chosen credit card type matches the 
            /// card number
            /// </summary>
            /// <param name="CCNum">string</param>
            /// <returns>bool</returns>
            public bool RetrieveCCType(string CCNum, int intChosenType)
            {
                // return value
                bool bResult = false;
    
                if (Convert.ToInt32(CCNum.Substring(0,2)) >= 51 && Convert.ToInt32(CCNum.Substring(0,2)) <= 55)
                {
                    // Check to see that the type of card that the user selected
                    // matches the 
                    if(intChosenType == 3)
                    {
                        bResult = true;
                    }
                    else
                    {
                        bResult = false;
                    }
                }
                else if (Convert.ToString(CCNum.Substring(0,1)) == "4")
                {
                    // Visa
    
                    if(intChosenType == 4)
                    {
                        bResult = true;
                    }
                    else
                    {
                        bResult = false;
                    }
                }
                else if (Convert.ToString(CCNum.Substring(0,2)) == "34" || Convert.ToString(CCNum.Substring(0,2)) == "37")
                {
                    // american express
                    // check to see that the card matches the type they chose
                    if(intChosenType == 1)
                    {
                        bResult = true;
                    }
                    else
                    {
                        bResult = false;
                    }
                }
                else if(Convert.ToString(CCNum.Substring(0,4)) == "6011")
                {
                    // Discover card
                    if(intChosenType == 2)
                    {
                        bResult = true;
                    }
                    else
                    {
                        bResult = false;
                    }
                }
                else
                {
                    bResult = false;
                }
    
                return bResult;
            }
        }
    }
    

      

  • 相关阅读:
    AcWing 276. I-区域
    学习笔记:可持久化线段树(主席树):静态 + 动态
    NOIP2016提高组 天天爱跑步
    AcWing 195. 骑士精神
    标准文档流
    css 盒模型
    css 继承性和层叠性
    css 选择器
    css 引入方式
    html body中的标签2
  • 原文地址:https://www.cnblogs.com/dudg/p/5726155.html
Copyright © 2020-2023  润新知