yahorrr

Untitled

Oct 11th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.81 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, null, 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.             if (!ValidateIsni(this.isni))
  75.             {
  76.                 throw new ArgumentException(" ", nameof(isni));
  77.             }
  78.  
  79.             if (!ValidateIsbn(this.Isbn) && !ValidateIsbnChecksum(this.Isbn))
  80.             {
  81.                 throw new ArgumentException(" ", nameof(isbn));
  82.             }
  83.  
  84.             this.authorName = authorName;
  85.             this.isni = isni;
  86.             this.currency = currency;
  87.             this.Title = title;
  88.             this.Publisher = publisher;
  89.             this.Isbn = isbn;
  90.             this.Published = published;
  91.             this.BookBinding = bookBinding;
  92.             this.Price = price;
  93.             this.Amount = amount;
  94.             this.hasIsni = isni != null;
  95.         }
  96.  
  97.         /// <summary>
  98.         /// Gets a book author's name.
  99.         /// </summary>
  100.         public string? AuthorName => this.authorName;
  101.  
  102.         /// <summary>
  103.         /// Gets an International Standard Name Identifier (ISNI) that uniquely identifies a book author.
  104.         /// </summary>
  105.         public string? Isni => this.isni;
  106.  
  107.         /// <summary>
  108.         /// Gets a value indicating whether an author has an International Standard Name Identifier (ISNI).
  109.         /// </summary>
  110.         public bool HasIsni => this.hasIsni;
  111.  
  112.         /// <summary>
  113.         /// Gets a book title.
  114.         /// </summary>
  115.         public string Title { get; private set; }
  116.  
  117.         /// <summary>
  118.         /// Gets a book publisher.
  119.         /// </summary>
  120.         public string Publisher { get; private set; }
  121.  
  122.         /// <summary>
  123.         /// Gets a book International Standard Book Number (ISBN).
  124.         /// </summary>
  125.         public string Isbn { get; private set; }
  126.  
  127.         /// <summary>
  128.         /// Gets a book publishing date.
  129.         /// </summary>
  130.         public DateTime? Published { get; private set; }
  131.  
  132.         /// <summary>
  133.         /// Gets a book binding type.
  134.         /// </summary>
  135.         public string BookBinding { get; private set; }
  136.  
  137.         /// <summary>
  138.         /// Gets an amount of money that a book costs.
  139.         /// </summary>
  140.         public decimal Price
  141.         {
  142.             get => this.price;
  143.  
  144.             set
  145.             {
  146.                 if (this.price < 0)
  147.                 {
  148.                     throw new ArgumentOutOfRangeException(nameof(this.Price), "Price is less then zero.");
  149.                 }
  150.  
  151.                 this.price = value;
  152.             }
  153.         }
  154.  
  155.         /// <summary>
  156.         /// Gets or sets a price currency.
  157.         /// </summary>
  158.         public string Currency
  159.         {
  160.             get => this.currency;
  161.  
  162.             set
  163.             {
  164.                 //if (this.currency.Length != 3 || this.currency)
  165.                 //{
  166.                 //    throw new ArgumentOutOfRangeException(nameof(this.Price), "Price is less then zero.");
  167.                 //}
  168.  
  169.                 this.currency = value;
  170.             }
  171.         }
  172.  
  173.         /// <summary>
  174.         /// Gets or sets an amount of books in the store's stock.
  175.         /// </summary>
  176.         public int Amount
  177.         {
  178.             get => this.amount;
  179.  
  180.             set => this.amount = value;
  181.         }
  182.  
  183.         /// <summary>
  184.         /// Gets a <see cref="Uri"/> to the contributor's page at the isni.org website.
  185.         /// </summary>
  186.         /// <returns>A <see cref="Uri"/> to the contributor's page at the isni.org website.</returns>
  187.         public Uri GetIsniUri()
  188.         {
  189.             if (!this.HasIsni)
  190.             {
  191.                 throw new InvalidOperationException();
  192.             }
  193.  
  194.             return new Uri($"https://isni.org/isni/{this.isni}");
  195.         }
  196.  
  197.         /// <summary>
  198.         /// Gets an <see cref="Uri"/> to the publication page on the isbnsearch.org website.
  199.         /// </summary>
  200.         /// <returns>an <see cref="Uri"/> to the publication page on the isbnsearch.org website.</returns>
  201.         public Uri GetIsbnSearchUri() => new Uri($"https://isbnsearch.org/isbn/{this.Isbn}");
  202.  
  203.         /// <summary>
  204.         /// Returns the string that represents a current object.
  205.         /// </summary>
  206.         /// <returns>A string that represents the current object.</returns>
  207.         public override string ToString()
  208.         {
  209.             string formatPriceAndCurrency = this.price.ToString("N", CultureInfo.InvariantCulture);
  210.             formatPriceAndCurrency = formatPriceAndCurrency.Contains(',', StringComparison.InvariantCulture) ? $"\"{formatPriceAndCurrency} {this.currency}\"" : $"{formatPriceAndCurrency} {this.currency}";
  211.  
  212.             if (!this.HasIsni)
  213.             {
  214.                 return $"{this.Title}, {this.authorName}, ISNI IS NOT SET, {formatPriceAndCurrency}, {this.amount}";
  215.             }
  216.  
  217.             return $"{this.Title}, {this.authorName}, {this.isni}, {formatPriceAndCurrency}, {this.amount}";
  218.         }
  219.  
  220.         private static bool ValidateIsni(string? isni)
  221.         {
  222.             if (isni.Length != 16)
  223.             {
  224.                 return false;
  225.             }
  226.  
  227.             for (int i = 0; i < isni.Length; i++)
  228.             {
  229.                 if (CharToInt(isni[i]) == -1)
  230.                 {
  231.                     return false;
  232.                 }
  233.             }
  234.  
  235.             return true;
  236.         }
  237.  
  238.         private static bool ValidateIsbn(string? isbn)
  239.         {
  240.             if (isbn.Length != 16)
  241.             {
  242.                 return false;
  243.             }
  244.  
  245.             for (int i = 0; i < isbn.Length; i++)
  246.             {
  247.                 if (CharToInt(isbn[i]) == -1)
  248.                 {
  249.                     return false;
  250.                 }
  251.             }
  252.  
  253.             return ValidateIsbnChecksum(isbn);
  254.         }
  255.  
  256.         private static bool ValidateIsbnChecksum(string? isbn)
  257.         {
  258.             int checkSum = 0;
  259.             for (int i = 0; i < isbn.Length; i++)
  260.             {
  261.                 checkSum += CharToInt(isbn[i]) * (10 - i);
  262.             }
  263.  
  264.             return checkSum % 11 == 0;
  265.         }
  266.  
  267.         // TODO Add a static method.
  268.  
  269.         private static int CharToInt(char digit) =>
  270.             digit switch
  271.             {
  272.                 '0' => 0,
  273.                 '1' => 1,
  274.                 '2' => 2,
  275.                 '3' => 3,
  276.                 '4' => 4,
  277.                 '5' => 5,
  278.                 '6' => 6,
  279.                 '7' => 7,
  280.                 '8' => 8,
  281.                 '9' => 9,
  282.                 'X' => 10,
  283.                 _ => -1
  284.             };
  285.     }
  286. }
  287.  
Add Comment
Please, Sign In to add comment