Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Globalization;
- namespace BookStoreItem
- {
- /// <summary>
- /// Represents the an item in a book store.
- /// </summary>
- public class BookStoreItem
- {
- private readonly string? authorName;
- private readonly string? isni;
- private readonly bool hasIsni;
- private decimal price;
- private string currency;
- private int amount;
- /// <summary>
- /// 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"/>.
- /// </summary>
- /// <param name="authorName">A book author's name.</param>
- /// <param name="title">A book title.</param>
- /// <param name="publisher">A book publisher.</param>
- /// <param name="isbn">A book ISBN.</param>
- public BookStoreItem(string authorName, string title, string publisher, string isbn)
- : this(authorName, null, title, publisher, isbn)
- {
- }
- /// <summary>
- /// 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"/>.
- /// </summary>
- /// <param name="authorName">A book author's name.</param>
- /// <param name="isni">A book author's ISNI.</param>
- /// <param name="title">A book title.</param>
- /// <param name="publisher">A book publisher.</param>
- /// <param name="isbn">A book ISBN.</param>
- public BookStoreItem(string authorName, string? isni, string title, string publisher, string isbn)
- : this(authorName, isni, title, publisher, isbn, null, string.Empty, 0, "USD", 0)
- {
- }
- /// <summary>
- /// 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"/>.
- /// </summary>
- /// <param name="authorName">A book author's name.</param>
- /// <param name="title">A book title.</param>
- /// <param name="publisher">A book publisher.</param>
- /// <param name="isbn">A book ISBN.</param>
- /// <param name="published">A book publishing date.</param>
- /// <param name="bookBinding">A book binding type.</param>
- /// <param name="price">An amount of money that a book costs.</param>
- /// <param name="currency">A price currency.</param>
- /// <param name="amount">An amount of books in the store's stock.</param>
- public BookStoreItem(string authorName, string title, string publisher, string isbn, DateTime? published, string bookBinding, decimal price, string currency, int amount)
- : this(authorName, null, title, publisher, isbn, published, bookBinding, price, currency, amount)
- {
- }
- /// <summary>
- /// 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"/>.
- /// </summary>
- /// <param name="authorName">A book author's name.</param>
- /// <param name="isni">A book author's ISNI.</param>
- /// <param name="title">A book title.</param>
- /// <param name="publisher">A book publisher.</param>
- /// <param name="isbn">A book ISBN.</param>
- /// <param name="published">A book publishing date.</param>
- /// <param name="bookBinding">A book binding type.</param>
- /// <param name="price">An amount of money that a book costs.</param>
- /// <param name="currency">A price currency.</param>
- /// <param name="amount">An amount of books in the store's stock.</param>
- public BookStoreItem(string authorName, string? isni, string title, string publisher, string? isbn, DateTime? published, string bookBinding, decimal price, string currency, int amount)
- {
- if (string.IsNullOrWhiteSpace(authorName))
- {
- throw new ArgumentException("The author name is not set.", nameof(authorName));
- }
- if (!ValidateIsni(isni) && isni != null)
- {
- throw new ArgumentException("Invalid ISNI.", nameof(isni));
- }
- if (string.IsNullOrWhiteSpace(title))
- {
- throw new ArgumentException("The title is not set.", nameof(title));
- }
- if (string.IsNullOrWhiteSpace(publisher))
- {
- throw new ArgumentException("The publiser is not set.", nameof(publisher));
- }
- if ((!ValidateIsbn(isbn) || !ValidateIsbnChecksum(isbn)) && isbn != null)
- {
- throw new ArgumentException("Invalid ISBN", nameof(isbn));
- }
- if (string.IsNullOrWhiteSpace(currency))
- {
- throw new ArgumentException("The currency is not set.", nameof(currency));
- }
- this.authorName = authorName;
- this.isni = isni;
- this.currency = currency;
- this.amount = amount;
- this.Title = title;
- this.Publisher = publisher;
- this.Isbn = isbn;
- this.Published = published;
- this.BookBinding = bookBinding;
- this.Price = price;
- this.hasIsni = isni != null;
- }
- /// <summary>
- /// Gets a book author's name.
- /// </summary>
- public string? AuthorName => this.authorName;
- /// <summary>
- /// Gets an International Standard Name Identifier (ISNI) that uniquely identifies a book author.
- /// </summary>
- public string? Isni => this.isni;
- /// <summary>
- /// Gets a value indicating whether an author has an International Standard Name Identifier (ISNI).
- /// </summary>
- public bool HasIsni => this.hasIsni;
- /// <summary>
- /// Gets a book title.
- /// </summary>
- public string Title { get; private set; }
- /// <summary>
- /// Gets a book publisher.
- /// </summary>
- public string Publisher { get; private set; }
- /// <summary>
- /// Gets a book International Standard Book Number (ISBN).
- /// </summary>
- public string? Isbn { get; private set; }
- /// <summary>
- /// Gets or sets a book publishing date.
- /// </summary>
- public DateTime? Published { get; set; }
- /// <summary>
- /// Gets or sets a book binding type.
- /// </summary>
- public string BookBinding { get; set; }
- /// <summary>
- /// Gets or sets an amount of money that a book costs.
- /// </summary>
- public decimal Price
- {
- get => this.price;
- set
- {
- if (this.price < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(this.Price), "Price is less then zero.");
- }
- this.price = value;
- }
- }
- /// <summary>
- /// Gets or sets a price currency.
- /// </summary>
- public string Currency
- {
- get => this.currency;
- set
- {
- ThrowExceptionIfCurrencyIsNotValid(this.currency);
- this.currency = value;
- }
- }
- /// <summary>
- /// Gets or sets an amount of books in the store's stock.
- /// </summary>
- public int Amount { get => this.amount; set => this.amount = value; }
- /// <summary>
- /// Gets a <see cref="Uri"/> to the contributor's page at the isni.org website.
- /// </summary>
- /// <returns>A <see cref="Uri"/> to the contributor's page at the isni.org website.</returns>
- public Uri GetIsniUri()
- {
- if (!this.HasIsni)
- {
- throw new InvalidOperationException("URL cannot made because ISNI is not set.");
- }
- return new Uri($"https://isni.org/isni/{this.isni}");
- }
- /// <summary>
- /// Gets an <see cref="Uri"/> to the publication page on the isbnsearch.org website.
- /// </summary>
- /// <returns>an <see cref="Uri"/> to the publication page on the isbnsearch.org website.</returns>
- public Uri GetIsbnSearchUri() => new Uri($"https://isbnsearch.org/isbn/{this.Isbn}");
- /// <summary>
- /// Returns the string that represents a current object.
- /// </summary>
- /// <returns>A string that represents the current object.</returns>
- public override string ToString()
- {
- string formatPriceAndCurrency = this.price.ToString("N", CultureInfo.InvariantCulture);
- formatPriceAndCurrency = formatPriceAndCurrency.Contains(',', StringComparison.InvariantCulture) ? $"\"{formatPriceAndCurrency} {this.currency}\"" : $"{formatPriceAndCurrency} {this.currency}";
- if (!this.HasIsni)
- {
- return $"{this.Title}, {this.authorName}, ISNI IS NOT SET, {formatPriceAndCurrency}, {this.Amount}";
- }
- return $"{this.Title}, {this.authorName}, {this.isni}, {formatPriceAndCurrency}, {this.Amount}";
- }
- private static bool ValidateIsni(string? isni)
- {
- if (isni == null || isni.Length != 16)
- {
- return false;
- }
- for (int i = 0; i < isni.Length; i++)
- {
- if (CharToInt(isni[i]) == -1)
- {
- return false;
- }
- }
- return true;
- }
- private static bool ValidateIsbn(string? isbn)
- {
- if (isbn == null || isbn.Length != 10)
- {
- return false;
- }
- for (int i = 0; i < isbn.Length; i++)
- {
- if (CharToInt(isbn[i]) == -1)
- {
- return false;
- }
- }
- return true;
- }
- private static bool ValidateIsbnChecksum(string? isbn)
- {
- if (isbn == null)
- {
- return false;
- }
- int checkSum = 0;
- for (int i = 0; i < isbn.Length; i++)
- {
- checkSum += CharToInt(isbn[i]) * (10 - i);
- }
- return checkSum % 11 == 0;
- }
- private static void ThrowExceptionIfCurrencyIsNotValid(string currency)
- {
- if (currency.Length != 3)
- {
- throw new ArgumentException("Currency must contains only 3 symbols.", nameof(currency));
- }
- for (int i = 0; i < currency.Length; i++)
- {
- if (!char.IsLetter(currency[i]))
- {
- throw new ArgumentException("Invalid symbols in currency.", nameof(currency));
- }
- }
- }
- private static int CharToInt(char digit) =>
- digit switch
- {
- '0' => 0,
- '1' => 1,
- '2' => 2,
- '3' => 3,
- '4' => 4,
- '5' => 5,
- '6' => 6,
- '7' => 7,
- '8' => 8,
- '9' => 9,
- 'X' => 10,
- _ => -1
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement