Advertisement
yahorrr

Untitled

Oct 11th, 2022
741
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.47 KB | None | 0 0
  1. using System.Globalization;
  2.  
  3. namespace BookStoreItem
  4. {
  5.     /// <summary>
  6.     /// Represents the an item in a book store.
  7.     /// </summary>
  8.     public class BookStoreItem
  9.     {
  10.         private readonly string? authorName;
  11.         private readonly string? isni;
  12.         private readonly bool hasIsni;
  13.         private decimal price;
  14.         private string currency;
  15.         private int amount;
  16.  
  17.         /// <summary>
  18.         /// Initializes a new instance of the <see cref="BookStoreItem"/> class with the specified <paramref name="authorName"/>, <paramref name="title"/>, <paramref name="publisher"/> and <paramref name="isbn"/>.
  19.         /// </summary>
  20.         /// <param name="authorName">A book author's name.</param>
  21.         /// <param name="title">A book title.</param>
  22.         /// <param name="publisher">A book publisher.</param>
  23.         /// <param name="isbn">A book ISBN.</param>
  24.         public BookStoreItem(string authorName, string title, string publisher, string isbn)
  25.             : this(authorName, string.Empty, title, publisher, isbn)
  26.         {
  27.         }
  28.  
  29.         /// <summary>
  30.         /// Initializes a new instance of the <see cref="BookStoreItem"/> class with the specified <paramref name="authorName"/>, <paramref name="isni"/>, <paramref name="title"/>, <paramref name="publisher"/> and <paramref name="isbn"/>.
  31.         /// </summary>
  32.         /// <param name="authorName">A book author's name.</param>
  33.         /// <param name="isni">A book author's ISNI.</param>
  34.         /// <param name="title">A book title.</param>
  35.         /// <param name="publisher">A book publisher.</param>
  36.         /// <param name="isbn">A book ISBN.</param>
  37.         public BookStoreItem(string authorName, string? isni, string title, string publisher, string isbn)
  38.             : this(authorName, isni, title, publisher, isbn, DateTime.MinValue, string.Empty, 0, "USD", 0)
  39.         {
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Initializes a new instance of the <see cref="BookStoreItem"/> class with the specified <paramref name="authorName"/>, <paramref name="title"/>, <paramref name="publisher"/> and <paramref name="isbn"/>, <paramref name="published"/>, <paramref name="bookBinding"/>, <paramref name="price"/>, <paramref name="currency"/> and <paramref name="amount"/>.
  44.         /// </summary>
  45.         /// <param name="authorName">A book author's name.</param>
  46.         /// <param name="title">A book title.</param>
  47.         /// <param name="publisher">A book publisher.</param>
  48.         /// <param name="isbn">A book ISBN.</param>
  49.         /// <param name="published">A book publishing date.</param>
  50.         /// <param name="bookBinding">A book binding type.</param>
  51.         /// <param name="price">An amount of money that a book costs.</param>
  52.         /// <param name="currency">A price currency.</param>
  53.         /// <param name="amount">An amount of books in the store's stock.</param>
  54.         public BookStoreItem(string authorName, string title, string publisher, string isbn, DateTime? published, string bookBinding, decimal price, string currency, int amount)
  55.             : this(authorName, string.Empty, title, publisher, isbn, published, bookBinding, price, currency, amount)
  56.         {
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Initializes a new instance of the <see cref="BookStoreItem"/> class with the specified <paramref name="authorName"/>, <paramref name="isni"/>, <paramref name="title"/>, <paramref name="publisher"/> and <paramref name="isbn"/>, <paramref name="published"/>, <paramref name="bookBinding"/>, <paramref name="price"/>, <paramref name="currency"/> and <paramref name="amount"/>.
  61.         /// </summary>
  62.         /// <param name="authorName">A book author's name.</param>
  63.         /// <param name="isni">A book author's ISNI.</param>
  64.         /// <param name="title">A book title.</param>
  65.         /// <param name="publisher">A book publisher.</param>
  66.         /// <param name="isbn">A book ISBN.</param>
  67.         /// <param name="published">A book publishing date.</param>
  68.         /// <param name="bookBinding">A book binding type.</param>
  69.         /// <param name="price">An amount of money that a book costs.</param>
  70.         /// <param name="currency">A price currency.</param>
  71.         /// <param name="amount">An amount of books in the store's stock.</param>
  72.         public BookStoreItem(string authorName, string? isni, string title, string publisher, string isbn, DateTime? published, string bookBinding, decimal price, string currency, int amount)
  73.         {
  74.             this.authorName = authorName;
  75.             this.isni = isni;
  76.             this.currency = currency;
  77.             this.Title = title;
  78.             this.Publisher = publisher;
  79.             this.Isbn = isbn;
  80.             this.Published = published;
  81.             this.BookBinding = bookBinding;
  82.             this.Price = price;
  83.             this.Amount = amount;
  84.             this.hasIsni = isni != null;
  85.         }
  86.  
  87.         /// <summary>
  88.         /// Gets a book author's name.
  89.         /// </summary>
  90.         public string? AuthorName => this.authorName;
  91.  
  92.         /// <summary>
  93.         /// Gets an International Standard Name Identifier (ISNI) that uniquely identifies a book author.
  94.         /// </summary>
  95.         public string? Isni => this.isni;
  96.  
  97.         /// <summary>
  98.         /// Gets a value indicating whether an author has an International Standard Name Identifier (ISNI).
  99.         /// </summary>
  100.         public bool HasIsni => this.hasIsni;
  101.  
  102.         /// <summary>
  103.         /// Gets a book title.
  104.         /// </summary>
  105.         public string? Title { get; private set; }
  106.  
  107.         /// <summary>
  108.         /// Gets a book publisher.
  109.         /// </summary>
  110.         public string? Publisher { get; private set; }
  111.  
  112.         /// <summary>
  113.         /// Gets a book International Standard Book Number (ISBN).
  114.         /// </summary>
  115.         public string? Isbn { get; private set; }
  116.  
  117.         /// <summary>
  118.         /// Gets a book publishing date.
  119.         /// </summary>
  120.         public DateTime? Published { get; private set; }
  121.  
  122.         /// <summary>
  123.         /// Gets a book binding type.
  124.         /// </summary>
  125.         public string? BookBinding { get; private set; }
  126.  
  127.         /// <summary>
  128.         /// Gets an amount of money that a book costs.
  129.         /// </summary>
  130.         public decimal Price
  131.         {
  132.             get => this.price;
  133.  
  134.             set => this.price = value;
  135.         }
  136.  
  137.         /// <summary>
  138.         /// Gets or sets a price currency.
  139.         /// </summary>
  140.         public string Currency
  141.         {
  142.             get => this.currency;
  143.  
  144.             set => this.currency = value;
  145.         }
  146.  
  147.         /// <summary>
  148.         /// Gets or sets an amount of books in the store's stock.
  149.         /// </summary>
  150.         public int Amount
  151.         {
  152.             get => this.amount;
  153.  
  154.             set => this.amount = value;
  155.         }
  156.  
  157.         /// <summary>
  158.         /// Gets a <see cref="Uri"/> to the contributor's page at the isni.org website.
  159.         /// </summary>
  160.         /// <returns>A <see cref="Uri"/> to the contributor's page at the isni.org website.</returns>
  161.         public Uri GetIsniUri()
  162.         {
  163.             if (!this.HasIsni)
  164.             {
  165.                 throw new InvalidOperationException();
  166.             }
  167.  
  168.             return new Uri($"https://isni.org/isni/{this.isni}");
  169.         }
  170.  
  171.         /// <summary>
  172.         /// Gets an <see cref="Uri"/> to the publication page on the isbnsearch.org website.
  173.         /// </summary>
  174.         /// <returns>an <see cref="Uri"/> to the publication page on the isbnsearch.org website.</returns>
  175.         public Uri GetIsbnSearchUri() => new Uri($"https://isbnsearch.org/isbn/{this.Isbn}");
  176.  
  177.         /// <summary>
  178.         /// Returns the string that represents a current object.
  179.         /// </summary>
  180.         /// <returns>A string that represents the current object.</returns>
  181.         public override string ToString()
  182.         {
  183.             string formatPrice = this.price.ToString(CultureInfo.InvariantCulture).Contains(',', StringComparison.InvariantCulture) ? $"\"{this.price}\"" : $"{this.price}";
  184.  
  185.             if (!this.HasIsni)
  186.             {
  187.                 return $"{this.Title}, {this.authorName}, ISNI IS NOT SET, {formatPrice}, {this.currency}, {this.amount}";
  188.             }
  189.  
  190.             return $"{this.Title}, {this.authorName}, {this.isni}, {formatPrice}, {this.currency}, {this.amount}";
  191.         }
  192.  
  193.         private static bool ValidateIsni(string? isni) => ValidateIsbn(isni);
  194.  
  195.         private static bool ValidateIsbn(string? isbn)
  196.         {
  197.             for (int i = 0; i < isbn.Length; i++)
  198.             {
  199.                 if (CharToInt(isbn[i]) == -1)
  200.                 {
  201.                     return false;
  202.                 }
  203.             }
  204.  
  205.             return true;
  206.         }
  207.  
  208.         private static bool ValidateIsbnChecksum(string? isbn)
  209.         {
  210.             int checkSum = 0;
  211.             for (int i = 0; i < isbn.Length; i++)
  212.             {
  213.                 checkSum += CharToInt(isbn[i]);
  214.             }
  215.  
  216.             return checkSum % 11 == 0;
  217.         }
  218.  
  219.         // TODO Add a static method.
  220.  
  221.         private static int CharToInt(char digit) =>
  222.             digit switch
  223.             {
  224.                 '0' => 0,
  225.                 '1' => 1,
  226.                 '2' => 2,
  227.                 '3' => 3,
  228.                 '4' => 4,
  229.                 '5' => 5,
  230.                 '6' => 6,
  231.                 '7' => 7,
  232.                 '8' => 8,
  233.                 '9' => 9,
  234.                 'X' => 10,
  235.                 _ => -1
  236.             };
  237.     }
  238. }
  239.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement