Exemplo n.º 1
0
        public void TryParseTests(string input, string expected, ISBN10.ResultCode resultCode)
        {
            var result = ISBN10.TryParse(input, out var isbn10);

            result.Should().Be(resultCode);
            if (resultCode == ISBN10.ResultCode.Success)
            {
                isbn10.ISBN10Code.Should().Be(expected);
            }
            else
            {
                isbn10.Should().Be(ISBN10.Empty);
            }
        }
Exemplo n.º 2
0
        public static ResultCode TryParse(string isbn, out ISBN10 value)
        {
            if (string.IsNullOrWhiteSpace(isbn))
            {
                value = Empty;
                return(ResultCode.Invalid);
            }

            isbn = isbn.Trim();
            if (isbn.Length != 10)
            {
                value = Empty;
                return(ResultCode.InvalidNumberOfDigits);
            }

            int sum = 0;

            for (int i = 0; i < 9; i++)
            {
                if (!int.TryParse(isbn[i].ToString(), out var digit))
                {
                    value = Empty;
                    return(ResultCode.InvalidNotAValidNumber);
                }
                sum += (i + 1) * digit;
            }

            int remainder = sum % 11;

            if (remainder == 10)
            {
                if (isbn[9] == 'X')
                {
                    value = new ISBN10(isbn, 10);
                    return(ResultCode.Success);
                }
            }

            var validChecksum = isbn[9] == (char)('0' + remainder);

            if (!validChecksum)
            {
                value = Empty;
                return(ResultCode.InvalidChecksum);
            }

            value = new ISBN10(isbn, remainder);
            return(ResultCode.Success);
        }
Exemplo n.º 3
0
        public void ConvertISBN10ToEan13Examples()
        {
            //Convert to Ean13 directly
            if (Ean13.TryConvertFomISBN10("0306406152", out var ean13) == Ean13.ResultCode.Success)
            {
                Console.WriteLine($"Convert ISBN10 from string; Ean13: {ean13.Ean13Code} Checksum: {ean13.Checksum}");
            }

            //Pass an ISBN10 struct
            if (ISBN10.TryParse("0306406152", out var isbn10) == ISBN10.ResultCode.Success)
            {
                if (Ean13.TryConvertFomISBN10(isbn10, out ean13) == Ean13.ResultCode.Success)
                {
                    Console.WriteLine($"Convert ISBN10 from struct; Ean13: {ean13.Ean13Code} Checksum: {ean13.Checksum}");
                }
            }
        }
Exemplo n.º 4
0
        public void ISBN10Examples()
        {
            //Validate an ISBN10
            if (ISBN10.TryParse("0306406152", out var isbn10) == ISBN10.ResultCode.Success)
            {
                Console.WriteLine($"Code: {isbn10.ISBN10Code} Checksum: {isbn10.Checksum}");
                //: Code: 0306406152 Checksum: 2
            }

            // ISBN10 with an invalid checksum
            var invalidChecksumInput = "0306406150";

            var parseResultCode = ISBN10.TryParse(invalidChecksumInput, out isbn10);

            Console.WriteLine($"Parse result for {invalidChecksumInput} is {parseResultCode}");
            //: Parse result for 0306406150 is InvalidChecksum
        }
Exemplo n.º 5
0
        /// <summary>
        /// Processes the specified ISBN.
        /// </summary>
        /// <param name="isbn">The ISBN to process.</param>
        /// <returns>The <see cref="IISBN"/> generated.</returns>
        static IISBN ProcessISBN(string isbn)
        {
            IISBN ret = null;

            try
            {
                if (isbn.Length == 10)
                {
                    ret = new ISBN10(isbn);
                }
                else if (isbn.Length == 13)
                {
                    ret = new ISBN13(isbn);
                }
            }
            catch { }   // don't care
            return(ret);
        }
Exemplo n.º 6
0
        public static ISBN13 From(ISBN10 isbn)
        {
            string newIsbn = "978-" + isbn;

            var clearIsbn = Regex.Replace(newIsbn, "-", "");

            var digits = clearIsbn
                         .Select(x => x == 'X' ? 0 : int.Parse(x.ToString()))
                         .ToArray();

            int checksum = 0;

            for (int i = 0; i < 12; i++)
            {
                checksum += (i + 1) % 2 != 0 ? digits[i] : digits[i] * 3;
            }
            checksum %= 10;
            checksum  = checksum > 0 ? 10 - checksum : 0;

            newIsbn = newIsbn.Substring(0, newIsbn.Length - 1) + checksum.ToString();

            return(new ISBN13(newIsbn));
        }
Exemplo n.º 7
0
 public void TryParsePropertyTestNoExceptions(NonEmptyString input)
 {
     ISBN10.TryParse(input.Get, out _);
 }
Exemplo n.º 8
0
 public void FromISBN10Test(string isbn10Str, long expected)
 {
     ISBN10.TryParse(isbn10Str, out var isbn10);
     Ean13.TryConvertFomISBN10(isbn10, out var ean13).Should().Be(Ean13.ResultCode.Success);
     ean13.Ean13Code.Should().Be(expected);
 }