Advertisement
yahorrr

Untitled

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