Example #1
0
        /// <summary>
        /// Convering book to string with provided format
        /// </summary>
        public string ToString(string format)
        {
            if (string.IsNullOrEmpty(format))
            {
                format = "F";
            }
            format = format.ToUpper();

            switch (format)
            {
            case "AT":
                return(Author + ", " + Title);

            case "ATPPY":
                return(Author + ", " +
                       Title + ", " +
                       Publisher + ", " +
                       PublishYear.ToString());

            case "IAT":
                return("ISBN: " + ISBN + ", " +
                       Author + ", " +
                       Title);

            case "IATPPYPA":
                return("ISBN: " + ISBN + ", " +
                       Author + ", " +
                       Title + ", " +
                       Publisher + ", " +
                       PublishYear + ", " +
                       Pages.ToString());

            case "IATPPYPAPR":
                return("ISBN: " + ISBN + ", " +
                       Author + ", " +
                       Title + ", " +
                       Publisher + ", " +
                       PublishYear.ToString() + ", " +
                       Pages.ToString() + ", " +
                       Price.ToString());

            case "F":
                goto case "IATPPYPAPR";

            default:
                log.Warn("Wrong format type");
                throw new FormatException("Invalid format");
            }
        }
        /// <summary>
        /// Returns a string representation of the book, using the specified format.
        /// </summary>
        /// <param name="format">Avaliable formats:
        ///                     "G" - general
        ///                     "S" - short
        ///                     You can enumerate the components of the book you want to get, separeted by ','.
        ///                     In the result the components values will also be separeted by ','.
        ///                     Components shortcuts:
        ///                     "ISBN" - Books ISBN number
        ///                     "AUT" - Books authors
        ///                     "TT" - Books Title
        ///                     "PB" - Books publisher
        ///                     "PBY" - Books publish year
        ///                     "PG" - Books number of pages
        ///                     "PR" - Books Price
        ///                     Example:
        ///                     The general format looks like this "ISBN,AUT,TT,PB,PBY,PG,PR".
        ///                     The short format "AUT,TT".
        ///                     </param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            string defaultFormat = "G";

            if (format == null)
            {
                format = defaultFormat;
            }

            if (formatProvider == null)
            {
                formatProvider = CultureInfo.CurrentCulture;
            }

            format = format.ToUpper();

            string generalFormat =
                "ISBN,AUT,TT,PB,PBY,PG,PR";
            string shortFormat =
                "AUT,TT";

            if (format == "G")
            {
                format = generalFormat;
            }

            if (format == "S")
            {
                format = shortFormat;
            }


            string[] fmtParts = format.Split(',');

            StringBuilder result = new StringBuilder();

            for (int i = 0; i < fmtParts.Length; i++)
            {
                if (i != 0)
                {
                    result.Append(", ");
                }

                switch (fmtParts[i])
                {
                case "ISBN":
                    result.Append(ISBN.ToString());
                    break;

                case "AUT":
                    result.Append(Author.ToString(formatProvider));
                    break;

                case "TT":
                    result.Append(Title.ToString(formatProvider));
                    break;

                case "PB":
                    result.Append(Publisher.ToString(formatProvider));
                    break;

                case "PBY":
                    result.Append(PublishYear.ToString(formatProvider));
                    break;

                case "PG":
                    result.Append($"P.{NumberOfPages.ToString(formatProvider)}");
                    break;

                case "PR":
                    result.Append(Price.ToString(formatProvider));
                    break;

                default:
                    throw new FormatException($"{fmtParts[i]} format is not supported");
                }
            }

            return(result.ToString());
        }