Advertisement
yahorrr

Untitled

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