如何使用单独的类来验证C#中的信用卡号

时间:2010-05-11 21:06:20

标签: c# class constructor enums

我已经设置了一个类来验证信用卡号码。信用卡类型和编号在单独的类中的表单上选择。我正在试图找出如何将我在其他类(frmPayment)中选择的信用卡类型和号码输入到我的信用卡类算法中:

public enum CardType
{
    MasterCard, Visa, AmericanExpress
}

public sealed class CardValidator
{
    public static string SelectedCardType { get; private set; }
    public static string CardNumber { get; private set; }

    private CardValidator(string selectedCardType, string cardNumber) 
    {
        SelectedCardType = selectedCardType;
        CardNumber = cardNumber;
    }

    public static bool Validate(CardType cardType, string cardNumber)
{
   byte[] number = new byte[16];


  int length = 0;
  for (int i = 0; i < cardNumber.Length; i++)
  {
      if (char.IsDigit(cardNumber, i))
      {
          if (length == 16) return false;
          number[length++] = byte.Parse(cardNumber[i]); //not working.  find different way to parse
      }
  }

  switch(cardType)
  {
     case CardType.MasterCard:
        if(length != 16)
           return false;
        if(number[0] != 5 || number[1] == 0 || number[1] > 5)
           return false;
        break;

     case CardType.Visa:
        if(length != 16 & length != 13)
           return false;
        if(number[0] != 4)
           return false;
        break;

     case CardType.AmericanExpress:
        if(length != 15)
           return false;
        if(number[0] != 3 || (number[1] != 4 & number[1] != 7))
           return false;
        break;

  }

  // Use Luhn Algorithm to validate
  int sum = 0;
  for(int i = length - 1; i >= 0; i--)
  {
     if(i % 2 == length % 2)
     {
        int n = number[i] * 2;
        sum += (n / 10) + (n % 10);
     }
     else
        sum += number[i];
  }
  return (sum % 10 == 0);

}     }

3 个答案:

答案 0 :(得分:5)

您错过了获得更好的OOP和更清晰代码的绝佳机会。

class CreditCard
{
   public CreditCard(string number, string expiration, string cvv2) {...}

   public virtual bool IsValid()
   {
       /* put common validation logic here */
   }

   /* factory for actual cards */
   public static CreditCard GetCardByType (CardType card, string number, string expiration, string cvv2)
   {
        switch (card)
        {
             case  CardType.Visa:
                  return new VisaCreditCard(...);

             ...
        }
   }
}

class VisaCreditCard : CreditCard
{
   public VisaCreditCard (string number, string expiration, string cvv2 )
      : base (number, expiration, cvv2)
   {...}

   public override bool IsValid()
   {
       /* check Visa rules... */
       bool isValid = ...

       return isValid & base.IsValid();
   }
}

答案 1 :(得分:2)

对于初学者,我会使用Regex进行简单验证(所有数字,特定长度。)

但就你的问题而言,我不确定我是否明白这是什么问题。根据你在这里发布的内容,看起来你应该删除构造函数并使整个东西成为静态类。

public enum CardType
{
    MasterCard,
    Visa,
    AmericanExpress,
}

public static class CardValidator
{
    public static bool Validate(CardType cardType, string cardNumber)
    {
        string strippedCardNumber = Regex.Replace(cardNumber, @"\D", String.Empty);


        ICardValidator validator = SelectCardValidator(cardType);

        return validator.Validate(strippedCardNumber);
    }

    private static ICardValidator SelectCardValidator(CardType cardType)
    {
        switch (cardType)
        {
            case CardType.MasterCard:
                return new MasterCardValidator();
            case CardType.Visa:
                return new VisaValidator();
            case CardType.AmericanExpress:
                return new AmericanExpressValidator();
            default:
                return new UnknownCardTypeValidator();
        }
    }

    private interface ICardValidator
    {
        bool Validate(string cardNumber);
    }

    private class UnknownCardTypeValidator : ICardValidator
    {
        #region ICardValidator Members

        public bool Validate(string cardNumber)
        {
            return false;
        }

        #endregion
    }

    private abstract class LuhnAlgorithmValidator : ICardValidator
    {

        #region ICardValidator Members

        public virtual bool Validate(string cardNumber)
        {
            // Implement Luhn Algorithm here

            return false;
        }

        #endregion
    }

    private class MasterCardValidator : LuhnAlgorithmValidator
    {
        public override bool Validate(string cardNumber)
        {
            bool isValid = false; // replace with MasterCard validation
            return isValid && base.Validate(cardNumber);
        }
    }

    private class VisaValidator : LuhnAlgorithmValidator
    {
        public override bool Validate(string cardNumber)
        {
            bool isValid = false; // replace with Visa validation
            return isValid && base.Validate(cardNumber);
        }
    }

    private class AmericanExpressValidator : LuhnAlgorithmValidator
    {
        public override bool Validate(string cardNumber)
        {
            bool isValid = false; // replace with AmEx validation
            return isValid && base.Validate(cardNumber);
        }
    }
}

答案 2 :(得分:1)

frmPayments btnValidate_click(或类似)事件处理程序中,只需调用CardValidator.Validate方法。

但是,CardValidator构造函数具有参数,即使它是私有的,也从未调用过?为什么要在课程中添加属性?

编辑:错过了一些示例代码。