Advertisement
yahorrr

Untitled

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