Advertisement
yahorrr

Untitled

Oct 3rd, 2022
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Threading.Channels;
  3.  
  4. namespace StringVerification
  5. {
  6.     public static class IsbnVerifier
  7.     {
  8.         /// <summary>
  9.         /// Verifies if the string representation of number is a valid ISBN-10 identification number of book.
  10.         /// </summary>
  11.         /// <param name="number">The string representation of book's number.</param>
  12.         /// <returns>true if number is a valid ISBN-10 identification number of book, false otherwise.</returns>
  13.         /// <exception cref="ArgumentException">Thrown if number is null or empty or whitespace.</exception>
  14.         public static bool IsValid(string number)
  15.         {
  16.             int checkSum = 0;
  17.  
  18.             if (CharToInt(number[0]) == -1 || CharToInt(number[0]) == 10)
  19.             {
  20.                 return false;
  21.             }
  22.  
  23.             checkSum += CharToInt(number[0]);
  24.  
  25.             if (number.Length < 10 || number.Length > 13)
  26.             {
  27.                 return false;
  28.             }
  29.  
  30.             byte countDefis = 0;
  31.  
  32.             if (number[1] == '-')
  33.             {
  34.                 countDefis++;
  35.             }
  36.  
  37.             for (int i = 1 + countDefis; i < 4 + countDefis; i++)
  38.             {
  39.                 if (CharToInt(number[i]) == -1 || CharToInt(number[i]) == 10)
  40.                 {
  41.                     return false;
  42.                 }
  43.  
  44.                 checkSum += CharToInt(number[i]) * (10 - i - countDefis);
  45.             }
  46.  
  47.             if (number[4 + countDefis] == '-')
  48.             {
  49.                 countDefis++;
  50.             }
  51.  
  52.             for (int i = 5 + countDefis; i < number.Length - 2; i++)
  53.             {
  54.                 if (CharToInt(number[i]) == -1 || CharToInt(number[i]) == 10)
  55.                 {
  56.                     return false;
  57.                 }
  58.  
  59.                 checkSum += CharToInt(number[i]) * (10 - i - countDefis);
  60.             }
  61.  
  62.             if (CharToInt(number[^1]) == -1)
  63.             {
  64.                 return false;
  65.             }
  66.  
  67.             checkSum += CharToInt(number[^1]) * 10;
  68.  
  69.             return checkSum % 11 == 0;
  70.         }
  71.  
  72.         private static int CharToInt(char digit) =>
  73.             digit switch
  74.             {
  75.                 '0' => 0,
  76.                 '1' => 1,
  77.                 '2' => 2,
  78.                 '3' => 3,
  79.                 '4' => 4,
  80.                 '5' => 5,
  81.                 '6' => 6,
  82.                 '7' => 7,
  83.                 '8' => 8,
  84.                 '9' => 9,
  85.                 'X' => 10,
  86.                 _ => -1
  87.             };
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement