Advertisement
yahorrr

Untitled

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