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, string.Empty, 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, string.Empty, 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 (!ValidateIsni(this.isni))
- {
- throw new ArgumentException(" ", nameof(isni));
- }
- if (!ValidateIsbn(this.Isbn) && !ValidateIsbnChecksum(this.Isbn))
- {
- throw new ArgumentException(" ", nameof(isbn));
- }
- this.authorName = authorName;
- this.isni = isni;
- this.currency = currency;
- this.Title = title;
- this.Publisher = publisher;
- this.Isbn = isbn;
- this.Published = published;
- this.BookBinding = bookBinding;
- this.Price = price;
- this.Amount = amount;
- 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 a book publishing date.
- /// </summary>
- public DateTime? Published { get; private set; }
- /// <summary>
- /// Gets a book binding type.
- /// </summary>
- public string BookBinding { get; private set; }
- /// <summary>
- /// Gets 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
- {
- //if (this.currency.Length != 3 || this.currency)
- //{
- // throw new ArgumentOutOfRangeException(nameof(this.Price), "Price is less then zero.");
- //}
- 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();
- }
- 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.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.Length != 16)
- {
- return false;
- }
- for (int i = 0; i < isbn.Length; i++)
- {
- if (CharToInt(isbn[i]) == -1)
- {
- return false;
- }
- }
- return ValidateIsbnChecksum(isbn);
- }
- private static bool ValidateIsbnChecksum(string? isbn)
- {
- int checkSum = 0;
- for (int i = 0; i < isbn.Length; i++)
- {
- checkSum += CharToInt(isbn[i]) * (10 - i);
- }
- return checkSum % 11 == 0;
- }
- // TODO Add a static method.
- 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
- };
- }
- }
Add Comment
Please, Sign In to add comment