C# Extension methods – uzupełnienie

Przeglądając dzisiaj internet, natknąłem się na ciekawą stronkę, gdzie znajduje się baza metod rozszerzających. Wszystkie wpisy są tworzone przez użytkowników. Każdy może pobrać gotowe rozwiązania, ocenić i skomentować wpisy oraz podzielić się własnym kodem. Na stronie znajduje się prosta wyszukiwarka a wpisy zostały pogrupowane według typów, które są rozszerzane co bardzo ułatwia przeglądanie. Stronka ta znajduje się pod adresem: http://extensionmethod.net.

Przy okazji poprzedniego wpisu dotyczącego metod rozszerzających, przypomniałem sobie, że swojego czasu napisałem kod, do sprawdzania poprawności numerów NIP, REGON oraz PESEL. Temat oklepany ale myślę, że nie zaszkodzi sobie odświeżyć wykorzystując do celu extension methods. Algorytmy obliczania sum kontrolnych można znaleźć na Wikipedii. Poniżej moje wypociny.

public static class Validators
{
    public static bool IsValidNIP(this string input)
    {
        int[] weights = { 6, 5, 7, 2, 3, 4, 5, 6, 7 };
        bool result = false;
        if (input.Length != 10)
        {
           return result;
        }
        int controlSum = CalculateControlSum(input, weights);
        int controlNum = controlSum % 11;
        if (controlNum == 10)
        {
            return result;
        }
        int lastDigit = int.Parse(input[input.Length – 1].ToString());
        result = controlNum == lastDigit;
        return result;
    }

    public static bool IsValidREGON(this string input)
    {
        int controlSum;
        if (input.Length == 7 || input.Length == 9)
        {
            int[] weights = { 8, 9, 2, 3, 4, 5, 6, 7 };
            int offset = 9 - input.Length;
            controlSum = CalculateControlSum(input, weights, offset);
        }
        else if (input.Length == 14)
        {
            int[] weights = { 2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8 };
            controlSum = CalculateControlSum(input, weights);
        }
        else
        {
            return false;
        }

        int controlNum = controlSum % 11;
        if (controlNum == 10)
        {
            controlNum = 0;
        }
        int lastDigit = int.Parse(input[input.Length - 1].ToString());

        return controlNum == lastDigit;
    }

    public static bool IsValidPESEL(this string input)
    {
        int[] weights = { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
        bool result = false;
        if (input.Length == 11)
        {
            int controlSum = CalculateControlSum(input, weights);
            int controlNum = controlSum % 10;
            controlNum = 10 - controlNum;
            if (controlNum == 10)
            {
                controlNum = 0;
            }
            int lastDigit = int.Parse(input[input.Length - 1].ToString());
            result = controlNum == lastDigit;
        }
        return result;
    }

    private static int CalculateControlSum(string input, int[] weights, int offset = 0)
    {
        int controlSum = 0;
        for (int i = 0; i < input.Length - 1; i++)
        {
            controlSum += weights[i + offset] * int.Parse(input[i].ToString());
        }
        return controlSum;
    }
}

Przykład wywołania:

string nip = "1234567890";
Console.WriteLine(nip.IsValidNIP() ? "NIP prawidłowy" : "NIP błędny");

Tagi:, ,

komentarze 3 do “C# Extension methods – uzupełnienie”

  1. adrole 17 czerwca 2016 w 18:20 #

    Nie działa poprawnie np. dla wartości: 0280040940

    • adrole 17 czerwca 2016 w 18:59 #

      int[] weights = { 6, 5, 7, 2, 3, 4, 5, 6, 7 };
      bool result = false;

      if (input.Length != 10)
      return result;

      int controlSum = CalculateControlSum(input, weights);
      int controlNum = controlSum % 11;
      if (controlNum == 10)
      return result;

      int lastDigit = int.Parse(input[input.Length – 1].ToString());
      result = controlNum == lastDigit;

      return result;

Dodaj komentarz

Uzupełnij * Time limit is exhausted. Please reload the CAPTCHA.